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.