0

我已经为数据库 CRUD 操作编写了这个服务类。

可见的公共类 CompanyServiceImpl 扩展 PersistentRemoteService 实现 CompanyService {

    private GileadHibernateUtil gileadHibernateUtil;

    public CompanyServiceImpl() {
        gileadHibernateUtil = new GileadHibernateUtil();
        setBeanManager(gileadHibernateUtil.getPersistentBeanManager());
    }

    @Override
    public void addCompany(Company newCompany) {
        Objects.requireNonNull(newCompany, "newCompany is null.");

        Session session = gileadHibernateUtil.getCurrentSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.persist(newCompany);
            session.getTransaction().commit();
        } finally {
            if (transaction.isActive()) {
                transaction.rollback();
            }
            session.close();
        }
    }

    @Override
    public Company updateCompany(Company company) {
        Objects.requireNonNull(company, "company is null.");

        Session session = gileadHibernateUtil.getCurrentSession();
        Transaction transaction = null;
        Company managedCompany = null;

        try {
            transaction = session.beginTransaction();
            managedCompany = (Company) session.merge(company);
            session.getTransaction().commit();
        } finally {
            if (transaction.isActive()) {
                transaction.rollback();
            }
        }

        return managedCompany;
    }

    @Override
    public void deleteCompany(Company company) {
        Objects.requireNonNull(company, "company is null.");

        Session session = gileadHibernateUtil.getCurrentSession();
        Transaction transaction = null;

        try {
            transaction = session.beginTransaction();
            session.delete(company);
            session.getTransaction().commit();
        } finally {
            if (transaction.isActive()) {
                transaction.rollback();
            }
        }

    }

    @Override
    public Company findCompany(int companyId) {
        if (companyId <= 0) {
            throw new IllegalArgumentException("companyId must be a positive integer.");
        }

        Session session = gileadHibernateUtil.getCurrentSession();
        Transaction transaction = null;
        Company company = null;

        try {
            transaction = session.beginTransaction();
            company = (Company) session.get(Company.class, companyId);
            session.getTransaction().commit();
        } finally {
            if (transaction.isActive()) {
                transaction.rollback();
            }
        }
        return company;
    }

    @Override
    public List<Company> findMatchingCompanies(String companyName) {

        Session session = gileadHibernateUtil.getCurrentSession();
        Transaction transaction = null;
        List<Company> matchingCompanies = null;

        try {
            transaction = session.beginTransaction();
            Criteria searchCriteria = session.createCriteria(Company.class).add(
                    Restrictions.or(Restrictions.like("code", companyName), Restrictions.like("name", companyName)));
            session.getTransaction().commit();

            matchingCompanies = searchCriteria.list();
        } finally {
            if (transaction.isActive()) {
                transaction.rollback();
            }
        }

        return matchingCompanies;
    }

    @Override
    public List<Company> findAllCompanies() {

        Session session = gileadHibernateUtil.getCurrentSession();
        Transaction transaction = null;
        List<Company> companies = null;

        try {
            transaction = session.beginTransaction();
            Criteria searchCriteria = session.createCriteria(Company.class);
            session.getTransaction().commit();

            companies = searchCriteria.list();
        } finally {
            if (transaction.isActive()) {
                transaction.rollback();
            }
        }
        return companies;
    }

}

我决定不使用单独的 DAO 层。它是一个小应用程序,我不会从 Hibernate 切换。它所包含的只是 Hibernate 会话命令。开始和结束事务仍然来自业务层。所以,Hibernate 几乎可以作为我的 DAO 层。困扰我的是看似样板的交易代码,必须在每个 CRUD 方法中编写。这是更好的方法,还是无论如何都要重复的最少代码量?

4

2 回答 2

1

如果您使用的是 spring 并且能够使用注释。提供的链接上有一篇很好的文章,它展示了如何使用注释来包装事务中的方法。

http://springinpractice.com/2008/03/18/annotation-based-transactions-in-spring/

一起使用 spring 和 hibernate 会让你的代码看起来更干净,看起来像下面这样。

@Override
@Transactional
public void addCompany(Company newCompany) {
    Objects.requireNonNull(newCompany, "newCompany is null.");
    Session session = gileadHibernateUtil.getCurrentSession();
    session.persist(newCompany);
    //session.close(); this line would also not be needed because the session would close automatically when the transaction is complete.
}
于 2013-01-28T16:51:36.770 回答
0

没有弹簧,这样的事情可以使用

java.util.function.Function

 public static Object transactionExecutor(Function<Session, Object> toRun) {
    Session session = null;
    Object result = null;
    try {
        session = getSession();
        final Transaction tx = session.beginTransaction();
        result = toRun.apply(session);
        session.flush();
        tx.commit();
        return result;
    } catch (HibernateException he) {
        Transaction tx = session.getTransaction();
        tx.rollback();
    } finally {

        session.close();
    }
    return result;
 }


 public static void save(final Students S) {
    Function<Session, Object> toRun = new Function<Session, Object>() {
        @Override
        public Object apply(final Session t) {
        t.save(S);
        return S.getId();
        }
    };
    int saveID = (int)transactionExecutor(toRun);
 }
于 2016-05-02T10:36:13.800 回答