Here is the scenario:
Suppose a Person is associated with a set of Events:
public class Person{
public Long id;
public int age;
public String firstName;
public String lastName;
public Set<Event> events = new HashSet<Event>();
}
public class Event{
private String title;
private Date date;
}
And then here is the mapping file for the Person (Event is a value type so it doesn't have mapping file)
<class name="Person" table="person">
<id name="id">
<generator class="native" />
</id>
<property name="age" />
<property name="firstName" />
<property name="lastName" />
<set name="events">
<key />
<composite-element class="Event">
<property name="title" />
<property name="date" />
</composite-element>
</set>
</class>
At last, here is the test function. As you can see, we modify nothing about the database, only queries:
Public Class Test{
private void loadEvent(){
Sessionsession=
HibernateUtil.getSessionFactory().getCurrentSession();
Transaction t = session.getTransaction();
t.begin();
Person p = (Person)session.get(Person.class, 1l);
p.getEvents().size(); //we only want to get the event size
t.commit();
}
}
When run this test, hibernate will reports the collection event of Person class is dirty and then:
1.delete all events from the table
2.reinsert them
Am I missing something? and any solutions for that problem? Thanks a lot!
|