Michael Horwitz wrote:
On 12/5/07, *Mike Wille* <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote:
Hi All,
I've been playing with Appfuse/Spring WebMVC/Hibernate and made a
change
to the base User object. I added a many-to-one reference to an
Organization object. Everything works fine in my unit tests for
creating a user, setting its organization property, and saving
it. The
problem comes in when I do all of that across transactions while
running
the webapp. I have a property editor for setting the user's
organization after the form is submitted. The property editor
calls the
orgDao to load the organization based off the passed in orgID. When I
perform the userDao.save(user) I get this exception message:
org.hibernate.TransientObjectException: object references an unsaved
transient instance - save the transient instance before flushing:
tutorial.model.Organization
I was able to setup a unit test that reproduces the problem:
User user = userDao.get(-1L);
assertEquals(-1L, user.getOrganization().getId());
user.setOrganization(orgDao.get(-1L));
endTransaction();
dao.save(user);
Though the organization already exists in the database, the transient
exception is thrown. If I remove the endTransaction() statement, the
save() is successful. My annotations for the organization field
in the
user object looks like:
@ManyToOne
@JoinColumn(name = "OrganizationID", nullable = false)
public Organization getOrganization()
You might want to try @ManyToOne(cascade=CascadeType.ALL) here.
From the Hibernate docs, I originally had: @ManyToOne(cascade = {
CascadeType.PERSIST, CascadeType.MERGE })
That wasn't working. I tried every other combination except ALL. And
ALL works!
Thank you so much!
-Mike