For beginners of hibernate, the solution of calling the save method of session does not work.

Source: Internet
Author: User
Recently, I started to study hibernate. The configuration file is ready, the mapping file is ready, and the session factory is ready. I got the session to test it. The program has not encountered any exceptions, looking at the data in the database, I found that the data was not inserted into the database. My call code is SessionsessionMySessionFactory. getSes

Recently, I started to study hibernate. The configuration file is ready, the mapping file is ready, and the session factory is ready. I got the session to test it. The program has not encountered any exceptions, looking at the data in the database, I found that the data was not inserted into the database. My call code is Session session = MySessionFactory. getSes

Recently, I started to study hibernate. The configuration file is ready, the mapping file is ready, and the session factory is ready. I got the session to test it. The program has not encountered any exceptions, I was about to look at the data in the database and found that the data was not inserted into the database. My call code is like this.
Session session = MySessionFactory. getSession (); User user = new User (); user. setID (1003); user. setUsername ("wowo"); user. setPwd ("111111"); user. setSex ('male'); user. setAge (24); Serializable s = session. save (user); System. out. println (s );

Why is there no data? I started to check the method provided by the session and found that there is a flush method. Do you need to refresh it after saving it? The result is flush, but it still doesn't work. The slot is depressing, no exception. Later, we can see that the save method is in things when people use hibernate. Do they need to put it in things? The result is to change the code

Transaction transaction = session. beginTransaction (); transaction. begin (); User user = new User (); user. setID (1003); user. setUsername ("wowo"); user. setPwd ("111111"); user. setSex ('male'); user. setAge (24); Serializable s = session. save (user); System. out. println (s); transaction. commit ();
YES. The positive solution must be placed in the transaction! We do not know much about hibernate. We can insert data into the database in this way.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.