Source code: events/EventManager.java
1 package events;
2 import org.hibernate.*;
3 import org.hibernate.criterion.Expression;
4
5 import java.util.*;
6
7 import util.HibernateUtil;
8
9 public class EventManager {
10
11 public static void main(String[] args) {
12 EventManager mgr = new EventManager();
13
14 if (args[0].equals("store")) {
15 mgr.createAndStoreEvent("My Event", new Date());
16 }
17 else if (args[0].equals("list")) {
18 List events = mgr.listEvents();
19 for (int i = 0; i < events.size(); i++) {
20 Event theEvent = (Event) events.get(i);
21 System.out.println("Event: " + theEvent.getTitle() +
22 " Time: " + theEvent.getDate());
23 }
24 }
25 else if (args[0].equals("addpersontoevent")) {
26 Long eventId = mgr.createAndStoreEvent("My Event", new Date());
27 Long personId = mgr.createAndStorePerson("Foo", "Bar");
28 mgr.addPersonToEvent(personId, eventId);
29 System.out.println("Added person " + personId + " to event " + eventId);
30 }
31 else if (args[0].equals("addemailtoperson")) {
32 Long personId = mgr.createAndStorePerson("Foozy", "Beary");
33 mgr.addEmailToPerson(personId, "foo@bar");
34 mgr.addEmailToPerson(personId, "bar@foo");
35 System.out.println("Added two email addresses (value typed objects) to person entity : " + personId);
36 }
37
38 HibernateUtil.getSessionFactory().close();
39 }
40
41 private Long createAndStoreEvent(String title, Date theDate) {
42
43 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
44 session.beginTransaction();
45
46 Event theEvent = new Event();
47 theEvent.setTitle(title);
48 theEvent.setDate(theDate);
49
50 session.save(theEvent);
51
52 session.getTransaction().commit();
53
54 return theEvent.getId();
55 }
56
57 private Long createAndStorePerson(String firstname, String lastname) {
58
59 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
60 session.beginTransaction();
61
62 Person thePerson = new Person();
63 thePerson.setFirstname(firstname);
64 thePerson.setLastname(lastname);
65
66 session.save(thePerson);
67
68 session.getTransaction().commit();
69
70 return thePerson.getId();
71 }
72
73
74 private List listEvents() {
75
76 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
77 session.beginTransaction();
78
79 List result = session.createQuery("from Event").list();
80
81 session.getTransaction().commit();
82
83 return result;
84 }
85
86 private void addPersonToEvent(Long personId, Long eventId) {
87
88 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
89 session.beginTransaction();
90
91 Person aPerson = (Person) session
92 .createQuery("select p from Person p left join fetch p.events where p.id = :pid")
93 .setParameter("pid", personId)
94 .uniqueResult(); // Eager fetch the collection so we can use it detached
95
96 Event anEvent = (Event) session.load(Event.class, eventId);
97 // If we want to handle it bidirectional and detached, we also need to load this
98 // collection with an eager outer-join fetch, this time with Criteria and not HQL:
99 /*
100 Event anEvent = (Event) session
101 .createCriteria(Event.class).setFetchMode("participants", FetchMode.JOIN)
102 .add( Expression.eq("id", eventId) )
103 .uniqueResult(); // Eager fetch the colleciton so we can use it detached
104 */
105
106 session.getTransaction().commit();
107
108 // End of first unit of work
109
110 aPerson.getEvents().add(anEvent); // aPerson is detached
111 // or bidirectional safety method, setting both sides: aPerson.addToEvent(anEvent);
112
113 // Begin second unit of work
114
115 Session session2 = HibernateUtil.getSessionFactory().getCurrentSession();
116 session2.beginTransaction();
117
118 session2.update(aPerson); // Reattachment of aPerson
119
120 session2.getTransaction().commit();
121 }
122
123 private void addEmailToPerson(Long personId, String emailAddress) {
124
125 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
126 session.beginTransaction();
127
128 Person aPerson = (Person) session.load(Person.class, personId);
129
130 // The getEmailAddresses() might trigger a lazy load of the collection
131 aPerson.getEmailAddresses().add(emailAddress);
132
133 session.getTransaction().commit();
134 }
135
136 }