001: package events;
002:
003: import org.hibernate.*;
004: import org.hibernate.criterion.Expression;
005:
006: import java.util.*;
007:
008: import util.HibernateUtil;
009:
010: public class EventManager {
011:
012: public static void main(String[] args) {
013: EventManager mgr = new EventManager();
014:
015: if (args[0].equals("store")) {
016: mgr.createAndStoreEvent("My Event", new Date());
017: } else if (args[0].equals("list")) {
018: List events = mgr.listEvents();
019: for (int i = 0; i < events.size(); i++) {
020: Event theEvent = (Event) events.get(i);
021: System.out.println("Event: " + theEvent.getTitle()
022: + " Time: " + theEvent.getDate());
023: }
024: } else if (args[0].equals("addpersontoevent")) {
025: Long eventId = mgr.createAndStoreEvent("My Event",
026: new Date());
027: Long personId = mgr.createAndStorePerson("Foo", "Bar");
028: mgr.addPersonToEvent(personId, eventId);
029: System.out.println("Added person " + personId
030: + " to event " + eventId);
031: } else if (args[0].equals("addemailtoperson")) {
032: Long personId = mgr.createAndStorePerson("Foozy", "Beary");
033: mgr.addEmailToPerson(personId, "foo@bar");
034: mgr.addEmailToPerson(personId, "bar@foo");
035: System.out
036: .println("Added two email addresses (value typed objects) to person entity : "
037: + personId);
038: }
039:
040: HibernateUtil.getSessionFactory().close();
041: }
042:
043: private Long createAndStoreEvent(String title, Date theDate) {
044:
045: Session session = HibernateUtil.getSessionFactory()
046: .getCurrentSession();
047: session.beginTransaction();
048:
049: Event theEvent = new Event();
050: theEvent.setTitle(title);
051: theEvent.setDate(theDate);
052:
053: session.save(theEvent);
054:
055: session.getTransaction().commit();
056:
057: return theEvent.getId();
058: }
059:
060: private Long createAndStorePerson(String firstname, String lastname) {
061:
062: Session session = HibernateUtil.getSessionFactory()
063: .getCurrentSession();
064: session.beginTransaction();
065:
066: Person thePerson = new Person();
067: thePerson.setFirstname(firstname);
068: thePerson.setLastname(lastname);
069:
070: session.save(thePerson);
071:
072: session.getTransaction().commit();
073:
074: return thePerson.getId();
075: }
076:
077: private List listEvents() {
078:
079: Session session = HibernateUtil.getSessionFactory()
080: .getCurrentSession();
081: session.beginTransaction();
082:
083: List result = session.createQuery("from Event").list();
084:
085: session.getTransaction().commit();
086:
087: return result;
088: }
089:
090: private void addPersonToEvent(Long personId, Long eventId) {
091:
092: Session session = HibernateUtil.getSessionFactory()
093: .getCurrentSession();
094: session.beginTransaction();
095:
096: Person aPerson = (Person) session
097: .createQuery(
098: "select p from Person p left join fetch p.events where p.id = :pid")
099: .setParameter("pid", personId).uniqueResult(); // Eager fetch the collection so we can use it detached
100:
101: Event anEvent = (Event) session.load(Event.class, eventId);
102: // If we want to handle it bidirectional and detached, we also need to load this
103: // collection with an eager outer-join fetch, this time with Criteria and not HQL:
104: /*
105: Event anEvent = (Event) session
106: .createCriteria(Event.class).setFetchMode("participants", FetchMode.JOIN)
107: .add( Expression.eq("id", eventId) )
108: .uniqueResult(); // Eager fetch the colleciton so we can use it detached
109: */
110:
111: session.getTransaction().commit();
112:
113: // End of first unit of work
114:
115: aPerson.getEvents().add(anEvent); // aPerson is detached
116: // or bidirectional safety method, setting both sides: aPerson.addToEvent(anEvent);
117:
118: // Begin second unit of work
119:
120: Session session2 = HibernateUtil.getSessionFactory()
121: .getCurrentSession();
122: session2.beginTransaction();
123:
124: session2.update(aPerson); // Reattachment of aPerson
125:
126: session2.getTransaction().commit();
127: }
128:
129: private void addEmailToPerson(Long personId, String emailAddress) {
130:
131: Session session = HibernateUtil.getSessionFactory()
132: .getCurrentSession();
133: session.beginTransaction();
134:
135: Person aPerson = (Person) session.load(Person.class, personId);
136:
137: // The getEmailAddresses() might trigger a lazy load of the collection
138: aPerson.getEmailAddresses().add(emailAddress);
139:
140: session.getTransaction().commit();
141: }
142:
143: }
|