Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Mapping a One-to-Many Association using a Foreign Key
PostPosted: Fri Mar 16, 2012 10:07 am 
Newbie

Joined: Fri Mar 16, 2012 9:33 am
Posts: 1
Hi everyone,
I'm writing this, to know if there is a way to map a one-to-many relationship, using a entity at one side and a foreing-key at the other as a Long/Integer.
I'll put my example:

public class ClassA {
__ private idClassA
__ private List<ClassB> classesB;
}

public class ClassB {
__private idClassB;
__private Long idClassA_FK;
}


this are my hbm.xml files for both entities:


<class name="ClassA" table="CLASS_A">
__<id name="idClassA" type="long">
____<column name="ID" sql-type="BIGINT" />
____<generator class="native" />
__</id>
__<bag name="classesB" lazy="false" cascade="save-update,delete-orphan">
____<key column="ID_CLASS_A" />
____<one-to-many class="ClassB" />
__</bag>
</class>

<class name="ClassB" table="CLASS_B">
__<id name="idClassB" type="long">
____<column name="ID" sql-type="BIGINT" />
____<generator class="native" />
__</id>
__<property name="idClassA_FK">
____<column name="ID_CLASS_A" sql-type="BIGINT" />
__</property>
</class>


I don't get any error from this, It works well. But I have the next problem: when I persist a ClassA the ClassB.idClassA_FK value is null, so, I need to iterate over the list, and set the new Id of ClassA into this FK, and then make an update again. I wanna know if exist a way, that hibernate sets the ClassB.idClassA_FK automatically when I send to persist ClassA.

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

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.