Source Code Cross Referenced for EventManager.java in  » Database-ORM » hibernate » events » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » hibernate » events 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.