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.