You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by David Goodenough <da...@btconnect.com> on 2008/04/01 16:04:03 UTC

How to diagnose rather cryptic error messages

I am relatively new to JPA, and trying to get to grips with what I can 
and can not do with it.

So I thought I would try creating a small system (which will grow) which
has amongst other things User objects and Role objects in it.

OpenJPA has successfully taken my list of Entities and created the
relevant tables.  The Entity classes have all been enhanced.  I am using
OpenJPA 1.0.2.

I create a Role object (which does not contain a reference to other 
objects) and persist it.  No errors are reported, but the object does
not appear as a record in the DB.  Does a persist have to be inside 
a Transaction?  The manual is confusing as it says that "this action can
only be performed in the context of an active transaction" right at the
end of the description of persist, but I read it (given the way it was 
laid out) to apply to the remove action which immediately follows it.

Then create a User object, which amongst other things has a List of
Role objects in it, so I add the Role object I just create to it, fill in all 
the other fields, and try to persist it.  

I get back an exception which says:-

Exception in thread "main" <openjpa-1.0.2-r420667:627158 fatal general error> 
org.apache.openjpa.persistence.PersistenceException: null
        at 
org.apache.openjpa.meta.ProxySetupStateManager.providedStringField(ProxySetupStateManager.java:234)
        at uk.co.dga.bm.jpa.User.pcProvideField(User.java)
        at 
org.apache.openjpa.meta.ProxySetupStateManager.setProxyData(ProxySetupStateManager.java:58)
        at 
org.apache.openjpa.meta.ClassMetaData.resolveMeta(ClassMetaData.java:1731)
        at 
org.apache.openjpa.meta.ClassMetaData.resolve(ClassMetaData.java:1613)
        at 
org.apache.openjpa.meta.MetaDataRepository.processBuffer(MetaDataRepository.java:675)
        at 
org.apache.openjpa.meta.MetaDataRepository.resolveMeta(MetaDataRepository.java:575)
        at 
org.apache.openjpa.meta.MetaDataRepository.resolve(MetaDataRepository.java:500)
        at 
org.apache.openjpa.meta.MetaDataRepository.getMetaData(MetaDataRepository.java:302)
        at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2372)
        at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2225)
        at 
org.apache.openjpa.kernel.DelegatingBroker.persist(DelegatingBroker.java:1005)
        at 
org.apache.openjpa.persistence.EntityManagerImpl.persist(EntityManagerImpl.java:541)
        at uk.co.dga.bm.loader.CreateUser.run(CreateUser.java:34)
        at uk.co.dga.bm.loader.CreateUser.main(CreateUser.java:41)

which is not very helpful.  How do I get more information on exactly what it 
is trying to do so that I can fix it?

David

Re: How to diagnose rather cryptic error messages

Posted by Patrick Linskey <pl...@gmail.com>.
>  > em.persist(new User());
>  > em.getTransaction().begin();
>  > em.getTransaction().commit();
>  What is the effect of such an apparently null transaction?

To insert the instance. You might want to take a look at section 3.3
of the JPA specification -- it discusses extended persistence contexts
in more detail. In particular:

"When an EntityManager with an extended persistence context is used,
the persist, remove, merge, and
refresh operations may be called regardless of whether a transaction
is active. The effects of these operations
will be committed to the database when the extended persistence
context is enlisted in a transaction
and the transaction commits."

-Patrick

On Tue, Apr 1, 2008 at 10:00 AM, David Goodenough
<da...@btconnect.com> wrote:
> On Tuesday 01 April 2008, Patrick Linskey wrote:
>
> > >  Also why does the persist apparently work? Should it now throw an
>  > >  exception if it is used out of a transaction?
>  >
>  > Because this is valid in some contexts (extended persistence contexts):
>  Ah, well I think I am in an extended context, being an SE app, not an EE one?
>
> >
>  > em.persist(new User());
>  > em.getTransaction().begin();
>  > em.getTransaction().commit();
>  What is the effect of such an apparently null transaction?
>
> >
>  > >  To be honest my first code has cascade=CascadeType.ALL just about
>  > >  everwhere, until I get into more details.
>  >
>  > Note that this can be dangerous -- CascadeType.REMOVE means that if
>  > you delete an instance, its so-marked related instances will be
>  > deleted also.
>  yes, as I said I was trying to get going so I removed the brakes (or throught
>  I did).
>
>  David
>
>
> >
>  > -Patrick
>  >
>  > On Tue, Apr 1, 2008 at 9:31 AM, David Goodenough
>  >
>  > <da...@btconnect.com> wrote:
>  > > On Tuesday 01 April 2008, Patrick Linskey wrote:
>  > >  > >  Does a persist have to be inside a Transaction?
>  > >  >
>  > >  > In short, yes -- changes will only get written back to the database
>  > >  > within a transaction. IIRC, if you call persist() outside a
>  > >  > transaction and then begin and commit one, the new objects will be
>  > >  > written also.
>  > >
>  > >  Can I suggest that the manual could be clearer here.  In section 8.2
>  > >  on page 65 it is not obvious whether the comment just above the
>  > >  code box for remove applies to the persist or remove methods.
>  > >
>  > >  Also why does the persist apparently work? Should it now throw an
>  > >  exception if it is used out of a transaction?
>  > >
>  > >  > >  Then create a User object, which amongst other things has a List of
>  > >  > >  Role objects in it, so I add the Role object I just create to it,
>  > >  > > fill in all the other fields, and try to persist it.
>  > >  >
>  > >  > Bear in mind that one of the interesting "features" of the JPA
>  > >  > specification is that when you have a persistent relationship, you
>  > >  > need to either put a 'cascade=CascadeType.PERSIST' (or equivalent)
>  > >  > into the annotation on the relationship, or manually call persist() on
>  > >  > related data, or set the cascade-persist default override switch in
>  > >  > your orm.xml. (Yes, I agree that this is more complicated than it
>  > >  > should be; this is just a warning.)
>  > >
>  > >  To be honest my first code has cascade=CascadeType.ALL just about
>  > >  everwhere, until I get into more details.
>  > >
>  > >  David
>  > >
>  > >  > >  I get back an exception which says:-
>  > >  > >
>  > >  > >  Exception in thread "main" <openjpa-1.0.2-r420667:627158 fatal
>  > >  > > general error> org.apache.openjpa.persistence.PersistenceException:
>  > >  > > null
>  > >  >
>  > >  > That's definitely not a great error.
>  > >  >
>  > >  > -Patrick
>  > >  >
>  > >  > On Tue, Apr 1, 2008 at 7:04 AM, David Goodenough
>  > >  >
>  > >  > <da...@btconnect.com> wrote:
>  > >  > > I am relatively new to JPA, and trying to get to grips with what I
>  > >  > > can and can not do with it.
>  > >  > >
>  > >  > >  So I thought I would try creating a small system (which will grow)
>  > >  > > which has amongst other things User objects and Role objects in it.
>  > >  > >
>  > >  > >  OpenJPA has successfully taken my list of Entities and created the
>  > >  > >  relevant tables.  The Entity classes have all been enhanced.  I am
>  > >  > > using OpenJPA 1.0.2.
>  > >  > >
>  > >  > >  I create a Role object (which does not contain a reference to other
>  > >  > >  objects) and persist it.  No errors are reported, but the object
>  > >  > > does not appear as a record in the DB.  Does a persist have to be
>  > >  > > inside a Transaction?  The manual is confusing as it says that "this
>  > >  > > action can only be performed in the context of an active
>  > >  > > transaction" right at the end of the description of persist, but I
>  > >  > > read it (given the way it was laid out) to apply to the remove
>  > >  > > action which immediately follows it.
>  > >  > >
>  > >  > >  Then create a User object, which amongst other things has a List of
>  > >  > >  Role objects in it, so I add the Role object I just create to it,
>  > >  > > fill in all the other fields, and try to persist it.
>  > >  > >
>  > >  > >  I get back an exception which says:-
>  > >  > >
>  > >  > >  Exception in thread "main" <openjpa-1.0.2-r420667:627158 fatal
>  > >  > > general error> org.apache.openjpa.persistence.PersistenceException:
>  > >  > > null at
>  > >  > >
>  > >  > > org.apache.openjpa.meta.ProxySetupStateManager.providedStringField(P
>  > >  > >roxyS etupStateManager.java:234) at
>
> > >  > > uk.co.dga.bm.jpa.User.pcProvideField(User.java)
>  > >  > >         at
>  > >  > >
>  > >  > > org.apache.openjpa.meta.ProxySetupStateManager.setProxyData(ProxySet
>  > >  > >upSta teManager.java:58) at
>
> > >  > >
>  > >  > > org.apache.openjpa.meta.ClassMetaData.resolveMeta(ClassMetaData.java
>  > >  > >:1731 ) at
>  > >  > >
>  > >  > > org.apache.openjpa.meta.ClassMetaData.resolve(ClassMetaData.java:161
>  > >  > >3) at
>
> > >  > >
>  > >  > > org.apache.openjpa.meta.MetaDataRepository.processBuffer(MetaDataRep
>  > >  > >osito ry.java:675) at
>
> > >  > >
>  > >  > > org.apache.openjpa.meta.MetaDataRepository.resolveMeta(MetaDataRepos
>  > >  > >itory .java:575) at
>
> > >  > >
>  > >  > > org.apache.openjpa.meta.MetaDataRepository.resolve(MetaDataRepositor
>  > >  > >y.jav a:500) at
>  > >  > >
>  > >  > > org.apache.openjpa.meta.MetaDataRepository.getMetaData(MetaDataRepos
>  > >  > >itory .java:302) at
>
> > >  > > org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2372)
>  > >  > > at
>  > >  > > org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2225)
>  > >  > > at
>  > >  > >
>  > >  > > org.apache.openjpa.kernel.DelegatingBroker.persist(DelegatingBroker.
>  > >  > >java: 1005) at
>  > >  > >
>  > >  > > org.apache.openjpa.persistence.EntityManagerImpl.persist(EntityManag
>  > >  > >erImp l.java:541) at
>
>
> > >  > > uk.co.dga.bm.loader.CreateUser.run(CreateUser.java:34) at
>  > >  > > uk.co.dga.bm.loader.CreateUser.main(CreateUser.java:41)
>  > >  > >
>  > >  > >  which is not very helpful.  How do I get more information on
>  > >  > > exactly what it is trying to do so that I can fix it?
>  > >  > >
>  > >  > >  David
>
>
>



-- 
Patrick Linskey
202 669 5907

Re: How to diagnose rather cryptic error messages

Posted by Michael Dick <mi...@gmail.com>.
On Tue, Apr 1, 2008 at 12:00 PM, David Goodenough <
david.goodenough@btconnect.com> wrote:

> On Tuesday 01 April 2008, Patrick Linskey wrote:
> > >  Also why does the persist apparently work? Should it now throw an
> > >  exception if it is used out of a transaction?
> >
> > Because this is valid in some contexts (extended persistence contexts):
> Ah, well I think I am in an extended context, being an SE app, not an EE
> one?
> >
> > em.persist(new User());
> > em.getTransaction().begin();
> > em.getTransaction().commit();
> What is the effect of such an apparently null transaction?


Well, the user will be persisted, but that's already been covered. In this
case you are using an extended persistence context which means that the
entities are considered managed even when they're obtained outside of a
transaction. They'll remain managed until the context (em) is closed, or
they're manually detached (ie em.clear()).

So when you begin and commit a transaction the EntityManager will flush
(write) the state of all the entities its managing to the database.

-Mike

>
> >
> > >  To be honest my first code has cascade=CascadeType.ALL just about
> > >  everwhere, until I get into more details.
> >
> > Note that this can be dangerous -- CascadeType.REMOVE means that if
> > you delete an instance, its so-marked related instances will be
> > deleted also.
> yes, as I said I was trying to get going so I removed the brakes (or
> throught
> I did).
>
> David
> >
> > -Patrick
> >
> > On Tue, Apr 1, 2008 at 9:31 AM, David Goodenough
> >
> > <da...@btconnect.com> wrote:
> > > On Tuesday 01 April 2008, Patrick Linskey wrote:
> > >  > >  Does a persist have to be inside a Transaction?
> > >  >
> > >  > In short, yes -- changes will only get written back to the database
> > >  > within a transaction. IIRC, if you call persist() outside a
> > >  > transaction and then begin and commit one, the new objects will be
> > >  > written also.
> > >
> > >  Can I suggest that the manual could be clearer here.  In section 8.2
> > >  on page 65 it is not obvious whether the comment just above the
> > >  code box for remove applies to the persist or remove methods.
> > >
> > >  Also why does the persist apparently work? Should it now throw an
> > >  exception if it is used out of a transaction?
> > >
> > >  > >  Then create a User object, which amongst other things has a List
> of
> > >  > >  Role objects in it, so I add the Role object I just create to
> it,
> > >  > > fill in all the other fields, and try to persist it.
> > >  >
> > >  > Bear in mind that one of the interesting "features" of the JPA
> > >  > specification is that when you have a persistent relationship, you
> > >  > need to either put a 'cascade=CascadeType.PERSIST' (or equivalent)
> > >  > into the annotation on the relationship, or manually call persist()
> on
> > >  > related data, or set the cascade-persist default override switch in
> > >  > your orm.xml. (Yes, I agree that this is more complicated than it
> > >  > should be; this is just a warning.)
> > >
> > >  To be honest my first code has cascade=CascadeType.ALL just about
> > >  everwhere, until I get into more details.
> > >
> > >  David
> > >
> > >  > >  I get back an exception which says:-
> > >  > >
> > >  > >  Exception in thread "main" <openjpa-1.0.2-r420667:627158 fatal
> > >  > > general error>
> org.apache.openjpa.persistence.PersistenceException:
> > >  > > null
> > >  >
> > >  > That's definitely not a great error.
> > >  >
> > >  > -Patrick
> > >  >
> > >  > On Tue, Apr 1, 2008 at 7:04 AM, David Goodenough
> > >  >
> > >  > <da...@btconnect.com> wrote:
> > >  > > I am relatively new to JPA, and trying to get to grips with what
> I
> > >  > > can and can not do with it.
> > >  > >
> > >  > >  So I thought I would try creating a small system (which will
> grow)
> > >  > > which has amongst other things User objects and Role objects in
> it.
> > >  > >
> > >  > >  OpenJPA has successfully taken my list of Entities and created
> the
> > >  > >  relevant tables.  The Entity classes have all been enhanced.  I
> am
> > >  > > using OpenJPA 1.0.2.
> > >  > >
> > >  > >  I create a Role object (which does not contain a reference to
> other
> > >  > >  objects) and persist it.  No errors are reported, but the object
> > >  > > does not appear as a record in the DB.  Does a persist have to be
> > >  > > inside a Transaction?  The manual is confusing as it says that
> "this
> > >  > > action can only be performed in the context of an active
> > >  > > transaction" right at the end of the description of persist, but
> I
> > >  > > read it (given the way it was laid out) to apply to the remove
> > >  > > action which immediately follows it.
> > >  > >
> > >  > >  Then create a User object, which amongst other things has a List
> of
> > >  > >  Role objects in it, so I add the Role object I just create to
> it,
> > >  > > fill in all the other fields, and try to persist it.
> > >  > >
> > >  > >  I get back an exception which says:-
> > >  > >
> > >  > >  Exception in thread "main" <openjpa-1.0.2-r420667:627158 fatal
> > >  > > general error>
> org.apache.openjpa.persistence.PersistenceException:
> > >  > > null at
> > >  > >
> > >  > >
> org.apache.openjpa.meta.ProxySetupStateManager.providedStringField(P
> > >  > >roxyS etupStateManager.java:234) at
> > >  > > uk.co.dga.bm.jpa.User.pcProvideField(User.java)
> > >  > >         at
> > >  > >
> > >  > > org.apache.openjpa.meta.ProxySetupStateManager.setProxyData
> (ProxySet
> > >  > >upSta teManager.java:58) at
> > >  > >
> > >  > > org.apache.openjpa.meta.ClassMetaData.resolveMeta(
> ClassMetaData.java
> > >  > >:1731 ) at
> > >  > >
> > >  > > org.apache.openjpa.meta.ClassMetaData.resolve(ClassMetaData.java
> :161
> > >  > >3) at
> > >  > >
> > >  > > org.apache.openjpa.meta.MetaDataRepository.processBuffer
> (MetaDataRep
> > >  > >osito ry.java:675) at
> > >  > >
> > >  > > org.apache.openjpa.meta.MetaDataRepository.resolveMeta
> (MetaDataRepos
> > >  > >itory .java:575) at
> > >  > >
> > >  > > org.apache.openjpa.meta.MetaDataRepository.resolve
> (MetaDataRepositor
> > >  > >y.jav a:500) at
> > >  > >
> > >  > > org.apache.openjpa.meta.MetaDataRepository.getMetaData
> (MetaDataRepos
> > >  > >itory .java:302) at
> > >  > > org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java
> :2372)
> > >  > > at
> > >  > > org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java
> :2225)
> > >  > > at
> > >  > >
> > >  > > org.apache.openjpa.kernel.DelegatingBroker.persist
> (DelegatingBroker.
> > >  > >java: 1005) at
> > >  > >
> > >  > > org.apache.openjpa.persistence.EntityManagerImpl.persist
> (EntityManag
> > >  > >erImp l.java:541) at
> > >  > > uk.co.dga.bm.loader.CreateUser.run(CreateUser.java:34) at
> > >  > > uk.co.dga.bm.loader.CreateUser.main(CreateUser.java:41)
> > >  > >
> > >  > >  which is not very helpful.  How do I get more information on
> > >  > > exactly what it is trying to do so that I can fix it?
> > >  > >
> > >  > >  David
>
>
>

Re: How to diagnose rather cryptic error messages

Posted by David Goodenough <da...@btconnect.com>.
On Tuesday 01 April 2008, Patrick Linskey wrote:
> >  Also why does the persist apparently work? Should it now throw an
> >  exception if it is used out of a transaction?
>
> Because this is valid in some contexts (extended persistence contexts):
Ah, well I think I am in an extended context, being an SE app, not an EE one?
>
> em.persist(new User());
> em.getTransaction().begin();
> em.getTransaction().commit();
What is the effect of such an apparently null transaction?
>
> >  To be honest my first code has cascade=CascadeType.ALL just about
> >  everwhere, until I get into more details.
>
> Note that this can be dangerous -- CascadeType.REMOVE means that if
> you delete an instance, its so-marked related instances will be
> deleted also.
yes, as I said I was trying to get going so I removed the brakes (or throught
I did).

David
>
> -Patrick
>
> On Tue, Apr 1, 2008 at 9:31 AM, David Goodenough
>
> <da...@btconnect.com> wrote:
> > On Tuesday 01 April 2008, Patrick Linskey wrote:
> >  > >  Does a persist have to be inside a Transaction?
> >  >
> >  > In short, yes -- changes will only get written back to the database
> >  > within a transaction. IIRC, if you call persist() outside a
> >  > transaction and then begin and commit one, the new objects will be
> >  > written also.
> >
> >  Can I suggest that the manual could be clearer here.  In section 8.2
> >  on page 65 it is not obvious whether the comment just above the
> >  code box for remove applies to the persist or remove methods.
> >
> >  Also why does the persist apparently work? Should it now throw an
> >  exception if it is used out of a transaction?
> >
> >  > >  Then create a User object, which amongst other things has a List of
> >  > >  Role objects in it, so I add the Role object I just create to it,
> >  > > fill in all the other fields, and try to persist it.
> >  >
> >  > Bear in mind that one of the interesting "features" of the JPA
> >  > specification is that when you have a persistent relationship, you
> >  > need to either put a 'cascade=CascadeType.PERSIST' (or equivalent)
> >  > into the annotation on the relationship, or manually call persist() on
> >  > related data, or set the cascade-persist default override switch in
> >  > your orm.xml. (Yes, I agree that this is more complicated than it
> >  > should be; this is just a warning.)
> >
> >  To be honest my first code has cascade=CascadeType.ALL just about
> >  everwhere, until I get into more details.
> >
> >  David
> >
> >  > >  I get back an exception which says:-
> >  > >
> >  > >  Exception in thread "main" <openjpa-1.0.2-r420667:627158 fatal
> >  > > general error> org.apache.openjpa.persistence.PersistenceException:
> >  > > null
> >  >
> >  > That's definitely not a great error.
> >  >
> >  > -Patrick
> >  >
> >  > On Tue, Apr 1, 2008 at 7:04 AM, David Goodenough
> >  >
> >  > <da...@btconnect.com> wrote:
> >  > > I am relatively new to JPA, and trying to get to grips with what I
> >  > > can and can not do with it.
> >  > >
> >  > >  So I thought I would try creating a small system (which will grow)
> >  > > which has amongst other things User objects and Role objects in it.
> >  > >
> >  > >  OpenJPA has successfully taken my list of Entities and created the
> >  > >  relevant tables.  The Entity classes have all been enhanced.  I am
> >  > > using OpenJPA 1.0.2.
> >  > >
> >  > >  I create a Role object (which does not contain a reference to other
> >  > >  objects) and persist it.  No errors are reported, but the object
> >  > > does not appear as a record in the DB.  Does a persist have to be
> >  > > inside a Transaction?  The manual is confusing as it says that "this
> >  > > action can only be performed in the context of an active
> >  > > transaction" right at the end of the description of persist, but I
> >  > > read it (given the way it was laid out) to apply to the remove
> >  > > action which immediately follows it.
> >  > >
> >  > >  Then create a User object, which amongst other things has a List of
> >  > >  Role objects in it, so I add the Role object I just create to it,
> >  > > fill in all the other fields, and try to persist it.
> >  > >
> >  > >  I get back an exception which says:-
> >  > >
> >  > >  Exception in thread "main" <openjpa-1.0.2-r420667:627158 fatal
> >  > > general error> org.apache.openjpa.persistence.PersistenceException:
> >  > > null at
> >  > >
> >  > > org.apache.openjpa.meta.ProxySetupStateManager.providedStringField(P
> >  > >roxyS etupStateManager.java:234) at
> >  > > uk.co.dga.bm.jpa.User.pcProvideField(User.java)
> >  > >         at
> >  > >
> >  > > org.apache.openjpa.meta.ProxySetupStateManager.setProxyData(ProxySet
> >  > >upSta teManager.java:58) at
> >  > >
> >  > > org.apache.openjpa.meta.ClassMetaData.resolveMeta(ClassMetaData.java
> >  > >:1731 ) at
> >  > > 
> >  > > org.apache.openjpa.meta.ClassMetaData.resolve(ClassMetaData.java:161
> >  > >3) at
> >  > >
> >  > > org.apache.openjpa.meta.MetaDataRepository.processBuffer(MetaDataRep
> >  > >osito ry.java:675) at
> >  > >
> >  > > org.apache.openjpa.meta.MetaDataRepository.resolveMeta(MetaDataRepos
> >  > >itory .java:575) at
> >  > >
> >  > > org.apache.openjpa.meta.MetaDataRepository.resolve(MetaDataRepositor
> >  > >y.jav a:500) at
> >  > >
> >  > > org.apache.openjpa.meta.MetaDataRepository.getMetaData(MetaDataRepos
> >  > >itory .java:302) at
> >  > > org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2372)
> >  > > at
> >  > > org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2225)
> >  > > at
> >  > >
> >  > > org.apache.openjpa.kernel.DelegatingBroker.persist(DelegatingBroker.
> >  > >java: 1005) at
> >  > >
> >  > > org.apache.openjpa.persistence.EntityManagerImpl.persist(EntityManag
> >  > >erImp l.java:541) at
> >  > > uk.co.dga.bm.loader.CreateUser.run(CreateUser.java:34) at
> >  > > uk.co.dga.bm.loader.CreateUser.main(CreateUser.java:41)
> >  > >
> >  > >  which is not very helpful.  How do I get more information on
> >  > > exactly what it is trying to do so that I can fix it?
> >  > >
> >  > >  David



Re: How to diagnose rather cryptic error messages

Posted by Patrick Linskey <pl...@gmail.com>.
>  Also why does the persist apparently work? Should it now throw an
>  exception if it is used out of a transaction?

Because this is valid in some contexts (extended persistence contexts):

em.persist(new User());
em.getTransaction().begin();
em.getTransaction().commit();

>  To be honest my first code has cascade=CascadeType.ALL just about
>  everwhere, until I get into more details.

Note that this can be dangerous -- CascadeType.REMOVE means that if
you delete an instance, its so-marked related instances will be
deleted also.

-Patrick

On Tue, Apr 1, 2008 at 9:31 AM, David Goodenough
<da...@btconnect.com> wrote:
> On Tuesday 01 April 2008, Patrick Linskey wrote:
>  > >  Does a persist have to be inside a Transaction?
>  >
>  > In short, yes -- changes will only get written back to the database
>  > within a transaction. IIRC, if you call persist() outside a
>  > transaction and then begin and commit one, the new objects will be
>  > written also.
>  Can I suggest that the manual could be clearer here.  In section 8.2
>  on page 65 it is not obvious whether the comment just above the
>  code box for remove applies to the persist or remove methods.
>
>  Also why does the persist apparently work? Should it now throw an
>  exception if it is used out of a transaction?
>
> >
>  > >  Then create a User object, which amongst other things has a List of
>  > >  Role objects in it, so I add the Role object I just create to it, fill
>  > > in all the other fields, and try to persist it.
>  >
>  > Bear in mind that one of the interesting "features" of the JPA
>  > specification is that when you have a persistent relationship, you
>  > need to either put a 'cascade=CascadeType.PERSIST' (or equivalent)
>  > into the annotation on the relationship, or manually call persist() on
>  > related data, or set the cascade-persist default override switch in
>  > your orm.xml. (Yes, I agree that this is more complicated than it
>  > should be; this is just a warning.)
>  To be honest my first code has cascade=CascadeType.ALL just about
>  everwhere, until I get into more details.
>
>  David
>
>
> >
>  > >  I get back an exception which says:-
>  > >
>  > >  Exception in thread "main" <openjpa-1.0.2-r420667:627158 fatal general
>  > > error> org.apache.openjpa.persistence.PersistenceException: null
>  >
>  > That's definitely not a great error.
>  >
>  > -Patrick
>  >
>  > On Tue, Apr 1, 2008 at 7:04 AM, David Goodenough
>  >
>  > <da...@btconnect.com> wrote:
>  > > I am relatively new to JPA, and trying to get to grips with what I can
>  > >  and can not do with it.
>  > >
>  > >  So I thought I would try creating a small system (which will grow) which
>  > >  has amongst other things User objects and Role objects in it.
>  > >
>  > >  OpenJPA has successfully taken my list of Entities and created the
>  > >  relevant tables.  The Entity classes have all been enhanced.  I am using
>  > >  OpenJPA 1.0.2.
>  > >
>  > >  I create a Role object (which does not contain a reference to other
>  > >  objects) and persist it.  No errors are reported, but the object does
>  > >  not appear as a record in the DB.  Does a persist have to be inside
>  > >  a Transaction?  The manual is confusing as it says that "this action can
>  > >  only be performed in the context of an active transaction" right at the
>  > >  end of the description of persist, but I read it (given the way it was
>  > >  laid out) to apply to the remove action which immediately follows it.
>  > >
>  > >  Then create a User object, which amongst other things has a List of
>  > >  Role objects in it, so I add the Role object I just create to it, fill
>  > > in all the other fields, and try to persist it.
>  > >
>  > >  I get back an exception which says:-
>  > >
>  > >  Exception in thread "main" <openjpa-1.0.2-r420667:627158 fatal general
>  > > error> org.apache.openjpa.persistence.PersistenceException: null
>  > >         at
>  > >
>  > > org.apache.openjpa.meta.ProxySetupStateManager.providedStringField(ProxyS
>  > >etupStateManager.java:234) at
>  > > uk.co.dga.bm.jpa.User.pcProvideField(User.java)
>  > >         at
>  > >
>  > > org.apache.openjpa.meta.ProxySetupStateManager.setProxyData(ProxySetupSta
>  > >teManager.java:58) at
>  > >
>  > > org.apache.openjpa.meta.ClassMetaData.resolveMeta(ClassMetaData.java:1731
>  > >) at
>  > >  org.apache.openjpa.meta.ClassMetaData.resolve(ClassMetaData.java:1613)
>  > >         at
>  > >
>  > > org.apache.openjpa.meta.MetaDataRepository.processBuffer(MetaDataReposito
>  > >ry.java:675) at
>  > >
>  > > org.apache.openjpa.meta.MetaDataRepository.resolveMeta(MetaDataRepository
>  > >.java:575) at
>  > >
>  > > org.apache.openjpa.meta.MetaDataRepository.resolve(MetaDataRepository.jav
>  > >a:500) at
>  > >
>  > > org.apache.openjpa.meta.MetaDataRepository.getMetaData(MetaDataRepository
>  > >.java:302) at
>  > > org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2372) at
>  > > org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2225) at
>  > >
>  > > org.apache.openjpa.kernel.DelegatingBroker.persist(DelegatingBroker.java:
>  > >1005) at
>  > >
>  > > org.apache.openjpa.persistence.EntityManagerImpl.persist(EntityManagerImp
>  > >l.java:541) at uk.co.dga.bm.loader.CreateUser.run(CreateUser.java:34) at
>  > > uk.co.dga.bm.loader.CreateUser.main(CreateUser.java:41)
>  > >
>  > >  which is not very helpful.  How do I get more information on exactly
>  > > what it is trying to do so that I can fix it?
>  > >
>  > >  David
>



-- 
Patrick Linskey
202 669 5907

Re: How to diagnose rather cryptic error messages

Posted by David Goodenough <da...@btconnect.com>.
On Tuesday 01 April 2008, Patrick Linskey wrote:
> >  Does a persist have to be inside a Transaction?
>
> In short, yes -- changes will only get written back to the database
> within a transaction. IIRC, if you call persist() outside a
> transaction and then begin and commit one, the new objects will be
> written also.
Can I suggest that the manual could be clearer here.  In section 8.2
on page 65 it is not obvious whether the comment just above the 
code box for remove applies to the persist or remove methods.

Also why does the persist apparently work? Should it now throw an
exception if it is used out of a transaction?
>
> >  Then create a User object, which amongst other things has a List of
> >  Role objects in it, so I add the Role object I just create to it, fill
> > in all the other fields, and try to persist it.
>
> Bear in mind that one of the interesting "features" of the JPA
> specification is that when you have a persistent relationship, you
> need to either put a 'cascade=CascadeType.PERSIST' (or equivalent)
> into the annotation on the relationship, or manually call persist() on
> related data, or set the cascade-persist default override switch in
> your orm.xml. (Yes, I agree that this is more complicated than it
> should be; this is just a warning.)
To be honest my first code has cascade=CascadeType.ALL just about
everwhere, until I get into more details.

David
>
> >  I get back an exception which says:-
> >
> >  Exception in thread "main" <openjpa-1.0.2-r420667:627158 fatal general
> > error> org.apache.openjpa.persistence.PersistenceException: null
>
> That's definitely not a great error.
>
> -Patrick
>
> On Tue, Apr 1, 2008 at 7:04 AM, David Goodenough
>
> <da...@btconnect.com> wrote:
> > I am relatively new to JPA, and trying to get to grips with what I can
> >  and can not do with it.
> >
> >  So I thought I would try creating a small system (which will grow) which
> >  has amongst other things User objects and Role objects in it.
> >
> >  OpenJPA has successfully taken my list of Entities and created the
> >  relevant tables.  The Entity classes have all been enhanced.  I am using
> >  OpenJPA 1.0.2.
> >
> >  I create a Role object (which does not contain a reference to other
> >  objects) and persist it.  No errors are reported, but the object does
> >  not appear as a record in the DB.  Does a persist have to be inside
> >  a Transaction?  The manual is confusing as it says that "this action can
> >  only be performed in the context of an active transaction" right at the
> >  end of the description of persist, but I read it (given the way it was
> >  laid out) to apply to the remove action which immediately follows it.
> >
> >  Then create a User object, which amongst other things has a List of
> >  Role objects in it, so I add the Role object I just create to it, fill
> > in all the other fields, and try to persist it.
> >
> >  I get back an exception which says:-
> >
> >  Exception in thread "main" <openjpa-1.0.2-r420667:627158 fatal general
> > error> org.apache.openjpa.persistence.PersistenceException: null
> >         at
> > 
> > org.apache.openjpa.meta.ProxySetupStateManager.providedStringField(ProxyS
> >etupStateManager.java:234) at
> > uk.co.dga.bm.jpa.User.pcProvideField(User.java)
> >         at
> > 
> > org.apache.openjpa.meta.ProxySetupStateManager.setProxyData(ProxySetupSta
> >teManager.java:58) at
> > 
> > org.apache.openjpa.meta.ClassMetaData.resolveMeta(ClassMetaData.java:1731
> >) at
> >  org.apache.openjpa.meta.ClassMetaData.resolve(ClassMetaData.java:1613)
> >         at
> > 
> > org.apache.openjpa.meta.MetaDataRepository.processBuffer(MetaDataReposito
> >ry.java:675) at
> > 
> > org.apache.openjpa.meta.MetaDataRepository.resolveMeta(MetaDataRepository
> >.java:575) at
> > 
> > org.apache.openjpa.meta.MetaDataRepository.resolve(MetaDataRepository.jav
> >a:500) at
> > 
> > org.apache.openjpa.meta.MetaDataRepository.getMetaData(MetaDataRepository
> >.java:302) at
> > org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2372) at
> > org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2225) at
> > 
> > org.apache.openjpa.kernel.DelegatingBroker.persist(DelegatingBroker.java:
> >1005) at
> > 
> > org.apache.openjpa.persistence.EntityManagerImpl.persist(EntityManagerImp
> >l.java:541) at uk.co.dga.bm.loader.CreateUser.run(CreateUser.java:34) at
> > uk.co.dga.bm.loader.CreateUser.main(CreateUser.java:41)
> >
> >  which is not very helpful.  How do I get more information on exactly
> > what it is trying to do so that I can fix it?
> >
> >  David

Re: How to diagnose rather cryptic error messages

Posted by Patrick Linskey <pl...@gmail.com>.
>  Does a persist have to be inside a Transaction?

In short, yes -- changes will only get written back to the database
within a transaction. IIRC, if you call persist() outside a
transaction and then begin and commit one, the new objects will be
written also.

>  Then create a User object, which amongst other things has a List of
>  Role objects in it, so I add the Role object I just create to it, fill in all
>  the other fields, and try to persist it.

Bear in mind that one of the interesting "features" of the JPA
specification is that when you have a persistent relationship, you
need to either put a 'cascade=CascadeType.PERSIST' (or equivalent)
into the annotation on the relationship, or manually call persist() on
related data, or set the cascade-persist default override switch in
your orm.xml. (Yes, I agree that this is more complicated than it
should be; this is just a warning.)

>  I get back an exception which says:-
>
>  Exception in thread "main" <openjpa-1.0.2-r420667:627158 fatal general error>
>  org.apache.openjpa.persistence.PersistenceException: null

That's definitely not a great error.

-Patrick

On Tue, Apr 1, 2008 at 7:04 AM, David Goodenough
<da...@btconnect.com> wrote:
> I am relatively new to JPA, and trying to get to grips with what I can
>  and can not do with it.
>
>  So I thought I would try creating a small system (which will grow) which
>  has amongst other things User objects and Role objects in it.
>
>  OpenJPA has successfully taken my list of Entities and created the
>  relevant tables.  The Entity classes have all been enhanced.  I am using
>  OpenJPA 1.0.2.
>
>  I create a Role object (which does not contain a reference to other
>  objects) and persist it.  No errors are reported, but the object does
>  not appear as a record in the DB.  Does a persist have to be inside
>  a Transaction?  The manual is confusing as it says that "this action can
>  only be performed in the context of an active transaction" right at the
>  end of the description of persist, but I read it (given the way it was
>  laid out) to apply to the remove action which immediately follows it.
>
>  Then create a User object, which amongst other things has a List of
>  Role objects in it, so I add the Role object I just create to it, fill in all
>  the other fields, and try to persist it.
>
>  I get back an exception which says:-
>
>  Exception in thread "main" <openjpa-1.0.2-r420667:627158 fatal general error>
>  org.apache.openjpa.persistence.PersistenceException: null
>         at
>  org.apache.openjpa.meta.ProxySetupStateManager.providedStringField(ProxySetupStateManager.java:234)
>         at uk.co.dga.bm.jpa.User.pcProvideField(User.java)
>         at
>  org.apache.openjpa.meta.ProxySetupStateManager.setProxyData(ProxySetupStateManager.java:58)
>         at
>  org.apache.openjpa.meta.ClassMetaData.resolveMeta(ClassMetaData.java:1731)
>         at
>  org.apache.openjpa.meta.ClassMetaData.resolve(ClassMetaData.java:1613)
>         at
>  org.apache.openjpa.meta.MetaDataRepository.processBuffer(MetaDataRepository.java:675)
>         at
>  org.apache.openjpa.meta.MetaDataRepository.resolveMeta(MetaDataRepository.java:575)
>         at
>  org.apache.openjpa.meta.MetaDataRepository.resolve(MetaDataRepository.java:500)
>         at
>  org.apache.openjpa.meta.MetaDataRepository.getMetaData(MetaDataRepository.java:302)
>         at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2372)
>         at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2225)
>         at
>  org.apache.openjpa.kernel.DelegatingBroker.persist(DelegatingBroker.java:1005)
>         at
>  org.apache.openjpa.persistence.EntityManagerImpl.persist(EntityManagerImpl.java:541)
>         at uk.co.dga.bm.loader.CreateUser.run(CreateUser.java:34)
>         at uk.co.dga.bm.loader.CreateUser.main(CreateUser.java:41)
>
>  which is not very helpful.  How do I get more information on exactly what it
>  is trying to do so that I can fix it?
>
>  David
>



-- 
Patrick Linskey
202 669 5907

Re: How to diagnose rather cryptic error messages

Posted by Nino Saturnino Martinez Vazquez Wael <ni...@jayway.dk>.
Hmm, could you provider the persistance.xml?

David Goodenough wrote:
> I am relatively new to JPA, and trying to get to grips with what I can 
> and can not do with it.
>
> So I thought I would try creating a small system (which will grow) which
> has amongst other things User objects and Role objects in it.
>
> OpenJPA has successfully taken my list of Entities and created the
> relevant tables.  The Entity classes have all been enhanced.  I am using
> OpenJPA 1.0.2.
>
> I create a Role object (which does not contain a reference to other 
> objects) and persist it.  No errors are reported, but the object does
> not appear as a record in the DB.  Does a persist have to be inside 
> a Transaction?  The manual is confusing as it says that "this action can
> only be performed in the context of an active transaction" right at the
> end of the description of persist, but I read it (given the way it was 
> laid out) to apply to the remove action which immediately follows it.
>
> Then create a User object, which amongst other things has a List of
> Role objects in it, so I add the Role object I just create to it, fill in all 
> the other fields, and try to persist it.  
>
> I get back an exception which says:-
>
> Exception in thread "main" <openjpa-1.0.2-r420667:627158 fatal general error> 
> org.apache.openjpa.persistence.PersistenceException: null
>         at 
> org.apache.openjpa.meta.ProxySetupStateManager.providedStringField(ProxySetupStateManager.java:234)
>         at uk.co.dga.bm.jpa.User.pcProvideField(User.java)
>         at 
> org.apache.openjpa.meta.ProxySetupStateManager.setProxyData(ProxySetupStateManager.java:58)
>         at 
> org.apache.openjpa.meta.ClassMetaData.resolveMeta(ClassMetaData.java:1731)
>         at 
> org.apache.openjpa.meta.ClassMetaData.resolve(ClassMetaData.java:1613)
>         at 
> org.apache.openjpa.meta.MetaDataRepository.processBuffer(MetaDataRepository.java:675)
>         at 
> org.apache.openjpa.meta.MetaDataRepository.resolveMeta(MetaDataRepository.java:575)
>         at 
> org.apache.openjpa.meta.MetaDataRepository.resolve(MetaDataRepository.java:500)
>         at 
> org.apache.openjpa.meta.MetaDataRepository.getMetaData(MetaDataRepository.java:302)
>         at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2372)
>         at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2225)
>         at 
> org.apache.openjpa.kernel.DelegatingBroker.persist(DelegatingBroker.java:1005)
>         at 
> org.apache.openjpa.persistence.EntityManagerImpl.persist(EntityManagerImpl.java:541)
>         at uk.co.dga.bm.loader.CreateUser.run(CreateUser.java:34)
>         at uk.co.dga.bm.loader.CreateUser.main(CreateUser.java:41)
>
> which is not very helpful.  How do I get more information on exactly what it 
> is trying to do so that I can fix it?
>
> David
>
>
>   

-- 
-Wicket for love

Nino Martinez Wael
Java Specialist @ Jayway DK
http://www.jayway.dk
+45 2936 7684