companydaohibernate.java

来自「hibernate and xdoclet 结合」· Java 代码 · 共 91 行

JAVA
91
字号
/**
 * 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 + =
减小字号Ctrl + -
显示快捷键?