⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 companydaohibernate.java

📁 hibernatedemo
💻 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 + -