📄 companydaohibernate.java
字号:
/**
* Hibernate Demo
* Copyright by cinc
*/
package dao.hibernate;
import dao.CompanyDAO;
import bean.Company;
import bean.Person;
import net.sf.hibernate.Session;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Transaction;
import java.util.List;
import java.util.Set;
public class CompanyDAOHibernate implements CompanyDAO{
public Company findById(String id) throws HibernateException {
Company c = null;
Session s = HibernateSessionFactory.openSession();
Transaction tx = null;
try{
tx = s.beginTransaction();
c = (Company) s.load( Company.class, id );
tx.commit();
}catch(HibernateException he){
if ( tx!=null ){
tx.rollback();
}
throw he;
}finally{
s.close();
}
return c;
}
public Company addCompany(Company company) throws HibernateException {
Session s = HibernateSessionFactory.openSession();
Transaction tx = null;
try{
tx = s.beginTransaction();
s.save( company );
tx.commit();
}catch(HibernateException he){
if ( tx!=null ){
tx.rollback();
}
throw he;
}finally{
s.close();
}
return company;
}
public void removeCompany(Company company) throws HibernateException {
Session s = HibernateSessionFactory.openSession();
Transaction tx = null;
try{
tx = s.beginTransaction();
s.delete( company );
tx.commit();
}catch(HibernateException he){
if ( tx!=null ){
tx.rollback();
}
throw he;
}finally{
s.close();
}
}
public Company updateCompany(Company company) throws HibernateException {
Session s = HibernateSessionFactory.openSession();
Transaction tx = null;
try{
tx = s.beginTransaction();
s.saveOrUpdate( company );
tx.commit();
}catch(HibernateException he){
if ( tx!=null ){
tx.rollback();
}
throw he;
}finally{
s.close();
}
return company;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -