-->
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: How to update only the join (foreign key) column
PostPosted: Thu Mar 23, 2006 4:41 pm 
Newbie

Joined: Thu Mar 23, 2006 4:02 pm
Posts: 4
Hello,

I'm a new Hibernate user but an experienced Java developer.

I'm using Hibernate 3.0 with Spring on WebLogic and Oracle, but I don't think that has any bearing on my problem.

I have three entities (classes) that are each stored on their own database table: Participant, Activity, and Option. These objects are managed with Hibernate, so far so good with no problems.

The business domain says that these three entities can be related each other to form a relationship. To model this, I created a ParticipantActivityOption class that has three member properties (in addition to a unique ID and audit-type stuff): a Participant, an Activity, and an Option. The database table that backs the ParticipantActivityOption class has three columns for these member properties that store the unique ID of each entity (the foreign key).

Since a ParticipantActivityOption is only related to one Participant or Activity or Option, and a Participant or Activity or Option can be related to many ParticipantActivityOptions, the ParticipantActivityOption.hbm.xml file looks like this to model those relationships:

<many-to-one name="participant" cascade="none" class="Participant" ... />


<many-to-one name="activity" cascade="none" class="Activity" ... />


<many-to-one name="option" cascade="none" class="Option" .... />

The problem I'm having is when it comes to saving the ParticipantActivityOption. To save, what I want to do is:

1. Create an instance of ParticipantActivityOption
2. Create instances of Participant, Activity, and Option, and since they are already persistent entities, set the ID on each object.
3. Set these three instances on the ParticipantActivityOption
4. Save the ParticipantActivityOption

When I try to do this, I get an error that says something like this:

org.hibernate.PropertyValueException: not-null property references a null or transient value: ParticipantActivityOption.participant

Again, I'm a Hibernate newbie, but I'm pretty sure that Hibernate is complaining because I'm using detached instances of Participant, Activity, and Option. (Each instance also has other relationships that are null on the detached object I create).

I think Hibernate wants to update the row representing the Participant based on the instance that is set on the ParticipantActivityObject, but I don't want it to. All I want it to do is update the ID columns on the ParticipantActivityOption table to point to individual, existing entities. I don't want to update the other entities, but I think that's what Hibernate is trying to do.

Can I make Hibernate do this, and if so, how? If not, what's the best way around the problem? I'm pretty sure that before saving the ParticipantActivityOption I can get the attached instances of each entity and set those on the ParticipantActivityOption and then try to save, but that just seems like a whole lot of work for updating the foreign key columns.

Any help is greatly appreciated.

Thank you!!!!

Ryan





Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 23, 2006 5:19 pm 
Newbie

Joined: Tue Mar 21, 2006 1:42 pm
Posts: 14
I beleive I understand your post, it's difficult because a lot is said, but little code is shown and the full stack trace is not shown. I imagine that there is another underlying problem. However, with that said you could map the foreign key columns in the ParticipantActivityOption.hbm.xml and also map the relationships, but mark them as insert="false" and update="false". Then all you need to is call participantActivityOption.setParticipantId(particpant.getId()) and the hbm needs to look like:

<property name="participantId" ... />

<many-to-one name="participant" cascade="none" class="Participant"
insert="false" update="false" ... />

_________________
Mike Ringrose


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 24, 2006 7:09 am 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Hi,

Quote:
1. Create an instance of ParticipantActivityOption
2. Create instances of Participant, Activity, and Option, and since they are already persistent entities, set the ID on each object.
3. Set these three instances on the ParticipantActivityOption
4. Save the ParticipantActivityOption


Can you please post the code where you are doing this ? I don't like the part with "set the ID on each object". Smells like a problem :).

Anyway:

getsession
begin transaction
create Participant, Activity, and Option
save Participant, Activity, and Option
create ParticipantActivityOption
save ParticipantActivityOption
commit transaction
close session

Next step will be to improve a little but first let's see this is working.


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.