-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Immutable list for Association causes Exception on Merge
PostPosted: Fri Jun 16, 2006 5:09 pm 
Beginner
Beginner

Joined: Mon Jan 09, 2006 3:05 pm
Posts: 24
Hibernate version:
* Hibernate Core 3.2.0.CR2
* Hibernate Annotation 3.2.0.CR1
* Hibernate Entity Manager 3.2.0.CR1

Mapping documents:

Annotated entity classes Parent and Child, in a Many to Many bidirectional relationship with Child being the owning side. These are as vanilla as you can get, but code available on request.

Code between emf.createEntityManager() and em.close():
Code:
        em.getTransaction().begin();       
        Parent p = new Parent();
        em.persist(p);       
        Child c = new Child(Arrays.asList(p));
        em.persist(c);               
        em.getTransaction().commit();   
     
        em.merge(c);


Description of Problem
In a Java 5 SE environment, which runs in an extended persistence context, executing the above code causes an exception when JPA/hibernate tries to modify the immutable list returned by Arrays.asList(). The standard says:
Quote:
If X is a managed entity, it is ignored by the merge operation

which obviously isn't happening.

Obviously, the merge above isn't necessary since it is all within the same persistence context, but in general what restrictions, if any, are put on the POJOs that you hand to the entity manager?

Full stack trace of any exception that occurs:
Code:
java.lang.UnsupportedOperationException
   at java.util.AbstractList.remove(Unknown Source)
   at java.util.AbstractList$Itr.remove(Unknown Source)
   at java.util.AbstractList.removeRange(Unknown Source)
   at java.util.AbstractList.clear(Unknown Source)
   at org.hibernate.collection.PersistentBag.clear(PersistentBag.java:344)
   at org.hibernate.type.CollectionType.replaceElements(CollectionType.java:404)
   at org.hibernate.type.CollectionType.replace(CollectionType.java:449)
   at org.hibernate.type.TypeFactory.replace(TypeFactory.java:431)
   at org.hibernate.event.def.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:282)
   at org.hibernate.event.def.DefaultMergeEventListener.entityIsPersistent(DefaultMergeEventListener.java:132)
   at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:105)
   at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:51)
   at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:679)
   at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:663)
   at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:667)
   at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:201)
   at com.acompany.persistence.TestJPA.testMerge(TestJPA.java:60)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 09, 2006 7:05 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I'm having hard time understanding your mapping, can you provide PArent and Child mapping?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 10, 2006 10:43 am 
Beginner
Beginner

Joined: Mon Jan 09, 2006 3:05 pm
Posts: 24
Welcome back Emmanuel. We missed you!

Following are the parent child classes with embedded mapping annotations. They are straightforward. I think the real problem is the Immutable list holding the parents.

Parent.java
Code:
@Entity
public class Parent {
   
    private long id;
    private Collection<Child> children;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public long getID() {
        return id;
    }
    public void setID(long id) {
        this.id = id;
    }

    @ManyToMany(mappedBy="parents")
    public Collection<Child> getChildren() {
        return children;
    }
    public void setChildren(Collection<Child> kids) {
        this.children = kids;       
    }
} // Parent


Child.java
Code:
@Entity
public  class Child {
   
    private long id;
    private Collection<Parent> parents;
   
    /**
     * Satisfy the JavaBean contract
     *
     */
    public Child() {
    }
   
    public Child(Collection<Parent> parents) {
        setParents(parents);
    }
   
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
   
    @ManyToMany()
    public Collection<Parent> getParents() {
        return parents;
    }
    public void setParents(Collection<Parent> parents) {       
        this.parents = parents;
    }

} // Child


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 16, 2006 12:23 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Year, I know I was (and still am) very busy working. But I'll try to come every now and then here. I like to get feedbacks from you guys.

http://opensource.atlassian.com/projects/hibernate/browse/HHH-1914

Not sure this will be fixed though, because your use case is really weird. If you don't want the colelction to be changed, then mark the columns as updatable=false

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 17, 2006 10:32 am 
Beginner
Beginner

Joined: Mon Jan 09, 2006 3:05 pm
Posts: 24
It is a strange use case, and probably not worth fixing unless someone can come up with a better reason.
--keenan


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.