-->
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: Retrieve data from ManyToMany association (non-owing side)
PostPosted: Tue Apr 10, 2012 10:15 am 
Newbie

Joined: Tue Apr 10, 2012 9:42 am
Posts: 1
Hi at all,
I'm new to Hibernate technology, I have a question about retrieving data from ManyToMany association.
I have 2 simple class:

Code:
@Entity
@Table(name="PERSON")
public class Person implements Serializable {

   @Id
   @GeneratedValue
   @Column(name="PERSON_ID")
   private long id;

   @Column(name="AGE", nullable=false)
   private int age;
   
   @Column(name="FIRSTNAME", nullable=false)
   private String firstname;

   @Column(name="LASTNAME", nullable=false)
   private String lastname;

   @ManyToMany(cascade = CascadeType.ALL)
   @JoinTable(name="PERSON_EVENT", joinColumns = {@JoinColumn (name="PERSON_ID")},
   inverseJoinColumns = {@JoinColumn (name="EVENT_ID")})
   private Set<Event> events = new HashSet<Event>();
        ....
        ....
}

// AND

@Entity
@Table(name="EVENT")
public class Event implements Serializable {

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

   @Id
   @GeneratedValue
   @Column(name="EVENT_ID")
   private Long id;
   
   @Column(name="TITLE", nullable=false)
   private String title;
   
   @Column(name="DATE", nullable=false)
   private Date date;
   
   @ManyToMany(targetEntity=Person.class, mappedBy = "events")
        private Set<Person> persons = new HashSet<Person>();
        ....
        ....
}


I want to retrieve data of all Person including Events of each, then I wrote code:
Code:
   public void listPersons() {
      EntityManager eManager = HibernateUtil.getEntityManagerFactory();
      try {
         eManager.getTransaction().begin();
         @SuppressWarnings("unchecked")
         List<Person> persons = eManager.createQuery("FROM Person").getResultList();

         Set<Event> e = null;
         Event event = null;
         Set<String> emails = null;
         String email = null;
         for (Person p : persons) {
            System.out.println("####");
            System.out.println(p.getFirstname());
            System.out.println(p.getLastname());
            System.out.println(p.getAge());

            System.out.println("## Events ##");
            e = p.getEvents();
            for (Iterator<Event> it = e.iterator(); it.hasNext();) {
               event = it.next();
               System.out.println("     "+event.getTitle());
               System.out.println("     "+event.getDate().toString());
            }
            System.out.println("####");
         }
         eManager.getTransaction().commit();
      } catch (HibernateException e) {
         eManager.getTransaction().rollback();
         e.printStackTrace();
      }
   }


and I get all Person object with a Set of all Events. I tried same thing from other side of association, writing following code:

Code:
   public void listAllAttendees(String eventTitle) {
      EntityManager eManager = HibernateUtil.getEntityManagerFactory();
      try {
         eManager.getTransaction().begin();
         @SuppressWarnings("unchecked")
         List<Event> events = eManager.createQuery("FROM Event e JOIN FETCH e.persons WHERE e.title = :title").
                           setParameter("title", eventTitle).
                           getResultList();
         Set<Person> persons = null;
         Person person = null;
         for (Event event : events) {
            System.out.println("## Events ##");
            System.out.println(event.getTitle());
            System.out.println(event.getDate());
            System.out.println("####");
            System.out.println("#### Attendees ####");
            persons = event.getPersons();
            for (Iterator<Person> it = persons.iterator(); it.hasNext();) {
               person = it.next();
               System.out.println("   "+person.getFirstname() + " " + person.getLastname());
            }
            System.out.println("####");
         }
         
         eManager.getTransaction().commit();
      } catch (HibernateException e) {
         eManager.getTransaction().rollback();
         e.printStackTrace();
      }
   }


but, in this case I can't get correct data set, or rather I have my Events objects but reference Persons is empty.
Maybe my mistake is in configuring JPA annotations...

Thanks in advance,
regards,
edcruise.


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.