You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by leesy <Le...@openobjects.com> on 2013/08/16 12:10:24 UTC
Java EE, transactions & lazy loading
I'm having trouble getting my head around Java EE, transactions & lazy
loading & I was wondering if anyone could be kind enough to help me get
things straight in my head. It's something I've never quite got right.
I'll try & keep the code samples in this email short, but I'll add links to
fuller examples at the end. Sorry if this was better on say the OpenJPA
Users list.
I've got a piece of code that retrieves a `User` from the Database, performs
some validation on it & then attempts to create another DB object called
`StoredCookie` if that validation passes. So imagine something like:
Now I'm having two separate troubles depending how I do things.
The first issue is that the User object u is being detached after the call
to findByEmail. The customer property of user is defined using a FetchTYpe
of LAZY & I believe this combined with my use of
TransactionAttributeType.REQUIRED at class level on my DAO's, is what is
causing the entity to come back detached. So I've got over this hurdle by
adding a TransactionAttributeType of NEVER to any find methods. This seems
to leave the user object detached. Although this is what I want, I don't
think I'm doing things right as it's probably causing issue #2.
The second issue comes about when I call storedCookieDAO.persist(...). It
gives me a message saying that "The given instance
"com.oobjects.sso.model.User-252" is not managed by this context". The user
is still attached to something, as I can fetch lazily fetched properties.
But as the UserDAO & StoredCookieDAO shared an entity manager (checked the
hash codes to make sure they are the same), I'm a little confused as to what
they are attached to.
Does anyone know what I'd need to do allow my StoredCookie to be persisted?
What is the User object attached to that's preventing it being persisted by
another DAO using the same entity manager?
Here are a few code samples of how I am attempting various bits:
https://gist.github.com/LTheobald/6248694
<https://gist.github.com/LTheobald/6248694>
Thanks for any help anyone can give,
Lee
--
View this message in context: http://openejb.979440.n4.nabble.com/Java-EE-transactions-lazy-loading-tp4664670.html
Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: Java EE, transactions & lazy loading
Posted by Lee Theobald <le...@openobjects.com>.
Seems like the email coming from Nabble messed up the small code
sample. That missing code is:
User u = userDAO.findByEmail("someone@somewhere.com");
if (u.getCustomer() == null) {
// Throw validation error
}
StoredCookie cookie = new StoredCookie(u);
cookie = storedCookieDAO.persist(cookie);
(It can also be seen on Nabble:
http://openejb.979440.n4.nabble.com/Java-EE-transactions-amp-lazy-loading-td4664670.html)
Regards,
Lee
leesy wrote:
> I'm having trouble getting my head around Java EE, transactions& lazy
> loading& I was wondering if anyone could be kind enough to help me get
> things straight in my head. It's something I've never quite got right.
> I'll try& keep the code samples in this email short, but I'll add links to
> fuller examples at the end. Sorry if this was better on say the OpenJPA
> Users list.
>
> I've got a piece of code that retrieves a `User` from the Database, performs
> some validation on it& then attempts to create another DB object called
> `StoredCookie` if that validation passes. So imagine something like:
>
>
>
> Now I'm having two separate troubles depending how I do things.
>
> The first issue is that the User object u is being detached after the call
> to findByEmail. The customer property of user is defined using a FetchTYpe
> of LAZY& I believe this combined with my use of
> TransactionAttributeType.REQUIRED at class level on my DAO's, is what is
> causing the entity to come back detached. So I've got over this hurdle by
> adding a TransactionAttributeType of NEVER to any find methods. This seems
> to leave the user object detached. Although this is what I want, I don't
> think I'm doing things right as it's probably causing issue #2.
>
> The second issue comes about when I call storedCookieDAO.persist(...). It
> gives me a message saying that "The given instance
> "com.oobjects.sso.model.User-252" is not managed by this context". The user
> is still attached to something, as I can fetch lazily fetched properties.
> But as the UserDAO& StoredCookieDAO shared an entity manager (checked the
> hash codes to make sure they are the same), I'm a little confused as to what
> they are attached to.
>
> Does anyone know what I'd need to do allow my StoredCookie to be persisted?
> What is the User object attached to that's preventing it being persisted by
> another DAO using the same entity manager?
>
> Here are a few code samples of how I am attempting various bits:
> https://gist.github.com/LTheobald/6248694
> <https://gist.github.com/LTheobald/6248694>
>
> Thanks for any help anyone can give,
> Lee
>
>
>
> --
> View this message in context: http://openejb.979440.n4.nabble.com/Java-EE-transactions-lazy-loading-tp4664670.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>
Re: Java EE, transactions & lazy loading
Posted by "Howard W. Smith, Jr." <sm...@gmail.com>.
Also, I like to search google for something like the following:
stackoverflow jpa lazy transactionattribute
and I found the following:
http://stackoverflow.com/questions/8056688/can-lazy-fetched-field-be-modified-before-loaded
you can click-and-read related links on stackoverflow and on google. :)
On Fri, Aug 16, 2013 at 6:26 AM, Howard W. Smith, Jr. <
smithh032772@gmail.com> wrote:
> Lee, I will let someone else answer your questions, but your test case
> reminded me of a BalusC (JSF guru) blog[1] that I visited many months ago.
> It does not answer your question, but it is an alternate solution for what
> you are already attempting to do...I think. :)
>
> [1] http://balusc.blogspot.com/2007/03/user-session-filter.html
>
>
> On Fri, Aug 16, 2013 at 6:10 AM, leesy <Le...@openobjects.com> wrote:
>
>> I'm having trouble getting my head around Java EE, transactions & lazy
>> loading & I was wondering if anyone could be kind enough to help me get
>> things straight in my head. It's something I've never quite got right.
>> I'll try & keep the code samples in this email short, but I'll add links
>> to
>> fuller examples at the end. Sorry if this was better on say the OpenJPA
>> Users list.
>>
>> I've got a piece of code that retrieves a `User` from the Database,
>> performs
>> some validation on it & then attempts to create another DB object called
>> `StoredCookie` if that validation passes. So imagine something like:
>>
>>
>>
>> Now I'm having two separate troubles depending how I do things.
>>
>> The first issue is that the User object u is being detached after the call
>> to findByEmail. The customer property of user is defined using a
>> FetchTYpe
>> of LAZY & I believe this combined with my use of
>> TransactionAttributeType.REQUIRED at class level on my DAO's, is what is
>> causing the entity to come back detached. So I've got over this hurdle by
>> adding a TransactionAttributeType of NEVER to any find methods. This
>> seems
>> to leave the user object detached. Although this is what I want, I don't
>> think I'm doing things right as it's probably causing issue #2.
>>
>> The second issue comes about when I call storedCookieDAO.persist(...). It
>> gives me a message saying that "The given instance
>> "com.oobjects.sso.model.User-252" is not managed by this context". The
>> user
>> is still attached to something, as I can fetch lazily fetched properties.
>> But as the UserDAO & StoredCookieDAO shared an entity manager (checked the
>> hash codes to make sure they are the same), I'm a little confused as to
>> what
>> they are attached to.
>>
>> Does anyone know what I'd need to do allow my StoredCookie to be
>> persisted?
>> What is the User object attached to that's preventing it being persisted
>> by
>> another DAO using the same entity manager?
>>
>> Here are a few code samples of how I am attempting various bits:
>> https://gist.github.com/LTheobald/6248694
>> <https://gist.github.com/LTheobald/6248694>
>>
>> Thanks for any help anyone can give,
>> Lee
>>
>>
>>
>> --
>> View this message in context:
>> http://openejb.979440.n4.nabble.com/Java-EE-transactions-lazy-loading-tp4664670.html
>> Sent from the OpenEJB User mailing list archive at Nabble.com.
>>
>
>
Re: Java EE, transactions & lazy loading
Posted by Lee Theobald <le...@openobjects.com>.
Thanks for the help Romain. I tried the transaction attribute on
StoredCookieDAO#persist but that gave me the same exception:
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public StoredCookie persist(StoredCookie entity) {
return super.persist(entity);
}
I'll take a look at turning my LoginAction bean into an EJB. This is
where it becomes even more shakey ground so wish me luck!
Lee,
Romain Manni-Bucau wrote:
> Hi,
>
> use an ejb as LoginAction or set a transaction attribute on
> StoredCookie#persist
> (redefine it in StoredCookie)
>
> *Romain Manni-Bucau*
> *Twitter: @rmannibucau<https://twitter.com/rmannibucau>*
> *Blog: **http://rmannibucau.wordpress.com/*<http://rmannibucau.wordpress.com/>
> *LinkedIn: **http://fr.linkedin.com/in/rmannibucau*
> *Github: https://github.com/rmannibucau*
>
>
>
> 2013/8/16 Howard W. Smith, Jr.<sm...@gmail.com>
>
>> Lee, I will let someone else answer your questions, but your test case
>> reminded me of a BalusC (JSF guru) blog[1] that I visited many months ago.
>> It does not answer your question, but it is an alternate solution for what
>> you are already attempting to do...I think. :)
>>
>> [1] http://balusc.blogspot.com/2007/03/user-session-filter.html
>>
>>
>> On Fri, Aug 16, 2013 at 6:10 AM, leesy<Le...@openobjects.com> wrote:
>>
>>> I'm having trouble getting my head around Java EE, transactions& lazy
>>> loading& I was wondering if anyone could be kind enough to help me get
>>> things straight in my head. It's something I've never quite got right.
>>> I'll try& keep the code samples in this email short, but I'll add links
>> to
>>> fuller examples at the end. Sorry if this was better on say the OpenJPA
>>> Users list.
>>>
>>> I've got a piece of code that retrieves a `User` from the Database,
>>> performs
>>> some validation on it& then attempts to create another DB object called
>>> `StoredCookie` if that validation passes. So imagine something like:
>>>
>>>
>>>
>>> Now I'm having two separate troubles depending how I do things.
>>>
>>> The first issue is that the User object u is being detached after the
>> call
>>> to findByEmail. The customer property of user is defined using a
>> FetchTYpe
>>> of LAZY& I believe this combined with my use of
>>> TransactionAttributeType.REQUIRED at class level on my DAO's, is what is
>>> causing the entity to come back detached. So I've got over this hurdle
>> by
>>> adding a TransactionAttributeType of NEVER to any find methods. This
>> seems
>>> to leave the user object detached. Although this is what I want, I don't
>>> think I'm doing things right as it's probably causing issue #2.
>>>
>>> The second issue comes about when I call storedCookieDAO.persist(...).
>> It
>>> gives me a message saying that "The given instance
>>> "com.oobjects.sso.model.User-252" is not managed by this context". The
>>> user
>>> is still attached to something, as I can fetch lazily fetched properties.
>>> But as the UserDAO& StoredCookieDAO shared an entity manager (checked
>> the
>>> hash codes to make sure they are the same), I'm a little confused as to
>>> what
>>> they are attached to.
>>>
>>> Does anyone know what I'd need to do allow my StoredCookie to be
>> persisted?
>>> What is the User object attached to that's preventing it being persisted
>> by
>>> another DAO using the same entity manager?
>>>
>>> Here are a few code samples of how I am attempting various bits:
>>> https://gist.github.com/LTheobald/6248694
>>> <https://gist.github.com/LTheobald/6248694>
>>>
>>> Thanks for any help anyone can give,
>>> Lee
>>>
>>>
>>>
>>> --
>>> View this message in context:
>>>
>> http://openejb.979440.n4.nabble.com/Java-EE-transactions-lazy-loading-tp4664670.html
>>> Sent from the OpenEJB User mailing list archive at Nabble.com.
>>>
>
Re: Java EE, transactions & lazy loading
Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi,
use an ejb as LoginAction or set a transaction attribute on
StoredCookie#persist
(redefine it in StoredCookie)
*Romain Manni-Bucau*
*Twitter: @rmannibucau <https://twitter.com/rmannibucau>*
*Blog: **http://rmannibucau.wordpress.com/*<http://rmannibucau.wordpress.com/>
*LinkedIn: **http://fr.linkedin.com/in/rmannibucau*
*Github: https://github.com/rmannibucau*
2013/8/16 Howard W. Smith, Jr. <sm...@gmail.com>
> Lee, I will let someone else answer your questions, but your test case
> reminded me of a BalusC (JSF guru) blog[1] that I visited many months ago.
> It does not answer your question, but it is an alternate solution for what
> you are already attempting to do...I think. :)
>
> [1] http://balusc.blogspot.com/2007/03/user-session-filter.html
>
>
> On Fri, Aug 16, 2013 at 6:10 AM, leesy <Le...@openobjects.com> wrote:
>
> > I'm having trouble getting my head around Java EE, transactions & lazy
> > loading & I was wondering if anyone could be kind enough to help me get
> > things straight in my head. It's something I've never quite got right.
> > I'll try & keep the code samples in this email short, but I'll add links
> to
> > fuller examples at the end. Sorry if this was better on say the OpenJPA
> > Users list.
> >
> > I've got a piece of code that retrieves a `User` from the Database,
> > performs
> > some validation on it & then attempts to create another DB object called
> > `StoredCookie` if that validation passes. So imagine something like:
> >
> >
> >
> > Now I'm having two separate troubles depending how I do things.
> >
> > The first issue is that the User object u is being detached after the
> call
> > to findByEmail. The customer property of user is defined using a
> FetchTYpe
> > of LAZY & I believe this combined with my use of
> > TransactionAttributeType.REQUIRED at class level on my DAO's, is what is
> > causing the entity to come back detached. So I've got over this hurdle
> by
> > adding a TransactionAttributeType of NEVER to any find methods. This
> seems
> > to leave the user object detached. Although this is what I want, I don't
> > think I'm doing things right as it's probably causing issue #2.
> >
> > The second issue comes about when I call storedCookieDAO.persist(...).
> It
> > gives me a message saying that "The given instance
> > "com.oobjects.sso.model.User-252" is not managed by this context". The
> > user
> > is still attached to something, as I can fetch lazily fetched properties.
> > But as the UserDAO & StoredCookieDAO shared an entity manager (checked
> the
> > hash codes to make sure they are the same), I'm a little confused as to
> > what
> > they are attached to.
> >
> > Does anyone know what I'd need to do allow my StoredCookie to be
> persisted?
> > What is the User object attached to that's preventing it being persisted
> by
> > another DAO using the same entity manager?
> >
> > Here are a few code samples of how I am attempting various bits:
> > https://gist.github.com/LTheobald/6248694
> > <https://gist.github.com/LTheobald/6248694>
> >
> > Thanks for any help anyone can give,
> > Lee
> >
> >
> >
> > --
> > View this message in context:
> >
> http://openejb.979440.n4.nabble.com/Java-EE-transactions-lazy-loading-tp4664670.html
> > Sent from the OpenEJB User mailing list archive at Nabble.com.
> >
>
Re: Java EE, transactions & lazy loading
Posted by Lee Theobald <le...@openobjects.com>.
Good ol' BalusC - that man has helped out a lot over the years.
Thanks for the tip Howard. I have previously done something similar
with a request filter that would open/setup & clear down an entity
manager & transaction for me. I certainly am thinking I could go down
that route again but I'd obviously like to do things the best way. I've
got a JavaEE book that I'm trying to find answers in but I find it's
often easier & more informative just to ask real people :)
Regards,
Lee
Howard W. Smith, Jr. wrote:
> Lee, I will let someone else answer your questions, but your test case
> reminded me of a BalusC (JSF guru) blog[1] that I visited many months ago.
> It does not answer your question, but it is an alternate solution for what
> you are already attempting to do...I think. :)
>
> [1] http://balusc.blogspot.com/2007/03/user-session-filter.html
>
>
> On Fri, Aug 16, 2013 at 6:10 AM, leesy<Le...@openobjects.com> wrote:
>
>> I'm having trouble getting my head around Java EE, transactions& lazy
>> loading& I was wondering if anyone could be kind enough to help me get
>> things straight in my head. It's something I've never quite got right.
>> I'll try& keep the code samples in this email short, but I'll add links to
>> fuller examples at the end. Sorry if this was better on say the OpenJPA
>> Users list.
>>
>> I've got a piece of code that retrieves a `User` from the Database,
>> performs
>> some validation on it& then attempts to create another DB object called
>> `StoredCookie` if that validation passes. So imagine something like:
>>
>>
>>
>> Now I'm having two separate troubles depending how I do things.
>>
>> The first issue is that the User object u is being detached after the call
>> to findByEmail. The customer property of user is defined using a FetchTYpe
>> of LAZY& I believe this combined with my use of
>> TransactionAttributeType.REQUIRED at class level on my DAO's, is what is
>> causing the entity to come back detached. So I've got over this hurdle by
>> adding a TransactionAttributeType of NEVER to any find methods. This seems
>> to leave the user object detached. Although this is what I want, I don't
>> think I'm doing things right as it's probably causing issue #2.
>>
>> The second issue comes about when I call storedCookieDAO.persist(...). It
>> gives me a message saying that "The given instance
>> "com.oobjects.sso.model.User-252" is not managed by this context". The
>> user
>> is still attached to something, as I can fetch lazily fetched properties.
>> But as the UserDAO& StoredCookieDAO shared an entity manager (checked the
>> hash codes to make sure they are the same), I'm a little confused as to
>> what
>> they are attached to.
>>
>> Does anyone know what I'd need to do allow my StoredCookie to be persisted?
>> What is the User object attached to that's preventing it being persisted by
>> another DAO using the same entity manager?
>>
>> Here are a few code samples of how I am attempting various bits:
>> https://gist.github.com/LTheobald/6248694
>> <https://gist.github.com/LTheobald/6248694>
>>
>> Thanks for any help anyone can give,
>> Lee
>>
>>
>>
>> --
>> View this message in context:
>> http://openejb.979440.n4.nabble.com/Java-EE-transactions-lazy-loading-tp4664670.html
>> Sent from the OpenEJB User mailing list archive at Nabble.com.
>>
>
Re: Java EE, transactions & lazy loading
Posted by "Howard W. Smith, Jr." <sm...@gmail.com>.
Lee, I will let someone else answer your questions, but your test case
reminded me of a BalusC (JSF guru) blog[1] that I visited many months ago.
It does not answer your question, but it is an alternate solution for what
you are already attempting to do...I think. :)
[1] http://balusc.blogspot.com/2007/03/user-session-filter.html
On Fri, Aug 16, 2013 at 6:10 AM, leesy <Le...@openobjects.com> wrote:
> I'm having trouble getting my head around Java EE, transactions & lazy
> loading & I was wondering if anyone could be kind enough to help me get
> things straight in my head. It's something I've never quite got right.
> I'll try & keep the code samples in this email short, but I'll add links to
> fuller examples at the end. Sorry if this was better on say the OpenJPA
> Users list.
>
> I've got a piece of code that retrieves a `User` from the Database,
> performs
> some validation on it & then attempts to create another DB object called
> `StoredCookie` if that validation passes. So imagine something like:
>
>
>
> Now I'm having two separate troubles depending how I do things.
>
> The first issue is that the User object u is being detached after the call
> to findByEmail. The customer property of user is defined using a FetchTYpe
> of LAZY & I believe this combined with my use of
> TransactionAttributeType.REQUIRED at class level on my DAO's, is what is
> causing the entity to come back detached. So I've got over this hurdle by
> adding a TransactionAttributeType of NEVER to any find methods. This seems
> to leave the user object detached. Although this is what I want, I don't
> think I'm doing things right as it's probably causing issue #2.
>
> The second issue comes about when I call storedCookieDAO.persist(...). It
> gives me a message saying that "The given instance
> "com.oobjects.sso.model.User-252" is not managed by this context". The
> user
> is still attached to something, as I can fetch lazily fetched properties.
> But as the UserDAO & StoredCookieDAO shared an entity manager (checked the
> hash codes to make sure they are the same), I'm a little confused as to
> what
> they are attached to.
>
> Does anyone know what I'd need to do allow my StoredCookie to be persisted?
> What is the User object attached to that's preventing it being persisted by
> another DAO using the same entity manager?
>
> Here are a few code samples of how I am attempting various bits:
> https://gist.github.com/LTheobald/6248694
> <https://gist.github.com/LTheobald/6248694>
>
> Thanks for any help anyone can give,
> Lee
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/Java-EE-transactions-lazy-loading-tp4664670.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>