我已经为数据库 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 方法中编写。这是更好的方法,还是无论如何都要重复的最少代码量?