📄 appointmentdaoimpl.java
字号:
package com.sean.pet.hibernate.dao.impl;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.sean.pet.hibernate.HibernateSessionFactory;
import com.sean.pet.hibernate.dao.AppointmentDao;
import com.sean.pet.hibernate.po.Appointment;
public class AppointmentDaoImpl implements AppointmentDao {
Session session = null;
public void delete(String id) {
// TODO Auto-generated method stub
Transaction tr = null;
try {
session = HibernateSessionFactory.getSession();
tr = session.beginTransaction();
Appointment app = (Appointment) session.load(Appointment.class, id);
session.delete(app);
tr.commit();
} catch (HibernateException e) {
e.printStackTrace();
tr.rollback();
} finally {
}
HibernateSessionFactory.closeSession();
}
@SuppressWarnings("unchecked")
public List<Appointment> findAll() {
// TODO Auto-generated method stub
Transaction tr = null;
List<Appointment> list = new ArrayList<Appointment>();
try {
session = HibernateSessionFactory.getSession();
tr = session.beginTransaction();
String hql = "from Appointment";
Query q = session.createQuery(hql);
list = q.list();
tr.commit();
return list;
} catch (HibernateException e) {
e.printStackTrace();
tr.rollback();
} finally {
}
HibernateSessionFactory.closeSession();
return null;
}
public Appointment findByID(String id) {
// TODO Auto-generated method stub
Appointment app = null;
Transaction tr = null;
try {
session = HibernateSessionFactory.getSession();
tr = session.beginTransaction();
String hql = "from Appointment where id=?";
Query q = session.createQuery(hql);
q.setString(0, id);
app = (Appointment)q.uniqueResult();
tr.commit();
return app;
} catch (HibernateException e) {
e.printStackTrace();
tr.rollback();
} finally {
}
HibernateSessionFactory.closeSession();
return null;
}
public void save(Appointment app) {
// TODO Auto-generated method stub
Transaction tr = null;
try {
session = HibernateSessionFactory.getSession();
tr = session.beginTransaction();
session.save(app);
tr.commit();
} catch (HibernateException e) {
e.printStackTrace();
tr.rollback();
} finally {
}
HibernateSessionFactory.closeSession();
}
public void update(Appointment app) {
// TODO Auto-generated method stub
Transaction tr = null;
try {
session = HibernateSessionFactory.getSession();
tr = session.beginTransaction();
session.merge(app);
tr.commit();
} catch (HibernateException e) {
e.printStackTrace();
tr.rollback();
} finally {
HibernateSessionFactory.closeSession();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -