I'm trying to get started with Hibernate, but can't insert data, for some reason. It seems to be working properly as no error is given, but when I check the database the table is empty. I don't think it the connection to the database itself that fail, because when I change the table name to a non-excisting one in the mapping xml, Hibernate creates this on the fly (But as I said, no data is inserted). Does anyone know what the problem could be?
Here's 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 mapping xml:
<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 will be able to help me! Thanks in advance :)
use this code and test in 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();
}
I suspect that the system is not committing the transaction that includes the needed insert. I always set transactions explicitly e.g. Hibernate Getting Started Tutorial Example 2.5
An alternative is that you should be able to set Hibernate's commit mode in you code so that transactions are implicit - look at FlushMode
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With