Hibernate many-to-one reference update

J

jstorta

I have 2 related tables with a many-to-one mapping setup. It all
works great when retrieving information, but I cannot figure out a way
to perform an update to the table with the foreign key.

I am simplifying what I have for the purposes this discussion. There
is more configuration with regards to DAOs and Beans and Spring
config, but I do not know that it pertains to this so I will leave it
out unless I need to include it.

Here are the basics of my configuration.

*******Database tables
contact
contact_id

project
project_id
contact_id


*******Corresponding classes
public class Contact {
private String contactId;
//getters, setters, constructors etc
}

public class Project {
private String projectId;
private Contact projectOwner;
//getters, setters, constructors etc
}

*******hbm mappings
<hibernate-mapping>
<class name="mypkg.Contact" table="contact">
<id column="contact_id" name="contactId"/>
</class>
</hibernate-mapping>

<hibernate-mapping>
<class name="mypkg.Project" table="project">
<id column="project_id" name="projectId">
<generator class="increment"/>
</id>
<many-to-one name="projectOwner" class="mypkg.Contact"
column="contact_id" lazy="false"/>
</class>
</hibernate-mapping>


I am using JSF to display the contents of the project table. It works
great. hibernate pulls all of the values and populates the Contact
object within each Project object. My problem is, how do I update the
contact_id foreign key on the project table?

I do not want to touch the Contact object or table. I just want to
update the contact_id field in the project table so that the project
references a different contact.

I am sure I can come up with some way to do this, but it seems like
something that should be pretty common and I would like to avoid
needless coding.

Thanks,
John
 
J

jstorta

I fear that in trying to explain my problem I may have convoluted what
I am trying to do.

The crux of the issue is that I have a table with a foreign key to
another table. I am trying to figure out how to update this foreign
key through Hibernate.

Let's say my contacts are
contact_id name
1234 joe Smith
5678 John Doe

And I have the following project
project_id contact_id
987 1234


And I want to update the contact_id to 5678.


When Hibernate pulls the data using a many-to-one mapping, my Project
object is populated with the project_id and the Contact object.

I.E.
class Project {
private String projectId;
private Contact contact; // <-- Not the foreign key, but the object
referenced by the key
}

If I set the contact object to be the 5678 contact, will Hibernate be
smart enough to update the contact_id foreign key on the project
table? Is there something special I need to do to make this happen?


Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top