我已经为数据库CRUD操作编写了这个服务类。
可见公共类CompanyServiceImpl extends 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方法中编写。 这是更好的方法,或者这是无论如何都要重复的最小代码量?
答案 0 :(得分:1)
如果您使用弹簧并且能够使用注释。在提供的链接上有一篇很好的文章,展示了如何使用注释来包装事务中的方法。
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.
}
答案 1 :(得分: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);
}