Again this is my Interface FOR DAO
Code:
package com.netplus.workstation.pprincess.hibernate.dao;
import java.util.List;
import com.netplus.workstation.pprincess.Limousine;
import net.sf.hibernate.HibernateException;
public interface HibernateLimousineDAO {
public Limousine create(String limType,String description,Double rate,byte[] pic,String fileName,int fileSize) throws Exception;
public List findByAll() throws Exception;
public Limousine findByPrimaryKey(long limId) throws Exception;
public void init() throws HibernateException;
public void remove(long limId) throws Exception;
public void update(Limousine limousine) throws Exception;
}
And this is my Real DAOCode:
package com.netplus.workstation.pprincess.hibernate.impl;
import java.util.List;
import java.io.Serializable;
import javax.ejb.EJBException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.InitialContext;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Session;
import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.Query;
import com.netplus.workstation.pprincess.Limousine;
import com.netplus.workstation.pprincess.hibernate.dao.HibernateLimousineDAO;
import com.netplus.workstation.pprincess.hibernate.entity.HibernateLimousine;
public class HibernateLimousineDAOImpl implements HibernateLimousineDAO , Serializable {
private Session hbSession;
private SessionFactory factory;
public HibernateLimousineDAOImpl() {
}
public Limousine create(String limType, String description, Double rate, byte[] pic, String fileName, int fileSize) throws Exception{
try{
System.out.println("Create Limousine.");
hbSession = factory.openSession();
Transaction tx = hbSession.beginTransaction();
HibernateLimousine hLimousine = new HibernateLimousine();
hLimousine.setLimousineType(limType);
hLimousine.setDescription(description);
hLimousine.setRate(rate);
hLimousine.setPicture(Hibernate.createBlob(pic));
hLimousine.setFileName(fileName);
hLimousine.setFileSize(fileSize);
long id;
try{
id = ((Long)(hbSession.iterate("select max(limousine.limousineId) from com.netplus.workstation.pprincess.hibernate.entity.HibernateLimousine limousine").next())).longValue()+1;
} catch(Exception ex){
id = 1;
}
Long genId = new Long(id);
hbSession.save(hLimousine, genId);
tx.commit();
return (Limousine)hbSession.load(HibernateLimousine.class, genId);
}catch(HibernateException error){
throw new Exception("Error saving object: "+ error.toString());
}catch(Exception e){
e.printStackTrace();
return null;
}finally{
hbSession.close();
}
}
public List findByAll() throws Exception {
try{
System.out.println("Find Limousine All.");
hbSession = factory.openSession();
Transaction tx = hbSession.beginTransaction();
Query query = hbSession.createQuery("from com.netplus.workstation.pprincess.hibernate.entity.HibernateLimousine");
List list = query.list();
System.out.println("Get Limousine size? "+list.size());
return list;
}catch(HibernateException error){
throw new Exception("Error FindByAll = "+error.toString());
}catch(Exception error){
error.printStackTrace();
return null;
}finally{
hbSession.close();
}
}
public Limousine findByPrimaryKey(long limId) throws Exception{
try{
System.out.println("Find By Limousine Id.");
hbSession = factory.openSession();
Transaction tx = hbSession.beginTransaction();
HibernateLimousine hLimousine = (HibernateLimousine)hbSession.load(HibernateLimousine.class, new Long(limId));
return (Limousine)hLimousine;
}catch(HibernateException error){
throw new Exception("Error find by limousine id = "+ limId +" . "+error.toString());
}catch(Exception error){
error.printStackTrace();
return null;
}finally{
hbSession.close();
}
}
public void init() throws HibernateException {
try {
System.out.println("Init DAO");
Context ctx = new InitialContext();
try {
factory = (SessionFactory) ctx.lookup("java:/PPrincessHibernateFactory");
} catch (NamingException e) {
throw new EJBException("Error looking up dataSource: " + e.toString());
}
} catch (NamingException e) {
throw new EJBException("Error initializing context:" + e.toString());
}
}
public void remove(long limId) throws HibernateException {
try{
System.out.println("Remove Limousine.");
hbSession = factory.openSession();
Transaction tx = hbSession.beginTransaction();
HibernateLimousine hLimousine = (HibernateLimousine)hbSession.load(HibernateLimousine.class, new Long(limId));
hbSession.delete(hLimousine);
tx.commit();
}catch(HibernateException error){
}catch(Exception e){
e.printStackTrace();
}finally{
hbSession.close();
}
}
public void update(Limousine limousine) throws Exception{
try{
System.out.println("Update Limousine.");
hbSession = factory.openSession();
Transaction tx = hbSession.beginTransaction();
hbSession.saveOrUpdate(limousine);
tx.commit();
}catch(HibernateException error){
throw new Exception("Error updating object: "+ error.toString());
}catch(Exception e){
e.printStackTrace();
}finally{
hbSession.close();
}
}
}
[/code]