-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: one-to-many problem
PostPosted: Wed Jan 18, 2006 10:08 am 
Beginner
Beginner

Joined: Fri Jan 13, 2006 8:07 am
Posts: 29
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,


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 19, 2006 5:00 am 
Beginner
Beginner

Joined: Fri Jan 13, 2006 8:07 am
Posts: 29
I solved the problem.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.