-->
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.  [ 4 posts ] 
Author Message
 Post subject: Many to one, one to many
PostPosted: Mon Dec 07, 2009 5:29 am 
Newbie

Joined: Mon Dec 07, 2009 5:09 am
Posts: 3
Hello,
I have a trouble with HB, I hope someone can help me...

I have 2 entities: Files and Details with relation One File -> Many Details.
First of all I would like to know if it is necessary to set in HB mappings the one to many Set in "Files" entity java object and also the many to one Object in "Details" entity java object.

My trouble is that sometimes (not always) when I save a new File with a new Details but with a Detail identical to an existing one the new File is created with not the Details, the Details reference of the existing one is updated referencing to the new File... i can no explain it very well, i try with an example:

Existing data in DB:
File(id: 1, name: Foo) -> Details (id:1, files: 1, string: Test)

I create new Object:
File(id: ?, name: Bar) -> Details (id:?, files: ?, string: Test)

Hibernate is set to get the new ids from the Oracle Sequences.
When i saveOrUpdate the File object this happend:
insert: File(id: 2, name: Bar)
update: Details (id:1, files: 1, string: Test) to Details (id:1, files: 2, string: Test)

So the first file loses its detail.
I thought the troble could be in the primary key, but it is set just on its id, and the id for new files is set by the oracle sequence.
Why happends an update for Details also if I made a: new DetailsEntity(null,"Test"); ? If i'm not wrong the update must be provided only if I get an object from a select and i change it, not if create a new java entity.


Thank you for your help.


Top
 Profile  
 
 Post subject: Re: Many to one, one to many
PostPosted: Mon Dec 07, 2009 5:59 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
Please post your mappings.


Top
 Profile  
 
 Post subject: Re: Many to one, one to many
PostPosted: Mon Dec 07, 2009 6:43 am 
Newbie

Joined: Mon Dec 07, 2009 5:09 am
Posts: 3
These are the two mappings.

Code:
<hibernate-mapping>
<class name="entity.FilesEntity" table="FILES">
  <id name="id" type="java.lang.Long">
   <column name="ID" precision="22" scale="0"/>
   <generator class="sequence">
    <param name="sequence">SEQ_FILES</param>
   </generator>
  </id>
  <property generated="never" lazy="false" name="name" type="string">
   <column length="64" name="NAME" not-null="true"/>
  </property>
  <set cascade="all-delete-orphan" inverse="true"  name="details" order-by="ID" sort="unsorted">
   <key>
    <column name="FILE" not-null="true" precision="22" scale="0"/>
   </key>
   <one-to-many class="entity.DetailsEntity"/>
  </set>
</class>
</hibernate-mapping>

Code:
<hibernate-mapping>
<class name="entity.DetailsEntity" table="DETAILS">
  <id name="id" type="java.lang.Long">
   <column name="ID" precision="22" scale="0"/>
   <generator class="sequence">
    <param name="sequence">SEQ_DETAILS</param>
   </generator>
  </id>
  <many-to-one
   class="entity.FilesEntity"
   fetch="select" name="FilesEntity" update="false">
   <column name="FILE" not-null="true" precision="22" scale="0"/>
  </many-to-one>
  <property generated="never" lazy="false" name="string" type="string">
   <column length="64" name="STRING"/>
  </property>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: Many to one, one to many
PostPosted: Wed Dec 09, 2009 12:35 pm 
Newbie

Joined: Mon Dec 07, 2009 5:09 am
Posts: 3
Can be the "many-to-one" attribute update="false" useful?
Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.