Data not inserted using hibernation but not giving errors?

I am trying to get started with Hibernate, but for some reason cannot insert data. It seems to work correctly, as no error is reported, but when I check the database, the table is empty. I donโ€™t think that it is connected with the database itself, because when I change the table name to non-excisting in xml, Hibernate creates it on the fly (but, as I said, the data is not inserted) Does anyone know what is the problem?

Here is my code:

hibernate.cfg.xml:

<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/intex</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password"></property> <property name="hibernate.connection.pool_size">10</property> <property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <!-- Mapping files --> <mapping resource="intex.hbm.xml"/> </session-factory> </hibernate-configuration> 

My xml mapping:

 <hibernate-mapping> <class name="com.intex.uni.courses.Course" table="course"> <id name="id" column="id" > <generator class="increment"/> </id> <property name="name" column="name" /> <property name="description" column="description" /> <property name="url" column="url" /> <property name="code" column="code" /> </class> </hibernate-mapping> 

And my test client:

 public class HibernateTest { public static void main(String[] args) { Session session = null; try { SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); session = sessionFactory.openSession(); Course course = new Course(); course.setDescription("Description"); course.setName("NAME"); course.setUrl("http://www.url.com"); session.save(course); } catch (Exception e) { System.out.println(e.getMessage()); } finally { session.flush(); session.close(); } } } 

I really hope someone can help me! Thanks in advance:)

+4
source share
4 answers

use this code and test it in the main class.

 Session session = null; Transaction txn = null; try { SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); session = sessionFactory.openSession(); txn = session.beginTransaction(); Course course = new Course(); course.setDescription("Description"); course.setName("NAME"); course.setUrl("http://www.url.com"); session.save(course); txn.commit(); } catch (Exception e) { System.out.println(e.getMessage()); } finally { if (!txn.wasCommitted()) { txn.rollback(); } session.flush(); session.close(); } 
+5
source

I suspect that the system is not committing a transaction that includes the desired insert. I always did transactions explicitly, for example. Hibernate Getting Started Tutorial Example 2.5

An alternative is that you have to set the Hibernate commit mode in your code so that transactions are implicit - see FlushMode

+5
source

Try using the following code:

 public class HibernateTest { public static void main(String[] args) { Session session = null; Transaction txn = null; try { SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); session = sessionFactory.openSession(); txn = session.beginTransaction(); Course course = new Course(); course.setDescription("Description"); course.setName("NAME"); course.setUrl("http://www.url.com"); session.save(course); txn.commit(); } catch (Exception e) { System.out.println(e.getMessage()); } finally { session.flush(); session.close(); } } } 
0
source

Please start the transaction before saving and committing the transaction after saving.

0
source

All Articles