On 12/5/07, Mike Wille <[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.

Mike

The organization object has no references to users and simply consists
> of ID, Name, and Version properties.  I have provided toString(),
> hashCode(), and equals() for the organization object.
>
> Does anyone have any insight or ideas I can try to fix this problem?  I
> REALLY appreciate any help that can be provided!
>
> Thanks,
>
> -Mike
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>

Reply via email to