-->
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.  [ 6 posts ] 
Author Message
 Post subject: Able to retrieve object with collection but not saving.
PostPosted: Fri Dec 04, 2009 8:01 am 
Newbie

Joined: Fri Dec 04, 2009 5:52 am
Posts: 9
I have doing the very first example: Person and Event
It defines the one-to-many relationship. One person is assigned many events.

My problem is that i m not abled to store event for a person. But i can retrieve details.
xml goes like this:
<class name="com.dynpro.bean.Person" table="PERSON" lazy="false">
<id name="id" column="PERSON_ID" type="long">
<generator class="native" />
</id>
<property name="age" />
<property name="firstname" />
<property name="lastname" />
<set name="events" table="EVENT">
<key column="PERSON_ID" />
<many-to-many column="id" class="com.dynpro.bean.Event" />
</set>
</class>

Tables are:
Person table:person_id,age,firstname,lastname
Event: id,title,date

This is similar hibernate reference's first example
Please anyone help with this

Advanced Thanks,
Jyeshta


Top
 Profile  
 
 Post subject: Re: Able to retrieve object with collection but not saving.
PostPosted: Fri Dec 04, 2009 8:09 am 
Regular
Regular

Joined: Mon Aug 07, 2006 5:07 am
Posts: 56
If you do "person.addEvent(event);" or do "person.setEvents(eventsSet);" and then save the person instance without saving the event first, you have to have cascade="save-update" or something on your event set in the hibernate mapping file.

Otherwise, if you don't specify cascading, you have to do something like following:
Code:
session.save(event);
person.addEvent(event);
session.save(person);


Top
 Profile  
 
 Post subject: Re: Able to retrieve object with collection but not saving.
PostPosted: Mon Dec 07, 2009 2:58 am 
Newbie

Joined: Fri Dec 04, 2009 5:52 am
Posts: 9
hi don thanks for your reply.

hmm well.. i got the concept but i struck out with another problem.
Now it s giving the error: Duplicate entry '1' for key 1


Altered mapping file is:
<set name="events" table="EVENT" cascade="save-update">
<key column="personid" />
<many-to-many column="id" class="com.dynpro.bean.Event" />
</set>
key column is field specified is 'personid' in 'event' table which refers 'person_id' of person table.
'id' is PK for event table.
And i have specified :<property name="personid" column="personid" /> in event.hbm.xml.

I am confused here: i wanted to make sure which field u will specify in key column and what field to specify in many-to-many


Top
 Profile  
 
 Post subject: Re: Able to retrieve object with collection but not saving.
PostPosted: Mon Dec 07, 2009 3:44 am 
Regular
Regular

Joined: Mon Aug 07, 2006 5:07 am
Posts: 56
I used to have that error a lot, but now it's about 3 years ago I was able to use Hibernate in a real project (pfff, EJB2)
I'll check it out when I have time :-)


Top
 Profile  
 
 Post subject: Re: Able to retrieve object with collection but not saving.
PostPosted: Mon Dec 07, 2009 5:43 am 
Newbie

Joined: Fri Dec 04, 2009 5:52 am
Posts: 9
ok..can i have example programs if u have it as a back up.and could u send me pdfs other than hibernate reference?? or any other stuff that could help me to learn???Here(bangalore) Hibernate wrox is not available

Thanks,
Jyeshta


Top
 Profile  
 
 Post subject: Re: Able to retrieve object with collection but not saving.
PostPosted: Mon Dec 07, 2009 9:58 am 
Newbie

Joined: Mon Dec 07, 2009 9:07 am
Posts: 2
-


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