-->
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.  [ 32 posts ]  Go to page Previous  1, 2, 3
Author Message
 Post subject: Re: cannot simultaneously fetch multiple bags
PostPosted: Thu Sep 22, 2011 7:46 am 
Newbie

Joined: Tue May 31, 2011 10:12 am
Posts: 4
Hi, there, thanx for the tips!

I had the same error, and it stoped with Set<E> type (yes, I did use List<E> because I didnt know Set<E> was the right thing to do that).

The strange thing here is that the return collection came with duplicated elements!!!

Let me give you an example. Imagine you have two model classes:

Code:
package test.model;

// Imports goes here

@Entity
public class Parent implements Serializable {

   /**
    *
    */
   private static final long serialVersionUID = 864134769850409275L;

   private Long id;
   private String name;
   private Set<String> someWords;
   private Set<String> someTags;
   private Set<Child> childs;

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   public Long getId() {
      return id;
   }

   public void setId(Long id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   @ElementCollection(fetch = FetchType.EAGER)
   public Set<String> getSomeWords() {
      return someWords;
   }

   public void setSomeWords(Set<String> someWords) {
      this.someWords = someWords;
   }

   @ElementCollection(fetch = FetchType.EAGER)
   public Set<String> getSomeTags() {
      return someTags;
   }

   public void setSomeTags(Set<String> someTags) {
      this.someTags = someTags;
   }

   @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
   public Set<Child> getChilds() {
      return childs;
   }

   public void setChilds(Set<Child> childs) {
      this.childs = childs;
   }

   // Here goes Eclipse generated hashCode, equal and toString methods

}


And also:

Code:
package test.model;

// Imports goes here

@Entity
public class Child implements Serializable {

   /**
    *
    */
   private static final long serialVersionUID = -2993621725021273658L;

   private Long id;
   private String name;

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   public Long getId() {
      return id;
   }

   public void setId(Long id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   // Here goes Eclipse generated hashCode, equal and toString methods

}


At first look, everything looks just fine, but running a simple application such as:

Code:
   public static void main(String[] args) {
      SessionFactory sessionFactory = new Configuration().configure().setProperty("hibernate.hbm2ddl.auto", "create").buildSessionFactory();
      sessionFactory.openSession();
      
      Parent parent = new Parent();
      parent.setName("Parent 1");
      parent.setSomeWords(new HashSet<String>());
      parent.setSomeTags(new HashSet<String>());
      parent.setChilds(new HashSet<Child>());
      
      parent.getSomeWords().add("word1");
      parent.getSomeWords().add("word2");
      
      parent.getSomeTags().add("tag1");
      
      sessionFactory.getCurrentSession().beginTransaction();
      sessionFactory.getCurrentSession().save(parent);
      sessionFactory.getCurrentSession().flush();
      sessionFactory.getCurrentSession().getTransaction().commit();
      
      List<?> results = sessionFactory.openStatelessSession().createCriteria(Parent.class).list();
      for (Object r : results) {
         System.out.println(r);
      }
   }


What you get?? this:

Code:
Parent [id=1, name=Parent 1, someWords=[word1, word2], someTags=[tag1], childs=[]]
Parent [id=1, name=Parent 1, someWords=[word1, word2], someTags=[tag1], childs=[]]


That's right, two copies of the same element!!!

And here is the generated SQL:

Code:
Hibernate: insert into Parent (name) values (?)
Hibernate: insert into Parent_someTags (Parent_id, someTags) values (?, ?)
Hibernate: insert into Parent_someWords (Parent_id, someWords) values (?, ?)
Hibernate: insert into Parent_someWords (Parent_id, someWords) values (?, ?)
Hibernate: select this_.id as id0_1_, this_.name as name0_1_, childs2_.Parent_id as Parent1_0_3_, child3_.id as childs2_3_, child3_.id as id1_0_, child3_.name as name1_0_, sometags4_.Parent_id as Parent1_0_4_, sometags4_.someTags as someTags4_, somewords5_.Parent_id as Parent1_0_5_, somewords5_.someWords as someWords5_ from Parent this_ left outer join Parent_Child childs2_ on this_.id=childs2_.Parent_id left outer join Child child3_ on childs2_.childs_id=child3_.id left outer join Parent_someTags sometags4_ on this_.id=sometags4_.Parent_id left outer join Parent_someWords somewords5_ on this_.id=somewords5_.Parent_id


Anyone knows what is going on???

Tx a lot!!!


Top
 Profile  
 
 Post subject: Re: cannot simultaneously fetch multiple bags
PostPosted: Thu Sep 22, 2011 12:16 pm 
Newbie

Joined: Tue May 31, 2011 10:12 am
Posts: 4
Got it!

I simply added this annotation on my Set's getter:

Code:
@Fetch(FetchMode.SELECT)


And no more stateless sessions for me :)

Tx anyway!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 32 posts ]  Go to page Previous  1, 2, 3

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.