openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Copeland <t...@jotobjects.com>
Subject Re: Identity class and parent/children entity relationship
Date Thu, 09 Apr 2009 05:44:23 GMT
Hi Fay -

Found my error here - my case now works on 1.2.1.  I had a mapping error 
that caused the reentrant flush exception -- this is probably not the 
problem fixed by JIRA-1004. 

This is the story - I have a class with a OneToMany relationship to 
itself (parent has a collection of children of the same type).  My 
mistake was to mark the "optional" attribute false.  Obviously somebody 
is the granddaddy with no parent or you have a pathological cycle.  So 
this works now -

@Table (name="product_category")
public class ProductCategory
    implements java.io.Serializable
{
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Id private long id;

    @OrderBy
    @OneToMany(mappedBy="parentCategory", fetch=FetchType.LAZY,
                          cascade={CascadeType.PERSIST,CascadeType.REMOVE})
    private List<ProductCategory> subCategories;

    @ManyToOne(fetch=FetchType.LAZY,cascade=CascadeType.PERSIST)
    private ProductCategory parentCategory;
}


On 4/7/2009 2:26 PM, Paul Copeland wrote:
> I also have a hard requirement to work with 1.x.x OpenJPA (JPA 1.0) 
> for a production delivery same as Nemanja.
>
> So I am willing to jump through hoops for a work around even it means 
> changing the identity type or doing lots of flushes or something else 
> like that.  Just wondering what my options are.
>
> I already tried doing flushes. Maybe I need to do a refresh too?
>
> - Paul
>
> On 4/7/2009 1:51 PM, Fay Wang wrote:
>> The fix is in JIRA-1004. I only checked in the fix to trunk, not 
>> 1.2.x, though.
>>
>> -Fay
>>
>>
>>
>> ----- Original Message ----
>> From: Paul Copeland <tech@jotobjects.com>
>> To: users@openjpa.apache.org
>> Sent: Tuesday, April 7, 2009 12:34:05 PM
>> Subject: Re: Identity class and parent/children entity relationship
>>
>> Is there a workaround for 1.2.1?
>>
>> I'm also hitting the reentrant flush exception with 
>> GenerationType.IDENTITY
>>
>>
>> On 3/30/2009 10:26 AM, Nemanja Joksovic wrote:
>>  
>>> Thanks Fay,
>>>
>>> I tried it before and it's working fine. But in production 
>>> environment I need to using stable OpenJPA 1.2.x version (JPA 1.0 
>>> specification).
>>>
>>> Regards,
>>> Nemanja J.
>>>
>>>
>>>
>>> The workaround is to use MappedById annotation (JPA 2.0 spec) in 
>>> Openjpa trunk:
>>>
>>> @Entity
>>> public class Child implements Serializable {
>>>
>>>     @EmbeddedId
>>>     @Column(name = "id", unique = false, nullable = false)
>>>     private ChildId childId;
>>>
>>>     @MappedById("parent")
>>>     @ManyToOne
>>>     @JoinColumn(name = "parent_id", referencedColumnName = "id")
>>>     private Parent parent;
>>>
>>> ...
>>> }
>>>
>>>
>>> @Embeddable
>>> public class ChildId {
>>>     public long id;
>>>     public long parent;
>>>    ...}
>>>
>>>
>>> @Entity
>>> public class Parent {
>>>     @Id
>>>     @GeneratedValue(strategy = GenerationType.IDENTITY)
>>>     @Column(name = "id", unique = true, insertable = false, 
>>> updatable = false, nullable = false)
>>>     private long id;
>>>         @OneToMany(cascade = CascadeType.ALL, mappedBy = "parent")
>>>     private List<Child> children = new ArrayList<Child>();
>>>         private String name;
>>>    ...}
>>>
>>>
>>> For more detail, please see
>>> org.apache.openjpa.persistence.enhance.identity.TestMappedById.testMappedById5

>>>
>>>
>>> -Fay
>>>
>>> --- On Mon, 3/30/09, Nemanja Joksovic <joksovicn@gmail.com> wrote:
>>>
>>>  
>>>    
>>>> From: Nemanja Joksovic <joksovicn@gmail.com>
>>>> Subject: Re: Identity class and parent/children entity relationship
>>>> To: users@openjpa.apache.org
>>>> Date: Monday, March 30, 2009, 9:56 AM
>>>> Thank you very much.
>>>>
>>>> Nemanja J.
>>>>
>>>>
>>>>
>>>> The second problem is a known problem. I will open a JIRA
>>>> for it.
>>>>
>>>>
>>>> --- On Sun, 3/29/09, Nemanja Joksovic
>>>> <joksovicn@gmail.com> wrote:
>>>>
>>>>         
>>>>> From: Nemanja Joksovic <joksovicn@gmail.com>
>>>>> Subject: Identity class and parent/children entity
>>>>>              
>>>> relationship
>>>>         
>>>>> To: users@openjpa.apache.org
>>>>> Date: Sunday, March 29, 2009, 5:33 AM
>>>>> Hi all,
>>>>>
>>>>> I've been experiencing few problems with Identity
>>>>>              
>>>> class
>>>>         
>>>>> and Parent/Children entity relationship. I tried with
>>>>>              
>>>> both
>>>>         
>>>>> OpenJPA 1.2.1 and 2.0.0-SNAPSHOT. I have a very simple
>>>>>              
>>>> test
>>>>         
>>>>> case which can briefly explain problems:
>>>>>
>>>>> @Entity
>>>>> public class Parent implements Serializable {
>>>>>
>>>>>     private long id;
>>>>>     ....
>>>>>     private List<Child> children = new
>>>>> LinkedList<Child>();
>>>>>
>>>>>     @Id
>>>>> //  @GeneratedValue(strategy =
>>>>>              
>>>> GenerationType.IDENTITY)
>>>>         
>>>>>     @Column(name = "id", unique = true,
>>>>> insertable = false, updatable = false, nullable =
>>>>>              
>>>> false)
>>>>         
>>>>>     public long getId() {
>>>>>         return id;
>>>>>     }
>>>>>
>>>>>     public void setId(long id) {
>>>>>        this.id = id;
>>>>>     }
>>>>>
>>>>>     ...
>>>>>
>>>>>     @OneToMany(cascade = CascadeType.ALL, mappedBy =
>>>>> "parent")
>>>>>     public List<Child> getChildren() {
>>>>>         return children;
>>>>>     }
>>>>>
>>>>>     public void addChild(Child child) {
>>>>>         if (child == null) {
>>>>>             throw new
>>>>>              
>>>> IllegalArgumentException("Cannot
>>>>         
>>>>> add a null Child");
>>>>>         }
>>>>>         this.getChildren().add(child);
>>>>>     }
>>>>>
>>>>>     public void setChildren(List<Child>
>>>>>              
>>>> children) {
>>>>         
>>>>>         this.children = children;
>>>>>     }
>>>>> }
>>>>>
>>>>> @Entity
>>>>> @IdClass(ChildId.class)
>>>>> public class Child implements Serializable {
>>>>>
>>>>>     private long id;
>>>>>     ...
>>>>>     private Parent parent;
>>>>>
>>>>>     @Id
>>>>>     @Column(name = "id", unique = false,
>>>>>              
>>>> nullable
>>>>         
>>>>> = false)
>>>>>     public long getId() {
>>>>>         return id;
>>>>>     }
>>>>>
>>>>>     public void setId(long id) {
>>>>>         this.id = id;
>>>>>     }
>>>>>
>>>>>     ...
>>>>>
>>>>>     @Id
>>>>>     @ManyToOne
>>>>>     @JoinColumn(name = "parent_id",
>>>>> referencedColumnName = "id")
>>>>>     public Parent getParent() {
>>>>>         return parent;
>>>>>     }
>>>>>
>>>>>     public void setParent(Parent parent) {
>>>>>         this.parent = parent;
>>>>>     }
>>>>> }
>>>>>
>>>>> ChidId is generated with the Application Identity
>>>>>              
>>>> Tool:
>>>>         
>>>>> public class ChildId implements Serializable {
>>>>>     ...
>>>>>     public long id;
>>>>>     public long parent;
>>>>>     ...
>>>>> }
>>>>>
>>>>> 1) Merge operation fail in any case with following
>>>>> exception:
>>>>>
>>>>> java.lang.NullPointerException
>>>>>     at
>>>>>
>>>>>              
>>>> net.company.persistence.Child.pcCopyKeyFieldsFromObjectId(Child.java)
>>>>         
>>>>>     at
>>>>>
>>>>>              
>>>> net.company.persistence.Child.pcNewInstance(Child.java)
>>>>         
>>>>>     at
>>>>>
>>>>>              
>>>> org.apache.openjpa.kernel.AttachStrategy.persist(AttachStrategy.java:93)

>>>>
>>>>         
>>>>>     at
>>>>>
>>>>>              
>>>> org.apache.openjpa.kernel.VersionAttachStrategy.attach(VersionAttachStrategy.java:100)

>>>>
>>>>         
>>>>>     at
>>>>>
>>>>>              
>>>> org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:241)
>>>>         
>>>>>     at
>>>>>
>>>>>              
>>>> org.apache.openjpa.kernel.AttachStrategy.attachCollection(AttachStrategy.java:333)

>>>>
>>>>         
>>>>>     at
>>>>>
>>>>>              
>>>> org.apache.openjpa.kernel.AttachStrategy.replaceList(AttachStrategy.java:359)

>>>>
>>>>         
>>>>>     at
>>>>>
>>>>>              
>>>> org.apache.openjpa.kernel.AttachStrategy.attachField(AttachStrategy.java:223)

>>>>
>>>>         
>>>>>     at
>>>>>
>>>>>              
>>>> org.apache.openjpa.kernel.VersionAttachStrategy.attach(VersionAttachStrategy.java:153)

>>>>
>>>>         
>>>>>     at
>>>>>
>>>>>              
>>>> org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:241)
>>>>         
>>>>>     at
>>>>>
>>>>>              
>>>> org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:101)
>>>>         
>>>>>     at
>>>>>
>>>>>              
>>>> org.apache.openjpa.kernel.BrokerImpl.attach(BrokerImpl.java:3212)
>>>>         
>>>>>     ... 25 more
>>>>>
>>>>>
>>>>> The problem is caused by the call
>>>>> pc.NewInstance(AttachStrategy:93) with null state
>>>>>              
>>>> manager
>>>>         
>>>>> instance which is used in
>>>>>              
>>>> pcCopyKeyFieldsFromObjectId(..):
>>>>         
>>>>>              
>>>> pcsetParent((Parent)pcStateManager.getPCPrimaryKey(childid,
>>>>         
>>>>> 2 + pcInheritedFieldCount));
>>>>>
>>>>> Also, for same problem exists JIRA Issue:
>>>>> https://issues.apache.org/jira/browse/OPENJPA-218
>>>>>
>>>>>
>>>>> 2) Persist working fine, but it's fail in the case
>>>>>              
>>>> when
>>>>         
>>>>> Parent.id is Generated Value field with following
>>>>>              
>>>> exception:
>>>>         
>>>>> <openjpa-1.2.1-r752877:753278 fatal user error>
>>>>> org.apache.openjpa.persistence.InvalidStateException:
>>>>> Detected reentrant flush.  Make sure your flush-time
>>>>> instance callback methods or event listeners do not
>>>>>              
>>>> invoke
>>>>         
>>>>> any operations that require the in-progress flush to
>>>>> complete.
>>>>>     at
>>>>>
>>>>>              
>>>> org.apache.openjpa.kernel.BrokerImpl.flushSafe(BrokerImpl.java:1904)
>>>>         
>>>>>     at
>>>>>
>>>>>              
>>>> org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:1679)
>>>>         
>>>>>     at
>>>>>
>>>>>              
>>>> org.apache.openjpa.kernel.StateManagerImpl.assignObjectId(StateManagerImpl.java:524)

>>>>
>>>>         
>>>>>     at
>>>>>
>>>>>              
>>>> org.apache.openjpa.kernel.StateManagerImpl.assignObjectId(StateManagerImpl.java:506)

>>>>
>>>>         
>>>>>     at
>>>>>
>>>>>              
>>>> org.apache.openjpa.kernel.StateManagerImpl.fetchObjectId(StateManagerImpl.java:1434)

>>>>
>>>>         
>>>>>     at
>>>>>
>>>>>              
>>>> net.company.persistence.Parent.pcFetchObjectId(Parent.java)
>>>>         
>>>>>     at
>>>>>
>>>>>              
>>>> net.company.persistence.Child.pcCopyKeyFieldsToObjectId(Child.java)
>>>>         
>>>>> Does exists some workaround for these problems ?
>>>>>
>>>>> Regards,
>>>>> Nemanja J.
>>>>> -- 
>>>>> View this message in context:
>>>>>
>>>>>              
>>>> http://n2.nabble.com/Identity-class-and-parent-children-entity-relationship-tp2552430p2552430.html

>>>>
>>>>         
>>>>> Sent from the OpenJPA Users mailing list archive at
>>>>> Nabble.com.
>>>>>              
>>>>     
>>>>
>>>>
>>>> -- 
>>>> View this message in context:
>>>> http://n2.nabble.com/Identity-class-and-parent-children-entity-relationship-tp2552430p2558373.html

>>>>
>>>> Sent from the OpenJPA Users mailing list archive at
>>>> Nabble.com.
>>>>          
>>>     
>>>
>>>
>>>  
>>>     
>>
>>
>>      
>>
>>
>>   
>
>
>


Mime
View raw message