-->
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: many to many with association - need help
PostPosted: Sun Jun 15, 2008 9:15 am 
Newbie

Joined: Mon Jun 09, 2008 2:17 am
Posts: 1
hi,

I am new to the hibernate and trying to establish many to many relationship with association table. I have two tables

A. Signal
B. Force

Signal can hv many forces and vice versa. i will have the association table called signalForce which is having extra information (Extra fields) called imactDirection and comment. i have also one component called project which will be <many-to-one>

so, my force.hbm will be something like this (not the exact syntax just giving an idea)


<class Force>

<id></id>
<properties></properties>

<set name=signalForces lazy=true table="signalForce">
<key-column=force_id>
<component-class signalForce>
<property impactDirection>sig
<property comment>
<many-to-one class="project">
</set>

my first question is do i need to create a same set at reverse end that is in signal.hbm?

2nd query is do i need to override equals and hash code in my signalForce.java?

my 3rd query is when i say lazy=true i get the exception lazy initialization exception the underlaying connection is closed. this exception i get when i write

force.getSignalForces().add(signalForce);

how do i save my object?

i searched on the net and not able to get the complete example with this situation. can i get the complete example. hbm mapping and java method . any information would be of great help.

Regards,

hb_learner


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 15, 2008 10:39 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
This may depend on the CascadeType, but if force has a CascadeType of all, then when the force is saved, and the transaxction iscomitted, then the object and all associated objects will be saved as well.

Image

On the other hand, if the operation is not cascaded at all, you will need to pass each entity to the save method of the Hibernate session, even the objects you pass into the collection.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 15, 2008 5:30 pm 
Beginner
Beginner

Joined: Tue Dec 12, 2006 6:43 am
Posts: 32
Location: London
Hi,


This is a sample code which shows the many-to-many relation from the tutorial source code when you download hibernate.


<hibernate-mapping>

<class name="events.Event" table="EVENTS">
<id name="id" column="EVENT_ID">
<generator class="native"/>
</id>
<property name="date" type="timestamp" column="EVENT_DATE"/>
<property name="title"/>

<!-- making one of association inverse=true gives bidrectional mapping -->
<!-- you may add lazy="true" to the collection which return a
proxy collection when you retrieve event and try to get the participants
all dependence on your desing choice to choose your fetching strategry.

-->
<set name="participants" table="PERSON_EVENT" inverse="true">
<key column="EVENT_ID"/>
<many-to-many column="PERSON_ID" class="events.Person"/>
</set>

</class>


</hibernate-mapping>




<hibernate-mapping>

<class name="events.Person" table="PERSON">
<id name="id" column="PERSON_ID">
<generator class="native"/>
</id>
<property name="age"/>
<property name="firstname"/>
<property name="lastname"/>

<set name="events" table="PERSON_EVENT">
<key column="PERSON_ID"/>
<many-to-many column="EVENT_ID" class="events.Event"/>
</set>



</class>

</hibernate-mapping>


As you can see the DDL , there is an intersection table which has a composite primary key consist of the the PK from PERSON and EVENT table.



create table EVENTS (EVENT_ID bigint generated by default as identity (start with 1), EVENT_DATE timestamp, title varchar(255), primary key (EVENT_ID));
create table PERSON (PERSON_ID bigint generated by default as identity (start with 1), age integer, firstname varchar(255), lastname varchar(255), primary key (PERSON_ID));
create table PERSON_EVENT (EVENT_ID bigint not null, PERSON_ID bigint not null, primary key (PERSON_ID, EVENT_ID));

alter table PERSON_EVENT add constraint FKAD91D9107708282F foreign key (PERSON_ID) references PERSON;
alter table PERSON_EVENT add constraint FKAD91D910F96D1A45 foreign key (EVENT_ID) references EVENTS;


Again the decision to make the <set> lazy or out-joint="true|false|auto" dependenc on how you want to navigate the object graph.


Please take a look at the source code for the Tutorial for further details.

_________________
Alan Mehio
London
UK


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.