Hi,
I desperately need help. I couldn't work out a simple example of hibernate .
I am using oracle as a database. Here is my hbm.xml file
<hibernate-mapping>
<class name="events.Event" table="EVENTS">
<id name="id" column="EVENT_ID" type="integer" unsaved-value="0">
<generator class="increment"/>
</id>
<property name="dt" type="timestamp" column="EVENT_DATE"/>
<property name="title" column="TITLE"/>
</class>
</hibernate-mapping>
the class file doing transactions :
/*
* EventManager.java
*
* Created on 12 January 2006, 10:33
*/
package events;
import org.hibernate.Session;
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();
long id = 101;
Event eve = new Event();
System.out.println("INSIDE LIST... ");
if (args[0].equals("store")) {
mgr.createAndStoreEvent("My Event", new Date());
}
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 void createAndStoreEvent(String title, Date theDate) {
try {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDt(theDate);
session.save(theEvent);
session.update(theEvent);
System.out.println("event id : "+theEvent.getId());
System.out.println("event title : "+theEvent.getTitle());
session.getTransaction().commit();
session.connection().commit();
//session.connection().createStatement().execute("SHUTDOWN");
}catch(Exception e){}
}
private List listEvents() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result = session.createQuery("from Event").list();
session.getTransaction().commit();
return result;
}
}
I am not getting any exceptions. But the values not been stored into the table. Please help me with it. Anybody have an idea ?
Thanks,
JSV.
|