You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by José Luis Cetina <ma...@gmail.com> on 2013/03/15 01:56:36 UTC

OpenJPA "strange behaviour with EJB+OpenJPA+JPQL/Criteria in TomEE"

Hi i discuss this in openJPA mailing lists after many mail Kevin Sutter
told me that i post this to the TomEE mailing lists.

PLEASE TRY TO READ ALL THE ENTIRE EMAIL.

I dont want to copy and paste all the emails. I will Resume what is happen
and you will read 2 last emails from openjpa mailing lists.

Scenario:

1. The webpage (jsf) call a method to an EJB, for get an existent entity
from database.
2. In EJB i get a (MyObject reference) , i get it using a query (jpql or
criterias).
3. The ejb return one entity of course at this point the entity are
"sended" by the ejb to the managedbean (then the entities are out of the
persistence context  therefore the are dettached)
4. Then in the managedbean in X method i do the follow:
     - Get the dettached entity. (MyObject)
     - Change some attribute let say the "DESCRIPTION" attribute
     - The managedbean now CALL and EJB method for do an update of this
entity.
5. The EJB method receive 1 MyObject ( public void update(MyObject obj) )
6. Then the EJB method need to call an update because the "DESCRIPTION"
property has changed
7. The method is like this:

   public void update(MyObject obj){
        begin transaction...
        em.merge(obj);
        commit transaction...
   }

Then i get this exception

<openjpa-2.2.0-r422266:1244990 nonfatal user error>
org.apache.openjpa.persistence.InvalidStateException: Primary key field
com.test.MyObject.id of com.test.MyObject@198e0705 has non-default value.
The instance life cycle is in PNewState state and hence an existing
non-default value for the identity field is not permitted. You either need
to remove the @GeneratedValue annotation or modify the code to remove the
initializer processing.

This only happens when i try to merge an entity that i got from ejb using
criterias or jpql, if i use the find method of the entity manager i dont
have any issue (but of course i cant use only em.find method).


What happen when i use the criterias why this works if i use em.find???


****HERE ARE THE LAST 2 EMAILS THAT I SET TO OPENJPA MAILING LISTS***

******My mail:

I could reproduce this problem easy in this project:
https://github.com/maxtorzito/openjpa.git.

You will see 2 files and 1 folder. The readme contains the
same instructions that you will see here below, the file ddl-dml-db
contains the required sql for create database/table/inserts, and the folder
contains the maven project for run it.

FOR RUN THIS PROJECT, JUST DO THIS STEPS IN pom.xml project

1) Create the database/users table/inserts using the ddl-dml-db file for
this. (MYSQL DATABASE)
2) mvn -DskipTests=true clean install (this will compile,enhance and
generate metamodel)
3) mvn tomee:run  (the server will start and you can see INFO - Server
startup in XXXX ms)
4) Acess the page using the browser and type
http://localhost:8080/jpatest-1.0-SNAPSHOT/index.xhtml
5) You will see 3 buttons, the first 2 buttons will fail, the third button
will work as expected.


Running the plugin will setup easily an Apache TomEE server with all
the necessary for run this "demo" (it will drop the mysql jar to lib folder
too)

**********The database parameter for connection are under
jpatest/src/main/tomee/conf/tomee.xml you will see this at the bottom. You
can change the username/password

<Resource id="ExampleDataSource" type="DataSource">
  JdbcDriver com.mysql.jdbc.Driver
  JdbcUrl
jdbc:mysql://localhost:3306/jpa_test?zeroDateTimeBehavior=convertToNull&amp;rewriteBatchedStatements=true
  UserName developer
  Password developer
  JtaManaged true
</Resource>

The expected result is the first record of the table will update the
description from "USER ONE" to "NEW NICKNAME USING FIND", this only happend
when you click in the THIRD BUTTON.
Im not sure why the first 2 actions buttons failed, the only difference
between the first 2 method's its that they use criterias and jpql for
retrive the entity and the third method use the find entity manager method.


It is a maven project using apache tomee as "application server", it has a
plugin for run the project easily.

Please let me know if you have some troubles for reproduce it as well if
you could reproduce it.


Regards.

******* END OF MY MAIL


*******  Kevin Sutter's Mail:

Jose -

I don't have any solid answers for you, but I can tell you what I've found.
It looks like the reason that the criteria/JPQL methods fail is because the
Entities that are returned are still a part of the persistence context that
loaded them. So, you're trying to merge non-transactional Entities that are
a part of one context into another. I have no clue why criteria/JPQL behave
differently than find though?!

I found that if I changed the @TransactionManagment attribute from BEAN to
CONTAINER, everything worked fine... you could also call em.clear() after
each of your bean methods. The net of it seems like the criteria/JPQL bean
methods aren't cleaning up as OpenJPA is expecting.

It might make sense to cross post this to a tomee list to see if they could
shed some more light on what is going on.

Thanks,
Rick


****END OF Kevin Sutter's Mail


This could be some TomEE issue using openJPA???
I didn't test this in other application server.


Thanks.
Sorry for this large email.

Re: OpenJPA "strange behaviour with EJB+OpenJPA+JPQL/Criteria in TomEE"

Posted by José Luis Cetina <ma...@gmail.com>.
Thanks. Its working now


2013/3/15 Romain Manni-Bucau <rm...@gmail.com>

> here was the issue: https://issues.apache.org/jira/browse/TOMEE-798
>
> *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/3/15 Romain Manni-Bucau <rm...@gmail.com>
>
> > think more or less know,
> >
> > in the mode you use it (hybrid between full JTA with EJB CMT and
> > RESOURCE_LOCAL) you have to clear the entitymanager with the end of the
> > method.
> >
> >  *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/3/15 Romain Manni-Bucau <rm...@gmail.com>
> >
> >> Hi,
> >>
> >> OpenJPA doesn't register any Synchronization (the way to do the work at
> >> commit time) when you use UserTransaction this way
> >> (ejbContext.getUserTransaction().begin();). So all tx work is not really
> >> done at commit time.
> >>
> >> basically calling ((EntityManagerImpl)
> >> em).setSyncWithManagedTransactions(true); before persist will do the
> trick
> >> but will import openjpa. To be able to get it automatically em needs to
> be
> >> created after the transaction started.
> >>
> >> After OpenJPA is not able to identify the id as an OpenJPA OID (because
> >> that's simply an int), not sure what it can be here.
> >>
> >> not really sure what's the issue ATM
> >>
> >> *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/3/15 José Luis Cetina <ma...@gmail.com>
> >>
> >>> I wrote "Kevin Sutter's Mail", this mail was written by Rick Curtis not
> >>> for
> >>> Kevin Sutter (sorry for that) both of them were helping me with this.
> >>>
> >>>
> >>> 2013/3/14 José Luis Cetina <ma...@gmail.com>
> >>>
> >>> >
> >>> > Hi i discuss this in openJPA mailing lists after many mail Kevin
> Sutter
> >>> > told me that i post this to the TomEE mailing lists.
> >>> >
> >>> > PLEASE TRY TO READ ALL THE ENTIRE EMAIL.
> >>> >
> >>> > I dont want to copy and paste all the emails. I will Resume what is
> >>> happen
> >>> > and you will read 2 last emails from openjpa mailing lists.
> >>> >
> >>> > Scenario:
> >>> >
> >>> > 1. The webpage (jsf) call a method to an EJB, for get an existent
> >>> entity
> >>> > from database.
> >>> > 2. In EJB i get a (MyObject reference) , i get it using a query (jpql
> >>> or
> >>> > criterias).
> >>> > 3. The ejb return one entity of course at this point the entity are
> >>> > "sended" by the ejb to the managedbean (then the entities are out of
> >>> the
> >>> > persistence context  therefore the are dettached)
> >>> > 4. Then in the managedbean in X method i do the follow:
> >>> >      - Get the dettached entity. (MyObject)
> >>> >      - Change some attribute let say the "DESCRIPTION" attribute
> >>> >      - The managedbean now CALL and EJB method for do an update of
> this
> >>> > entity.
> >>> > 5. The EJB method receive 1 MyObject ( public void update(MyObject
> >>> obj) )
> >>> > 6. Then the EJB method need to call an update because the
> "DESCRIPTION"
> >>> > property has changed
> >>> > 7. The method is like this:
> >>> >
> >>> >    public void update(MyObject obj){
> >>> >         begin transaction...
> >>> >         em.merge(obj);
> >>> >         commit transaction...
> >>> >    }
> >>> >
> >>> > Then i get this exception
> >>> >
> >>> > <openjpa-2.2.0-r422266:1244990 nonfatal user error>
> >>> > org.apache.openjpa.persistence.InvalidStateException: Primary key
> field
> >>> > com.test.MyObject.id of com.test.MyObject@198e0705 has non-default
> >>> value.
> >>> > The instance life cycle is in PNewState state and hence an existing
> >>> > non-default value for the identity field is not permitted. You either
> >>> need
> >>> > to remove the @GeneratedValue annotation or modify the code to remove
> >>> the
> >>> > initializer processing.
> >>> >
> >>> > This only happens when i try to merge an entity that i got from ejb
> >>> using
> >>> > criterias or jpql, if i use the find method of the entity manager i
> >>> dont
> >>> > have any issue (but of course i cant use only em.find method).
> >>> >
> >>> >
> >>> > What happen when i use the criterias why this works if i use
> em.find???
> >>> >
> >>> >
> >>> > ****HERE ARE THE LAST 2 EMAILS THAT I SET TO OPENJPA MAILING LISTS***
> >>> >
> >>> > ******My mail:
> >>> >
> >>> > I could reproduce this problem easy in this project:
> >>> > https://github.com/maxtorzito/openjpa.git.
> >>> >
> >>> > You will see 2 files and 1 folder. The readme contains the
> >>> > same instructions that you will see here below, the file ddl-dml-db
> >>> > contains the required sql for create database/table/inserts, and the
> >>> folder
> >>> > contains the maven project for run it.
> >>> >
> >>> > FOR RUN THIS PROJECT, JUST DO THIS STEPS IN pom.xml project
> >>> >
> >>> > 1) Create the database/users table/inserts using the ddl-dml-db file
> >>> for
> >>> > this. (MYSQL DATABASE)
> >>> > 2) mvn -DskipTests=true clean install (this will compile,enhance and
> >>> > generate metamodel)
> >>> > 3) mvn tomee:run  (the server will start and you can see INFO -
> Server
> >>> > startup in XXXX ms)
> >>> > 4) Acess the page using the browser and type
> >>> > http://localhost:8080/jpatest-1.0-SNAPSHOT/index.xhtml
> >>> > 5) You will see 3 buttons, the first 2 buttons will fail, the third
> >>> button
> >>> > will work as expected.
> >>> >
> >>> >
> >>> > Running the plugin will setup easily an Apache TomEE server with all
> >>> > the necessary for run this "demo" (it will drop the mysql jar to lib
> >>> folder
> >>> > too)
> >>> >
> >>> > **********The database parameter for connection are under
> >>> > jpatest/src/main/tomee/conf/tomee.xml you will see this at the
> bottom.
> >>> You
> >>> > can change the username/password
> >>> >
> >>> > <Resource id="ExampleDataSource" type="DataSource">
> >>> >   JdbcDriver com.mysql.jdbc.Driver
> >>> >   JdbcUrl
> >>> >
> >>>
> jdbc:mysql://localhost:3306/jpa_test?zeroDateTimeBehavior=convertToNull&amp;rewriteBatchedStatements=true
> >>> >   UserName developer
> >>> >   Password developer
> >>> >   JtaManaged true
> >>> > </Resource>
> >>> >
> >>> > The expected result is the first record of the table will update the
> >>> > description from "USER ONE" to "NEW NICKNAME USING FIND", this only
> >>> happend
> >>> > when you click in the THIRD BUTTON.
> >>> > Im not sure why the first 2 actions buttons failed, the only
> difference
> >>> > between the first 2 method's its that they use criterias and jpql for
> >>> > retrive the entity and the third method use the find entity manager
> >>> method.
> >>> >
> >>> >
> >>> > It is a maven project using apache tomee as "application server", it
> >>> has a
> >>> > plugin for run the project easily.
> >>> >
> >>> > Please let me know if you have some troubles for reproduce it as well
> >>> if
> >>> > you could reproduce it.
> >>> >
> >>> >
> >>> > Regards.
> >>> >
> >>> > ******* END OF MY MAIL
> >>> >
> >>> >
> >>> > *******  Kevin Sutter's Mail:
> >>> >
> >>> > Jose -
> >>> >
> >>> > I don't have any solid answers for you, but I can tell you what I've
> >>> found.
> >>> > It looks like the reason that the criteria/JPQL methods fail is
> >>> because the
> >>> > Entities that are returned are still a part of the persistence
> context
> >>> that
> >>> > loaded them. So, you're trying to merge non-transactional Entities
> >>> that are
> >>> > a part of one context into another. I have no clue why criteria/JPQL
> >>> behave
> >>> > differently than find though?!
> >>> >
> >>> > I found that if I changed the @TransactionManagment attribute from
> >>> BEAN to
> >>> > CONTAINER, everything worked fine... you could also call em.clear()
> >>> after
> >>> > each of your bean methods. The net of it seems like the criteria/JPQL
> >>> bean
> >>> > methods aren't cleaning up as OpenJPA is expecting.
> >>> >
> >>> > It might make sense to cross post this to a tomee list to see if they
> >>> could
> >>> > shed some more light on what is going on.
> >>> >
> >>> > Thanks,
> >>> > Rick
> >>> >
> >>> >
> >>> > ****END OF Kevin Sutter's Mail
> >>> >
> >>> >
> >>> > This could be some TomEE issue using openJPA???
> >>> > I didn't test this in other application server.
> >>> >
> >>> >
> >>> > Thanks.
> >>> > Sorry for this large email.
> >>> >
> >>>
> >>>
> >>>
> >>> --
> >>> -------------------------------------------------------------------
> >>> *SCJA. José Luis Cetina*
> >>> -------------------------------------------------------------------
> >>>
> >>
> >>
> >
>



-- 
-------------------------------------------------------------------
*SCJA. José Luis Cetina*
-------------------------------------------------------------------

Re: OpenJPA "strange behaviour with EJB+OpenJPA+JPQL/Criteria in TomEE"

Posted by Romain Manni-Bucau <rm...@gmail.com>.
here was the issue: https://issues.apache.org/jira/browse/TOMEE-798

*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/3/15 Romain Manni-Bucau <rm...@gmail.com>

> think more or less know,
>
> in the mode you use it (hybrid between full JTA with EJB CMT and
> RESOURCE_LOCAL) you have to clear the entitymanager with the end of the
> method.
>
>  *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/3/15 Romain Manni-Bucau <rm...@gmail.com>
>
>> Hi,
>>
>> OpenJPA doesn't register any Synchronization (the way to do the work at
>> commit time) when you use UserTransaction this way
>> (ejbContext.getUserTransaction().begin();). So all tx work is not really
>> done at commit time.
>>
>> basically calling ((EntityManagerImpl)
>> em).setSyncWithManagedTransactions(true); before persist will do the trick
>> but will import openjpa. To be able to get it automatically em needs to be
>> created after the transaction started.
>>
>> After OpenJPA is not able to identify the id as an OpenJPA OID (because
>> that's simply an int), not sure what it can be here.
>>
>> not really sure what's the issue ATM
>>
>> *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/3/15 José Luis Cetina <ma...@gmail.com>
>>
>>> I wrote "Kevin Sutter's Mail", this mail was written by Rick Curtis not
>>> for
>>> Kevin Sutter (sorry for that) both of them were helping me with this.
>>>
>>>
>>> 2013/3/14 José Luis Cetina <ma...@gmail.com>
>>>
>>> >
>>> > Hi i discuss this in openJPA mailing lists after many mail Kevin Sutter
>>> > told me that i post this to the TomEE mailing lists.
>>> >
>>> > PLEASE TRY TO READ ALL THE ENTIRE EMAIL.
>>> >
>>> > I dont want to copy and paste all the emails. I will Resume what is
>>> happen
>>> > and you will read 2 last emails from openjpa mailing lists.
>>> >
>>> > Scenario:
>>> >
>>> > 1. The webpage (jsf) call a method to an EJB, for get an existent
>>> entity
>>> > from database.
>>> > 2. In EJB i get a (MyObject reference) , i get it using a query (jpql
>>> or
>>> > criterias).
>>> > 3. The ejb return one entity of course at this point the entity are
>>> > "sended" by the ejb to the managedbean (then the entities are out of
>>> the
>>> > persistence context  therefore the are dettached)
>>> > 4. Then in the managedbean in X method i do the follow:
>>> >      - Get the dettached entity. (MyObject)
>>> >      - Change some attribute let say the "DESCRIPTION" attribute
>>> >      - The managedbean now CALL and EJB method for do an update of this
>>> > entity.
>>> > 5. The EJB method receive 1 MyObject ( public void update(MyObject
>>> obj) )
>>> > 6. Then the EJB method need to call an update because the "DESCRIPTION"
>>> > property has changed
>>> > 7. The method is like this:
>>> >
>>> >    public void update(MyObject obj){
>>> >         begin transaction...
>>> >         em.merge(obj);
>>> >         commit transaction...
>>> >    }
>>> >
>>> > Then i get this exception
>>> >
>>> > <openjpa-2.2.0-r422266:1244990 nonfatal user error>
>>> > org.apache.openjpa.persistence.InvalidStateException: Primary key field
>>> > com.test.MyObject.id of com.test.MyObject@198e0705 has non-default
>>> value.
>>> > The instance life cycle is in PNewState state and hence an existing
>>> > non-default value for the identity field is not permitted. You either
>>> need
>>> > to remove the @GeneratedValue annotation or modify the code to remove
>>> the
>>> > initializer processing.
>>> >
>>> > This only happens when i try to merge an entity that i got from ejb
>>> using
>>> > criterias or jpql, if i use the find method of the entity manager i
>>> dont
>>> > have any issue (but of course i cant use only em.find method).
>>> >
>>> >
>>> > What happen when i use the criterias why this works if i use em.find???
>>> >
>>> >
>>> > ****HERE ARE THE LAST 2 EMAILS THAT I SET TO OPENJPA MAILING LISTS***
>>> >
>>> > ******My mail:
>>> >
>>> > I could reproduce this problem easy in this project:
>>> > https://github.com/maxtorzito/openjpa.git.
>>> >
>>> > You will see 2 files and 1 folder. The readme contains the
>>> > same instructions that you will see here below, the file ddl-dml-db
>>> > contains the required sql for create database/table/inserts, and the
>>> folder
>>> > contains the maven project for run it.
>>> >
>>> > FOR RUN THIS PROJECT, JUST DO THIS STEPS IN pom.xml project
>>> >
>>> > 1) Create the database/users table/inserts using the ddl-dml-db file
>>> for
>>> > this. (MYSQL DATABASE)
>>> > 2) mvn -DskipTests=true clean install (this will compile,enhance and
>>> > generate metamodel)
>>> > 3) mvn tomee:run  (the server will start and you can see INFO - Server
>>> > startup in XXXX ms)
>>> > 4) Acess the page using the browser and type
>>> > http://localhost:8080/jpatest-1.0-SNAPSHOT/index.xhtml
>>> > 5) You will see 3 buttons, the first 2 buttons will fail, the third
>>> button
>>> > will work as expected.
>>> >
>>> >
>>> > Running the plugin will setup easily an Apache TomEE server with all
>>> > the necessary for run this "demo" (it will drop the mysql jar to lib
>>> folder
>>> > too)
>>> >
>>> > **********The database parameter for connection are under
>>> > jpatest/src/main/tomee/conf/tomee.xml you will see this at the bottom.
>>> You
>>> > can change the username/password
>>> >
>>> > <Resource id="ExampleDataSource" type="DataSource">
>>> >   JdbcDriver com.mysql.jdbc.Driver
>>> >   JdbcUrl
>>> >
>>> jdbc:mysql://localhost:3306/jpa_test?zeroDateTimeBehavior=convertToNull&amp;rewriteBatchedStatements=true
>>> >   UserName developer
>>> >   Password developer
>>> >   JtaManaged true
>>> > </Resource>
>>> >
>>> > The expected result is the first record of the table will update the
>>> > description from "USER ONE" to "NEW NICKNAME USING FIND", this only
>>> happend
>>> > when you click in the THIRD BUTTON.
>>> > Im not sure why the first 2 actions buttons failed, the only difference
>>> > between the first 2 method's its that they use criterias and jpql for
>>> > retrive the entity and the third method use the find entity manager
>>> method.
>>> >
>>> >
>>> > It is a maven project using apache tomee as "application server", it
>>> has a
>>> > plugin for run the project easily.
>>> >
>>> > Please let me know if you have some troubles for reproduce it as well
>>> if
>>> > you could reproduce it.
>>> >
>>> >
>>> > Regards.
>>> >
>>> > ******* END OF MY MAIL
>>> >
>>> >
>>> > *******  Kevin Sutter's Mail:
>>> >
>>> > Jose -
>>> >
>>> > I don't have any solid answers for you, but I can tell you what I've
>>> found.
>>> > It looks like the reason that the criteria/JPQL methods fail is
>>> because the
>>> > Entities that are returned are still a part of the persistence context
>>> that
>>> > loaded them. So, you're trying to merge non-transactional Entities
>>> that are
>>> > a part of one context into another. I have no clue why criteria/JPQL
>>> behave
>>> > differently than find though?!
>>> >
>>> > I found that if I changed the @TransactionManagment attribute from
>>> BEAN to
>>> > CONTAINER, everything worked fine... you could also call em.clear()
>>> after
>>> > each of your bean methods. The net of it seems like the criteria/JPQL
>>> bean
>>> > methods aren't cleaning up as OpenJPA is expecting.
>>> >
>>> > It might make sense to cross post this to a tomee list to see if they
>>> could
>>> > shed some more light on what is going on.
>>> >
>>> > Thanks,
>>> > Rick
>>> >
>>> >
>>> > ****END OF Kevin Sutter's Mail
>>> >
>>> >
>>> > This could be some TomEE issue using openJPA???
>>> > I didn't test this in other application server.
>>> >
>>> >
>>> > Thanks.
>>> > Sorry for this large email.
>>> >
>>>
>>>
>>>
>>> --
>>> -------------------------------------------------------------------
>>> *SCJA. José Luis Cetina*
>>> -------------------------------------------------------------------
>>>
>>
>>
>

Re: OpenJPA "strange behaviour with EJB+OpenJPA+JPQL/Criteria in TomEE"

Posted by Romain Manni-Bucau <rm...@gmail.com>.
think more or less know,

in the mode you use it (hybrid between full JTA with EJB CMT and
RESOURCE_LOCAL) you have to clear the entitymanager with the end of the
method.

*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/3/15 Romain Manni-Bucau <rm...@gmail.com>

> Hi,
>
> OpenJPA doesn't register any Synchronization (the way to do the work at
> commit time) when you use UserTransaction this way
> (ejbContext.getUserTransaction().begin();). So all tx work is not really
> done at commit time.
>
> basically calling ((EntityManagerImpl)
> em).setSyncWithManagedTransactions(true); before persist will do the trick
> but will import openjpa. To be able to get it automatically em needs to be
> created after the transaction started.
>
> After OpenJPA is not able to identify the id as an OpenJPA OID (because
> that's simply an int), not sure what it can be here.
>
> not really sure what's the issue ATM
>
> *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/3/15 José Luis Cetina <ma...@gmail.com>
>
>> I wrote "Kevin Sutter's Mail", this mail was written by Rick Curtis not
>> for
>> Kevin Sutter (sorry for that) both of them were helping me with this.
>>
>>
>> 2013/3/14 José Luis Cetina <ma...@gmail.com>
>>
>> >
>> > Hi i discuss this in openJPA mailing lists after many mail Kevin Sutter
>> > told me that i post this to the TomEE mailing lists.
>> >
>> > PLEASE TRY TO READ ALL THE ENTIRE EMAIL.
>> >
>> > I dont want to copy and paste all the emails. I will Resume what is
>> happen
>> > and you will read 2 last emails from openjpa mailing lists.
>> >
>> > Scenario:
>> >
>> > 1. The webpage (jsf) call a method to an EJB, for get an existent entity
>> > from database.
>> > 2. In EJB i get a (MyObject reference) , i get it using a query (jpql or
>> > criterias).
>> > 3. The ejb return one entity of course at this point the entity are
>> > "sended" by the ejb to the managedbean (then the entities are out of the
>> > persistence context  therefore the are dettached)
>> > 4. Then in the managedbean in X method i do the follow:
>> >      - Get the dettached entity. (MyObject)
>> >      - Change some attribute let say the "DESCRIPTION" attribute
>> >      - The managedbean now CALL and EJB method for do an update of this
>> > entity.
>> > 5. The EJB method receive 1 MyObject ( public void update(MyObject obj)
>> )
>> > 6. Then the EJB method need to call an update because the "DESCRIPTION"
>> > property has changed
>> > 7. The method is like this:
>> >
>> >    public void update(MyObject obj){
>> >         begin transaction...
>> >         em.merge(obj);
>> >         commit transaction...
>> >    }
>> >
>> > Then i get this exception
>> >
>> > <openjpa-2.2.0-r422266:1244990 nonfatal user error>
>> > org.apache.openjpa.persistence.InvalidStateException: Primary key field
>> > com.test.MyObject.id of com.test.MyObject@198e0705 has non-default
>> value.
>> > The instance life cycle is in PNewState state and hence an existing
>> > non-default value for the identity field is not permitted. You either
>> need
>> > to remove the @GeneratedValue annotation or modify the code to remove
>> the
>> > initializer processing.
>> >
>> > This only happens when i try to merge an entity that i got from ejb
>> using
>> > criterias or jpql, if i use the find method of the entity manager i dont
>> > have any issue (but of course i cant use only em.find method).
>> >
>> >
>> > What happen when i use the criterias why this works if i use em.find???
>> >
>> >
>> > ****HERE ARE THE LAST 2 EMAILS THAT I SET TO OPENJPA MAILING LISTS***
>> >
>> > ******My mail:
>> >
>> > I could reproduce this problem easy in this project:
>> > https://github.com/maxtorzito/openjpa.git.
>> >
>> > You will see 2 files and 1 folder. The readme contains the
>> > same instructions that you will see here below, the file ddl-dml-db
>> > contains the required sql for create database/table/inserts, and the
>> folder
>> > contains the maven project for run it.
>> >
>> > FOR RUN THIS PROJECT, JUST DO THIS STEPS IN pom.xml project
>> >
>> > 1) Create the database/users table/inserts using the ddl-dml-db file for
>> > this. (MYSQL DATABASE)
>> > 2) mvn -DskipTests=true clean install (this will compile,enhance and
>> > generate metamodel)
>> > 3) mvn tomee:run  (the server will start and you can see INFO - Server
>> > startup in XXXX ms)
>> > 4) Acess the page using the browser and type
>> > http://localhost:8080/jpatest-1.0-SNAPSHOT/index.xhtml
>> > 5) You will see 3 buttons, the first 2 buttons will fail, the third
>> button
>> > will work as expected.
>> >
>> >
>> > Running the plugin will setup easily an Apache TomEE server with all
>> > the necessary for run this "demo" (it will drop the mysql jar to lib
>> folder
>> > too)
>> >
>> > **********The database parameter for connection are under
>> > jpatest/src/main/tomee/conf/tomee.xml you will see this at the bottom.
>> You
>> > can change the username/password
>> >
>> > <Resource id="ExampleDataSource" type="DataSource">
>> >   JdbcDriver com.mysql.jdbc.Driver
>> >   JdbcUrl
>> >
>> jdbc:mysql://localhost:3306/jpa_test?zeroDateTimeBehavior=convertToNull&amp;rewriteBatchedStatements=true
>> >   UserName developer
>> >   Password developer
>> >   JtaManaged true
>> > </Resource>
>> >
>> > The expected result is the first record of the table will update the
>> > description from "USER ONE" to "NEW NICKNAME USING FIND", this only
>> happend
>> > when you click in the THIRD BUTTON.
>> > Im not sure why the first 2 actions buttons failed, the only difference
>> > between the first 2 method's its that they use criterias and jpql for
>> > retrive the entity and the third method use the find entity manager
>> method.
>> >
>> >
>> > It is a maven project using apache tomee as "application server", it
>> has a
>> > plugin for run the project easily.
>> >
>> > Please let me know if you have some troubles for reproduce it as well if
>> > you could reproduce it.
>> >
>> >
>> > Regards.
>> >
>> > ******* END OF MY MAIL
>> >
>> >
>> > *******  Kevin Sutter's Mail:
>> >
>> > Jose -
>> >
>> > I don't have any solid answers for you, but I can tell you what I've
>> found.
>> > It looks like the reason that the criteria/JPQL methods fail is because
>> the
>> > Entities that are returned are still a part of the persistence context
>> that
>> > loaded them. So, you're trying to merge non-transactional Entities that
>> are
>> > a part of one context into another. I have no clue why criteria/JPQL
>> behave
>> > differently than find though?!
>> >
>> > I found that if I changed the @TransactionManagment attribute from BEAN
>> to
>> > CONTAINER, everything worked fine... you could also call em.clear()
>> after
>> > each of your bean methods. The net of it seems like the criteria/JPQL
>> bean
>> > methods aren't cleaning up as OpenJPA is expecting.
>> >
>> > It might make sense to cross post this to a tomee list to see if they
>> could
>> > shed some more light on what is going on.
>> >
>> > Thanks,
>> > Rick
>> >
>> >
>> > ****END OF Kevin Sutter's Mail
>> >
>> >
>> > This could be some TomEE issue using openJPA???
>> > I didn't test this in other application server.
>> >
>> >
>> > Thanks.
>> > Sorry for this large email.
>> >
>>
>>
>>
>> --
>> -------------------------------------------------------------------
>> *SCJA. José Luis Cetina*
>> -------------------------------------------------------------------
>>
>
>

Re: OpenJPA "strange behaviour with EJB+OpenJPA+JPQL/Criteria in TomEE"

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi,

OpenJPA doesn't register any Synchronization (the way to do the work at
commit time) when you use UserTransaction this way
(ejbContext.getUserTransaction().begin();). So all tx work is not really
done at commit time.

basically calling ((EntityManagerImpl)
em).setSyncWithManagedTransactions(true); before persist will do the trick
but will import openjpa. To be able to get it automatically em needs to be
created after the transaction started.

After OpenJPA is not able to identify the id as an OpenJPA OID (because
that's simply an int), not sure what it can be here.

not really sure what's the issue ATM

*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/3/15 José Luis Cetina <ma...@gmail.com>

> I wrote "Kevin Sutter's Mail", this mail was written by Rick Curtis not for
> Kevin Sutter (sorry for that) both of them were helping me with this.
>
>
> 2013/3/14 José Luis Cetina <ma...@gmail.com>
>
> >
> > Hi i discuss this in openJPA mailing lists after many mail Kevin Sutter
> > told me that i post this to the TomEE mailing lists.
> >
> > PLEASE TRY TO READ ALL THE ENTIRE EMAIL.
> >
> > I dont want to copy and paste all the emails. I will Resume what is
> happen
> > and you will read 2 last emails from openjpa mailing lists.
> >
> > Scenario:
> >
> > 1. The webpage (jsf) call a method to an EJB, for get an existent entity
> > from database.
> > 2. In EJB i get a (MyObject reference) , i get it using a query (jpql or
> > criterias).
> > 3. The ejb return one entity of course at this point the entity are
> > "sended" by the ejb to the managedbean (then the entities are out of the
> > persistence context  therefore the are dettached)
> > 4. Then in the managedbean in X method i do the follow:
> >      - Get the dettached entity. (MyObject)
> >      - Change some attribute let say the "DESCRIPTION" attribute
> >      - The managedbean now CALL and EJB method for do an update of this
> > entity.
> > 5. The EJB method receive 1 MyObject ( public void update(MyObject obj) )
> > 6. Then the EJB method need to call an update because the "DESCRIPTION"
> > property has changed
> > 7. The method is like this:
> >
> >    public void update(MyObject obj){
> >         begin transaction...
> >         em.merge(obj);
> >         commit transaction...
> >    }
> >
> > Then i get this exception
> >
> > <openjpa-2.2.0-r422266:1244990 nonfatal user error>
> > org.apache.openjpa.persistence.InvalidStateException: Primary key field
> > com.test.MyObject.id of com.test.MyObject@198e0705 has non-default
> value.
> > The instance life cycle is in PNewState state and hence an existing
> > non-default value for the identity field is not permitted. You either
> need
> > to remove the @GeneratedValue annotation or modify the code to remove the
> > initializer processing.
> >
> > This only happens when i try to merge an entity that i got from ejb using
> > criterias or jpql, if i use the find method of the entity manager i dont
> > have any issue (but of course i cant use only em.find method).
> >
> >
> > What happen when i use the criterias why this works if i use em.find???
> >
> >
> > ****HERE ARE THE LAST 2 EMAILS THAT I SET TO OPENJPA MAILING LISTS***
> >
> > ******My mail:
> >
> > I could reproduce this problem easy in this project:
> > https://github.com/maxtorzito/openjpa.git.
> >
> > You will see 2 files and 1 folder. The readme contains the
> > same instructions that you will see here below, the file ddl-dml-db
> > contains the required sql for create database/table/inserts, and the
> folder
> > contains the maven project for run it.
> >
> > FOR RUN THIS PROJECT, JUST DO THIS STEPS IN pom.xml project
> >
> > 1) Create the database/users table/inserts using the ddl-dml-db file for
> > this. (MYSQL DATABASE)
> > 2) mvn -DskipTests=true clean install (this will compile,enhance and
> > generate metamodel)
> > 3) mvn tomee:run  (the server will start and you can see INFO - Server
> > startup in XXXX ms)
> > 4) Acess the page using the browser and type
> > http://localhost:8080/jpatest-1.0-SNAPSHOT/index.xhtml
> > 5) You will see 3 buttons, the first 2 buttons will fail, the third
> button
> > will work as expected.
> >
> >
> > Running the plugin will setup easily an Apache TomEE server with all
> > the necessary for run this "demo" (it will drop the mysql jar to lib
> folder
> > too)
> >
> > **********The database parameter for connection are under
> > jpatest/src/main/tomee/conf/tomee.xml you will see this at the bottom.
> You
> > can change the username/password
> >
> > <Resource id="ExampleDataSource" type="DataSource">
> >   JdbcDriver com.mysql.jdbc.Driver
> >   JdbcUrl
> >
> jdbc:mysql://localhost:3306/jpa_test?zeroDateTimeBehavior=convertToNull&amp;rewriteBatchedStatements=true
> >   UserName developer
> >   Password developer
> >   JtaManaged true
> > </Resource>
> >
> > The expected result is the first record of the table will update the
> > description from "USER ONE" to "NEW NICKNAME USING FIND", this only
> happend
> > when you click in the THIRD BUTTON.
> > Im not sure why the first 2 actions buttons failed, the only difference
> > between the first 2 method's its that they use criterias and jpql for
> > retrive the entity and the third method use the find entity manager
> method.
> >
> >
> > It is a maven project using apache tomee as "application server", it has
> a
> > plugin for run the project easily.
> >
> > Please let me know if you have some troubles for reproduce it as well if
> > you could reproduce it.
> >
> >
> > Regards.
> >
> > ******* END OF MY MAIL
> >
> >
> > *******  Kevin Sutter's Mail:
> >
> > Jose -
> >
> > I don't have any solid answers for you, but I can tell you what I've
> found.
> > It looks like the reason that the criteria/JPQL methods fail is because
> the
> > Entities that are returned are still a part of the persistence context
> that
> > loaded them. So, you're trying to merge non-transactional Entities that
> are
> > a part of one context into another. I have no clue why criteria/JPQL
> behave
> > differently than find though?!
> >
> > I found that if I changed the @TransactionManagment attribute from BEAN
> to
> > CONTAINER, everything worked fine... you could also call em.clear() after
> > each of your bean methods. The net of it seems like the criteria/JPQL
> bean
> > methods aren't cleaning up as OpenJPA is expecting.
> >
> > It might make sense to cross post this to a tomee list to see if they
> could
> > shed some more light on what is going on.
> >
> > Thanks,
> > Rick
> >
> >
> > ****END OF Kevin Sutter's Mail
> >
> >
> > This could be some TomEE issue using openJPA???
> > I didn't test this in other application server.
> >
> >
> > Thanks.
> > Sorry for this large email.
> >
>
>
>
> --
> -------------------------------------------------------------------
> *SCJA. José Luis Cetina*
> -------------------------------------------------------------------
>

Re: OpenJPA "strange behaviour with EJB+OpenJPA+JPQL/Criteria in TomEE"

Posted by José Luis Cetina <ma...@gmail.com>.
I wrote "Kevin Sutter's Mail", this mail was written by Rick Curtis not for
Kevin Sutter (sorry for that) both of them were helping me with this.


2013/3/14 José Luis Cetina <ma...@gmail.com>

>
> Hi i discuss this in openJPA mailing lists after many mail Kevin Sutter
> told me that i post this to the TomEE mailing lists.
>
> PLEASE TRY TO READ ALL THE ENTIRE EMAIL.
>
> I dont want to copy and paste all the emails. I will Resume what is happen
> and you will read 2 last emails from openjpa mailing lists.
>
> Scenario:
>
> 1. The webpage (jsf) call a method to an EJB, for get an existent entity
> from database.
> 2. In EJB i get a (MyObject reference) , i get it using a query (jpql or
> criterias).
> 3. The ejb return one entity of course at this point the entity are
> "sended" by the ejb to the managedbean (then the entities are out of the
> persistence context  therefore the are dettached)
> 4. Then in the managedbean in X method i do the follow:
>      - Get the dettached entity. (MyObject)
>      - Change some attribute let say the "DESCRIPTION" attribute
>      - The managedbean now CALL and EJB method for do an update of this
> entity.
> 5. The EJB method receive 1 MyObject ( public void update(MyObject obj) )
> 6. Then the EJB method need to call an update because the "DESCRIPTION"
> property has changed
> 7. The method is like this:
>
>    public void update(MyObject obj){
>         begin transaction...
>         em.merge(obj);
>         commit transaction...
>    }
>
> Then i get this exception
>
> <openjpa-2.2.0-r422266:1244990 nonfatal user error>
> org.apache.openjpa.persistence.InvalidStateException: Primary key field
> com.test.MyObject.id of com.test.MyObject@198e0705 has non-default value.
> The instance life cycle is in PNewState state and hence an existing
> non-default value for the identity field is not permitted. You either need
> to remove the @GeneratedValue annotation or modify the code to remove the
> initializer processing.
>
> This only happens when i try to merge an entity that i got from ejb using
> criterias or jpql, if i use the find method of the entity manager i dont
> have any issue (but of course i cant use only em.find method).
>
>
> What happen when i use the criterias why this works if i use em.find???
>
>
> ****HERE ARE THE LAST 2 EMAILS THAT I SET TO OPENJPA MAILING LISTS***
>
> ******My mail:
>
> I could reproduce this problem easy in this project:
> https://github.com/maxtorzito/openjpa.git.
>
> You will see 2 files and 1 folder. The readme contains the
> same instructions that you will see here below, the file ddl-dml-db
> contains the required sql for create database/table/inserts, and the folder
> contains the maven project for run it.
>
> FOR RUN THIS PROJECT, JUST DO THIS STEPS IN pom.xml project
>
> 1) Create the database/users table/inserts using the ddl-dml-db file for
> this. (MYSQL DATABASE)
> 2) mvn -DskipTests=true clean install (this will compile,enhance and
> generate metamodel)
> 3) mvn tomee:run  (the server will start and you can see INFO - Server
> startup in XXXX ms)
> 4) Acess the page using the browser and type
> http://localhost:8080/jpatest-1.0-SNAPSHOT/index.xhtml
> 5) You will see 3 buttons, the first 2 buttons will fail, the third button
> will work as expected.
>
>
> Running the plugin will setup easily an Apache TomEE server with all
> the necessary for run this "demo" (it will drop the mysql jar to lib folder
> too)
>
> **********The database parameter for connection are under
> jpatest/src/main/tomee/conf/tomee.xml you will see this at the bottom. You
> can change the username/password
>
> <Resource id="ExampleDataSource" type="DataSource">
>   JdbcDriver com.mysql.jdbc.Driver
>   JdbcUrl
> jdbc:mysql://localhost:3306/jpa_test?zeroDateTimeBehavior=convertToNull&amp;rewriteBatchedStatements=true
>   UserName developer
>   Password developer
>   JtaManaged true
> </Resource>
>
> The expected result is the first record of the table will update the
> description from "USER ONE" to "NEW NICKNAME USING FIND", this only happend
> when you click in the THIRD BUTTON.
> Im not sure why the first 2 actions buttons failed, the only difference
> between the first 2 method's its that they use criterias and jpql for
> retrive the entity and the third method use the find entity manager method.
>
>
> It is a maven project using apache tomee as "application server", it has a
> plugin for run the project easily.
>
> Please let me know if you have some troubles for reproduce it as well if
> you could reproduce it.
>
>
> Regards.
>
> ******* END OF MY MAIL
>
>
> *******  Kevin Sutter's Mail:
>
> Jose -
>
> I don't have any solid answers for you, but I can tell you what I've found.
> It looks like the reason that the criteria/JPQL methods fail is because the
> Entities that are returned are still a part of the persistence context that
> loaded them. So, you're trying to merge non-transactional Entities that are
> a part of one context into another. I have no clue why criteria/JPQL behave
> differently than find though?!
>
> I found that if I changed the @TransactionManagment attribute from BEAN to
> CONTAINER, everything worked fine... you could also call em.clear() after
> each of your bean methods. The net of it seems like the criteria/JPQL bean
> methods aren't cleaning up as OpenJPA is expecting.
>
> It might make sense to cross post this to a tomee list to see if they could
> shed some more light on what is going on.
>
> Thanks,
> Rick
>
>
> ****END OF Kevin Sutter's Mail
>
>
> This could be some TomEE issue using openJPA???
> I didn't test this in other application server.
>
>
> Thanks.
> Sorry for this large email.
>



-- 
-------------------------------------------------------------------
*SCJA. José Luis Cetina*
-------------------------------------------------------------------