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: