-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Problem when saveOrUpdate new child on existing parent
PostPosted: Wed Mar 10, 2010 12:32 pm 
Newbie

Joined: Wed Mar 10, 2010 12:03 pm
Posts: 7
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


Top
 Profile  
 
 Post subject: Re: Problem when saveOrUpdate new child on existing parent
PostPosted: Wed Mar 10, 2010 12:39 pm 
Newbie

Joined: Wed Mar 10, 2010 12:03 pm
Posts: 7
Fixed ..

The problem is that my test is written incorrectly !

Sorry !

Kind regards

Armando


Top
 Profile  
 
 Post subject: Re: Problem when saveOrUpdate new child on existing parent
PostPosted: Wed Mar 10, 2010 12:45 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Your mapping is not consistent and the code is not correct. Read the example about parent/child in the Hibernate documentation http://docs.jboss.org/hibernate/stable/ ... child.html to find out that you among other things need to set inverse="true" in the <map> and that you need to set both ends of the association in your code. Eg. r.setMedia(m) and m.addRubric(r) (maybe you already do this in the addRubric() method... but it looks like you don't)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.