Hi ...
I'm a noob with hibernate. Learning it now, but something weird is happening ...
have two entities .. Media and Rubric (of media)
And I wrote two beans for them and configs etc ... ant Test class which fails and that's why I'm asking for your help !
My database tables are created when I start tests.
I use
Code:
<property name="hbm2ddl.auto">update</property>
setting
config for media
Code:
<hibernate-mapping package="my.package">
<class name="Media" table="media">
<id name="id" type="my.package.hibernate.UUIDUserType">
<column name="id" sql-type="uuid" />
</id>
<property column="name" name="name" type="string" />
<map name="rubrics" cascade="all">
<key column="fk_media_id" />
<map-key column="id" type="string" />
<one-to-many class="Rubric" />
</map>
</class>
</hibernate-mapping>
config for rubric:
Code:
<hibernate-mapping package="my.package">
<class name="Rubric" table="rubric">
<id name="id" type="my.package.hibernate.UUIDUserType">
<column name="id" sql-type="uuid" />
</id>
<many-to-one name="media" foreign-key="fk_media_id">
<column name="fk_media_id" sql-type="uuid" />
</many-to-one>
<property column="title" name="title" type="string"/>
<property column="updated" name="updated" type="timestamp"/>
</class>
</hibernate-mapping>
So my test goes like this:
- new Media()
- set media data
- store media (saveOrUpdate())
- new Rubric()
- set rubric data
- store rubric (saveOrUpdate())
- load created media
- new Rubric
- add rubric to media
- store media !!!(saveOrUpdate) (with new rubrics in a map)
first time when I start the test .. it passes .. if I repeat the test with tables created and data inside .. it fails ! I can't find out whats the problem with the configuration
The second time I call my test method, and It gets to step "store rubric", insert queries appear and after that queries for "update Media" start to appear and they fail, because they're all wrong .. and I don't even want to update media when I store new Rubric on a media .. :S
my test code
Code:
session = HibernateUtil.getSessionFactory().getCurrentSession();
System.out.println("CREATE MEDIA");
session.beginTransaction();
Media m = new Media();
m.setId("cd4b2f41-ddc2-4a63-96cf-2b64f3d24002"); // I WANT IT ALL THE SAME !
m.setName("Media 1");
session.saveOrUpdate(m);
System.out.println("CREATE R1 ");
//one way to add a Rubric to a media
Rubric r = new Rubric();
r.setMedia(m);
r.setTitle("Rubric 1");
r.setTitle(sl,"Rubrika 1");
session.saveOrUpdate(r);
session.getTransaction().commit();
session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
System.out.println("LOAD MEDIA");
Media m1 = (Media)session.load(Media.class, m.getId());
//another way to add a Rubric to a media
System.out.println("CREATE R2 ");
Rubric r2 = new Rubric();
r2.setTitle("Rubric 2");
r2.setTitle(sl,"Rubrika 2");
m1.addRubric(r2);
System.out.println("SAVE MEDIA 1 ");
session.saveOrUpdate(m1);
and console stuff from test
Code:
CREATE MEDIA
Hibernate: select media_.id, media_.name as name6_, media_.short_name as short3_6_, media_.circulation as circulat4_6_, media_.media_reach as media5_6_, media_.page_size as page6_6_, media_.is_active as is7_6_, media_.updated as updated6_ from media media_ where media_.id=?
CREATE R1
Hibernate: select rubric_.id, rubric_.fk_media_id as fk2_7_, rubric_.updated as updated7_ from rubric rubric_ where rubric_.id=?
Hibernate: insert into rubric (fk_media_id, updated, id) values (?, ?, ?)
Hibernate: update media set name=?, short_name=?, circulation=?, media_reach=?, page_size=?, is_active=?, updated=? where id=?
Hibernate: update rubric set fk_media_id=null, id=null where fk_media_id=?
2010-03-10 17:03:08,204 WARN [org.hibernate.util.JDBCExceptionReporter] SQL Error: 0, SQLState: 23502
2010-03-10 17:03:08,204 ERROR [org.hibernate.util.JDBCExceptionReporter] Batch entry 0 update rubric set fk_media_id=null, id=null where fk_media_id='cd4b2f41-ddc2-4a63-96cf-2b64f3d24002' was aborted. Call getNextException to see the cause.
2010-03-10 17:03:08,205 WARN [org.hibernate.util.JDBCExceptionReporter] SQL Error: 0, SQLState: 23502
2010-03-10 17:03:08,205 ERROR [org.hibernate.util.JDBCExceptionReporter] ERROR: null value in column "id" violates not-null constraint
2010-03-10 17:03:08,205 ERROR [org.hibernate.event.def.AbstractFlushingEventListener] Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
Note: I left out some of the properties in confs and code that have nothing to do with the problem ...
Any help would be appreciated !
Kind regards
Armando