Re: "Detached entity passed to persist"

2017-11-28 Thread Kalle Korhonen
Surprisingly, this (re-attaching a detached entity) is not a simple
problem. First of all, you need to be very careful about holding onto
entities in your session. It's too easy with JPA/Hibernate to accidentally
keep the whole query resultset in memory when you think you are holding
onto a single object (check with your jvisualvm). Second, since you are
using Hibernate, you can re-attach an entity with (the now deprecated
operation) session.lock(entity, LockMode.NONE), not absolutely sure how the
latest versions behave. I've written about it at
http://www.tynamo.org/tapestry-conversations+guide/, referencing this
stackoverflow issue about it:
https://stackoverflow.com/questions/912659/what-is-the-proper-way-to-re-attach-detached-objects-in-hibernate,
make sure you read all of it. Your best bet is to only store the company id
in the session and do a find() before setting the company (I know, it
defeats some of the perceived user benefits of using Hibernate/JPA).

Kalle


On Tue, Nov 28, 2017 at 7:55 PM, Christopher Dodunski <
chrisfromtapes...@christopher.net.nz> wrote:

> Hi team,
>
> I suspect you'll be able to answer this right off the top of your head, as
> this error occurs in response to what I imagine is a very routine code
> assignment: set a newly created user's company to be the same company as
> that of the user doing the creating.
>
>
> Code snippet from 'pages/user/CreateUser.java'...
>
> User user = new User(firstName, lastName, userName, email,
> authenticator.encryptPassword(password));
>
> //Set the new user's company to the same as the creator's company
> user.setCompany(authenticator.getLoggedUser().getCompany());
>
> crudServiceDAO.create(user);
>
>
> The User>>Company relationship, as defined in the User entity class...
>
> @ManyToOne(cascade=CascadeType.ALL)
> @JoinColumn(name="COMPANY_ID")
> private Company company;
>
>
> The Company>>User relationship, as defined in the Company entity class...
>
> @OneToMany(mappedBy="company", cascade=CascadeType.ALL)
> private Collection users = new ArrayList();
>
>
> The USER table has a COMPANY_ID field, so I'd be expecting Hibernate to
> create a new user with the same company ID as the user I have logged in
> as.  Instead, Tapestry gives the following error...
>
> org.apache.tapestry5.runtime.ComponentEventException
>
> detached entity passed to persist: com.example.harbour.entities.
> Company
>
>
> Why is this?  Appreciate your help.
>
> Thanks,
>
> Chris.
>
>
> -
> To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
> For additional commands, e-mail: users-h...@tapestry.apache.org
>
>


"Detached entity passed to persist"

2017-11-28 Thread Christopher Dodunski
Hi team,

I suspect you'll be able to answer this right off the top of your head, as
this error occurs in response to what I imagine is a very routine code
assignment: set a newly created user's company to be the same company as
that of the user doing the creating.


Code snippet from 'pages/user/CreateUser.java'...

User user = new User(firstName, lastName, userName, email,
authenticator.encryptPassword(password));

//Set the new user's company to the same as the creator's company
user.setCompany(authenticator.getLoggedUser().getCompany());

crudServiceDAO.create(user);


The User>>Company relationship, as defined in the User entity class...

@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="COMPANY_ID")
private Company company;


The Company>>User relationship, as defined in the Company entity class...

@OneToMany(mappedBy="company", cascade=CascadeType.ALL)
private Collection users = new ArrayList();


The USER table has a COMPANY_ID field, so I'd be expecting Hibernate to
create a new user with the same company ID as the user I have logged in
as.  Instead, Tapestry gives the following error...

org.apache.tapestry5.runtime.ComponentEventException

detached entity passed to persist: com.example.harbour.entities.Company


Why is this?  Appreciate your help.

Thanks,

Chris.


-
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org