Hi,
I expanded the mapping tutorial (
http://www.hibernate.org/hib_docs/v3/re ... sociations)and run in some problems. Perhaps you can help me.
I have three database tables like described in the tutorial:
- person
- event
- person_event
I want to list all events of a person.
Here is my mapping file Person.hbm.xml:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="events.Person" table="person">
<id name="id" column="person_id">
<generator class="native"/>
</id>
<property name="age"/>
<property name="firstname"/>
<property name="lastname"/>
<set name="events" table="person_event">
<key column="person_id"/>
<many-to-many column="event_id" class="events.Event" lazy="false"/>
</set>
</class>
</hibernate-mapping>
This is the method where I want to list all events of all Persons. The problem is that I get an error when I want to get a Set of events belonging to the person.
Perhaps you can help me (I am a hibernate newbie :) )
Here is my method code:
Code:
private List listPersons() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result = session.createQuery("from Person").list();
for (int i = 0; i < result.size(); i++) {
Person aPerson = (Person) result.get(i);
System.out.println("Person: " + aPerson.getFirstname() + " " + aPerson.getLastname() + " participates in the following events:");
HashSet pSet = (HashSet) aPerson.getEvents();
System.out.println("Event count: " + aPerson.getEvents().toArray().length);
}
session.getTransaction().commit();
return result;
}
Furthermore here are the two classes of event and person:
Class person:
Code:
public class Person {
private Long id;
private int age;
private String firstname;
private String lastname;
private Set events = new HashSet();
//getter and setter methods
Class Event:
Code:
private Long id;
private String title;
private Date date;
//getter and setter methods
Regards,
TMK