-->
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.  [ 1 post ] 
Author Message
 Post subject: Collection content is many times the same entity
PostPosted: Tue May 05, 2015 2:18 pm 
Newbie

Joined: Tue May 05, 2015 2:10 pm
Posts: 1
My code is involving 3 entities:
Code:
   @Entity
   public class Entity1 implements Serializable {
      ...
      @OneToMany(cascade={CascadeType.ALL},fetch = FetchType.EAGER, targetEntity = Entity3.class,mappedBy = "entity1")
      private Collection<Entity3> entity3;   
      
      @OneToMany(cascade={CascadeType.ALL},fetch = FetchType.EAGER, targetEntity = Entity2.class,mappedBy = "entity1")
      private Collection<Entity2> entity2;
   }
   
   @Entity
   public class Entity2 implements Serializable {
      ...
      @EmbeddedId
      private Entity2IdPK id;
      
      @ManyToOne(optional=true,fetch = FetchType.EAGER,targetEntity = Entity1.class)
      private Entity1 entity1;
   }
   
   @Entity
   public class Entity3 implements Serializable {
      ...
      @EmbeddedId
      private Entity3IdPK id;
      
      @MapsId("entity1")@ManyToOne(optional=true,targetEntity = Entity1.class)
      private Entity1 entity1;


I'm performing a search using a Spring repository in order to get one instance of Entity1. I know that the element I'm querying from the database has "1 Entity2" elements, and "3 Entity3" element in the database.

When I loop over the collection Entity1.entity3, I find 3 elements, which is normal.

But when I loop over the collection Entity1.entity2, I find 3 elements, which are pointing to the same object, whereas I was expecting only 1 element.

Same result if I add more Entity3 elements, the 2 collections have the same number of elements. This is very strange, because when I try to replicate the same environment, with the same database content, and the same code, on another computer, I have the expected (1 element of Entity2 in the Collection).

Is it a normal behaviour ? Is it a bug in my code, or is it a bug in Hibernate ?


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

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.