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 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:)
source share