我如何在基于休眠的dao实现中减少bioler板代码

时间:2019-03-07 15:08:18

标签: java hibernate

我编写了一个dao接口的基于休眠的实现,但是现在我意识到有很多重复的代码,例如打开sesison,开始事务,提交和回滚,每种方法都具有相同的代码,我想知道将其保存在一个地方的方式,听说过一个叫做AbstractDao的东西,但是这个概念在这里如何适用,真的有效,我想减少样板代码。

这是我的实现类的片段

package com.javarnd.cip.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.javarnd.cip.db.HibernateUtil;
import com.javarnd.cip.model.Sports;

public class SportsDaoImpl implements SportsDao {

    public void save(Sports sports) {
        Session session = HibernateUtil.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            session.save(sports);
            session.getTransaction().commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }

    }

    public List<Sports> findAll() {
        Session session = HibernateUtil.openSession();
        Transaction tx = null;
        List<Sports> sportsList = new ArrayList<>();
        try {
            tx = session.beginTransaction();
            sportsList = session.createQuery("FROM Sports", Sports.class).list();
            session.getTransaction().commit();

        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
                return null;
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
        return sportsList;

    }

    public void update(Sports sports) {
        Session session = HibernateUtil.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();

            Sports sportsData = session.get(Sports.class, sports.getSportId());
            sportsData.setName(sports.getName());
            session.getTransaction().commit();

        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
    }

    public void delete(Sports sports) {
        Session session = HibernateUtil.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();

            Sports sportsData = session.get(Sports.class, sports.getSportId());
            session.delete(sportsData);

            session.getTransaction().commit();

        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
    }

    @Override
    public Sports findById(long sportsId) {
        Session session = HibernateUtil.openSession();
        Transaction tx = null;
        Sports sportsData = null;
        try {
            tx = session.beginTransaction();
            sportsData = session.get(Sports.class, sportsId);
            session.getTransaction().commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
        return sportsData;
    }

}

1 个答案:

答案 0 :(得分:0)

如果您可以使用Java 8,它可能看起来像这样:

private static <OUTPUT> OUTPUT runInTransaction(Function<Session, OUTPUT> function) {
    Transaction tx = null;
    try (Session session = HibernateUtil.openSession()) {
        tx = session.beginTransaction();
        final OUTPUT result = function.apply(session);
        session.getTransaction().commit();
        return result;
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
        return null;
    }
}

然后:

runInTransaction(session -> {
    return session.get(Sports.class, sportsId);
});