I am doing a one-to-many mapping. I have two tables event and person and person_event. I am successful in adding values to tables event and person. And from those table am getting eventId and personId and try to store in person_event table.
The class for the transaction is :
/*
* EventManager.java
*
* Created on 12 January 2006, 10:33
*/
package events;
import org.hibernate.Session;
import org.hibernate.Transaction;
import java.util.Date;
import java.util.List;
import util.HibernateUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class EventManager {
private static Log log = LogFactory.getLog(HibernateUtil.class);
public static void main(String[] args) {
EventManager mgr = new EventManager();
int age = 10;
if (args[0].equals("store")) {
Long eventId = mgr.createAndStoreEvent("Second Event", new Date());
Long personId = mgr.createAndStorePerson(age, "Foo", "Bar");
System.out.println("event id : "+eventId);
System.out.println("person id : "+personId);
mgr.addPersonToEvent(personId, eventId);
}
else if (args[0].equals("list")) {
List events = mgr.listEvents();
for (int i = 0; i < events.size(); i++) {
Event theEvent = (Event) events.get(i);
System.out.println("Event: " + theEvent.getTitle() +" Time: " + theEvent.getDt());
}
}
HibernateUtil.getSessionFactory().close();
}
private Long createAndStoreEvent(String title, Date theDate) {
Long eventId = null;
try {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDt(theDate);
session.save(theEvent);
eventId = theEvent.getId();
System.out.println("getconnected "+session.getTransaction());
session.connection().commit();
session.flush();
session.getTransaction().commit();
System.out.println("transaction committed...");
}catch(Exception e){System.out.println(""+e.getMessage());}
return eventId;
}
private Long createAndStorePerson(int age,String fname, String lname) {
Long personId = null;
try {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Person thePerson = new Person();
thePerson.setAge(age);
thePerson.setFirstName(fname);
thePerson.setLastName(lname);
session.save(thePerson);
personId = thePerson.getId();
System.out.println("getconnected person "+session.getTransaction());
session.connection().commit();
session.flush();
session.getTransaction().commit();
System.out.println("transaction committed for person ...");
}catch(Exception e){System.out.println(""+e.getMessage());}
return personId;
}
private List listEvents() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result = session.createQuery("from Event").list();
session.getTransaction().commit();
return result;
}
private void addPersonToEvent(Long personId,Long eventId) { try { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Person p = (Person) session.load(Person.class, personId); Event e = (Event) session.load(Event.class,eventId); p.getEvents().add(e); //session.save(p); session.getTransaction().commit(); }catch(Exception ee){System.out.println("exception ee :"+ee.getMessage());} }
}
Please tell me where i got mistaken .
Thanks,
|