I made the following from the hibernate reference doc. while there were no exceptions, i expected that when i check the Tables created, i will find 4 tables from calling show tables from MySQL but only PERSON table and EVENT tables were created. I expected PERSON_EVENT and PERSON_EMAIL_ADDR. So why are my having two tables?
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
addEvent("Swimming",new Date());
addPerson("Jow", "Babe" , "24");
addPersonToEvent(new Long(1),new Long(1),out);
addEmailToPerson(Long.valueOf("1"), "
[email protected]");
out.write("Person Added");
out.close();
}
private void addPerson(String fname, String lname, String age){
Session session = HibernateUtil.currentSession();
session = HibernateUtil.currentSession();
session.beginTransaction();
Person aPerson = new Person();
aPerson.setFirstname(fname);
aPerson.setLastname(lname);
aPerson.setAge(age);
session.save(aPerson);
session.getTransaction().commit();
}
private void addEvent(String title, Date date){
Session session = HibernateUtil.currentSession();
session.beginTransaction();
Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(date);
session.save(theEvent);
session.getTransaction().commit();
}
private List listEvents() {
Session session = HibernateUtil.currentSession();
session.beginTransaction();
List result = session.createQuery("from Event").list();
session.getTransaction().commit();
return result;
}
private void addPersonToEvent(Long personId, Long eventId, PrintWriter out) {
Session session = HibernateUtil.currentSession();
session.beginTransaction();
Person aPerson = (Person) session.load(Person.class, personId);
Event anEvent = (Event) session.load(Event.class, eventId);
Set s = new HashSet();
s.add(anEvent);
aPerson.setEvents(s);
session.save(aPerson);
session.getTransaction().commit();
}
private void addEmailToPerson(Long personId, String emailAddress) {
Session session = HibernateUtil.currentSession();
session.beginTransaction();
Person aPerson = (Person) session.load(Person.class, personId);
aPerson.getEmailAddresses().add(emailAddress);
session.getTransaction().commit();
}
Hibernate: select person0_.PERSON_ID as PERSON1_2_0_, person0_.age as age2_0_, person0_.firstname as firstname2_0_, person0_.lastname as lastname2_0_ from PERSON person0_ where person0_.PERSON_ID=?