import java.util.*;
import org.hibernate.*;
import org.hibernate.criterion.*;
public class Main {
public static void main(String[] args) {
HibernateUtil.setup("create table EVENTS ( uid int, name VARCHAR, start_Date date, duration int);");
// hibernate code start
SimpleEventDao eventDao = new SimpleEventDao();
Event event = new Event();
event.setName("Create an Event");
eventDao.create(event);
Event foundEvent = eventDao.find(/*event.getId()*/1L);
System.out.println(foundEvent.getName());
HibernateUtil.checkData("select uid, name from events");
// hibernate code end
}
}
/////////////////////////////////////////////////////////////////////////
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.Query;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.List;
public class SimpleEventDao {
Log log = LogFactory.getLog(SimpleEventDao.class);
private Session session;
private Transaction tx;
public SimpleEventDao() {
HibernateFactory.buildIfNeeded();
}
/**
* Insert a new Event into the database.
* @param event
*/
public void create(Event event) throws DataAccessLayerException {
try {
startOperation();
session.save(event);
tx.commit();
} catch (HibernateException e) {
handleException(e);
} finally {
HibernateFactory.close(session);
}
}
/**
* Delete a detached Event from the database.
* @param event
*/
public void delete(Event event) throws DataAccessLayerException {
try {
startOperation();
session.delete(event);
tx.commit();
} catch (HibernateException e) {
handleException(e);
} finally {
HibernateFactory.close(session);
}
}
/**
* Find an Event by its primary key.
* @param id
* @return
*/
public Event find(Long id) throws DataAccessLayerException {
Event event = null;
try {
startOperation();
event = (Event) session.load(Event.class, id);
tx.commit();
} catch (HibernateException e) {
handleException(e);
} finally {
HibernateFactory.close(session);
}
return event;
}
/**
* Updates the state of a detached Event.
*
* @param event
*/
public void update(Event event) throws


No comments:
Post a Comment