Hi,
I am new to Hibernate and my prog is simple deletion from the db.
Hibernate vertionis 3.1
my DB is mysql
My Question is, can i do the deletion without using any getter and setter method? I gave my id value wihtin the same prog. where i create the session using a variable.
Eventmanager.java
----------------------------
package events;
import org.hibernate.Session;
import java.util.Date;
import java.util.*;
import util.HibernateUtil;
public class EventManager
{
public static void main(String[] args)
{
EventManager mgr = new EventManager();
if (args[0].equals("delete"))
{
mgr.createAndStoreEvent(1);
}
HibernateUtil.getSessionFactory().close();
}
private void createAndStoreEvent(int id)
{
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
int EVENT_ID=id;
Event theEvent = (Event) session.get(Event.class, new Long(EVENT_ID));
session.delete(theEvent);
session.getTransaction().commit();
}
}
Event.java
--------------
package events;
import java.util.Date;
public class Event
{
private int id;
private Date date;
private String title;
public Event()
{}
public int getId() {
return id;
}
private void setId(int id) {
this.id = id;
}
util\HibernateUtil.java
-------------------------------
package util;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
The Exception is:Exception in thread main
Initial SessionFactory crreation is failed.
Pls.... Help.
Thanks
|