休眠通用DAO,通用服务和通用视图层? [英] Hibernate generic DAO, Generic Service, and Generic View Layer?

查看:93
本文介绍了休眠通用DAO,通用服务和通用视图层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在hibernate中完全理解并实现一个GenericDAO层。我对这个概念很陌生,一直在做一些阅读和学习。我已经找到了关于GenericDAO Layer的示例实现的大量例子,这就是我最终的结果。

  public class GenericDAOImpl< T,ID extends Serializable>实现GenericDAO< T,ID> {

private static Logger log = Logger.getLogger(GenericDAOImpl.class.getName());


private SessionFactory sessionFactory;

$ b @SuppressWarnings(unchecked)
public T findById(long id,Class< T> objectClass){
log.info(Entered GenericDAOImpl findById + id +));
T result =(T)getSessionFactory()。getCurrentSession()。load(objectClass,id);
if(result!= null){
Hibernate.initialize(result);
返回结果;
} else {
return null;



public boolean create(T newInstance){
log.info(Entered GenericDAOImpl create());
if(newInstance == null){
return false;
}
getSessionFactory()。getCurrentSession()。saveOrUpdate(newInstance);
返回true;
}


public boolean updpate(T updateInstance){
log.info(Entered GenericDAOmpl updpate());
if(updateInstance == null){
return false;
}
getSessionFactory()。getCurrentSession()。update(updateInstance);
返回true;


public boolean delete(T entity){
log.info(Entered GenericDAOmpl delete());
if(entity == null){
return false;
}
getSessionFactory()。getCurrentSession()。delete(entity);
返回true;
}

@SuppressWarnings(unchecked)
public List< T> findByExample(T exampleInstance,Class< T> objectClass){
log.info(Entered GenericDAOImpl findByExample());
Criteria searchCriteria = getSessionFactory()。getCurrentSession()。createCriteria(objectClass);

searchCriteria.add(Example.create(exampleInstance));

return(List< T>)searchCriteria.list();

}

public SessionFactory getSessionFactory(){
return sessionFactory;
}

public void setSessionFactory(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;


好像这应该在理论上可行需要一些调整)

我的问题是我可以使用通用服务查看图层为穿越分层架构方法?我不完全理解hibernate事务,足以知道这是否安全,处理事务等等。



也许像这样的服务层例如

  public class GenericServiceImpl< T,ID extends Serializable>实现GenericService< T,ID> {

private GenericDAO< T,ID> genericDao;
$ b @Override
public T findById(long id,Class< T> objectClass){
return this.getGenericDao()。findById(id,objectClass);
}

@Override
public boolean create(T newInstance){
return this.getGenericDao()。create(newInstance);
}

@Override
public boolean updpate(T updateInstance){
return this.getGenericDao()。updpate(updateInstance);
}

@Override
public boolean delete(T entity){
return this.getGenericDao()。delete(entity);
}

@SuppressWarnings({unchecked,rawtypes})
@Override
public List findByExample(T exampleInstance,Class< T> objectClass){
返回this.getGenericDao()。findByExample(exampleInstance,objectClass);
}


public GenericDAO< T,ID> getGenericDao(){
return genericDao;


public void setGenericDao(GenericDAO< T,ID> genericDao){
this.genericDao = genericDao;
}


}

然后我可以继续做一个通用的视图层?



请让我知道这种方法是否可以接受,或者是否有任何问题。 b
$ b

感谢您的想法和回应! ,只是将所有事情委托给底层的DAO。有时候这可能是需要的,但通常我会在业务层中加入业务逻辑。将逻辑放入服务层将有助于保持您的控制器层非常轻。



服务可以使用一个或多个DAO来完成它所需的任务。因此,考虑一个简单的银行系统,我有一个AccountDao

  public class AccountDao实现GenericDao< Account,Long> {
//您的保存,插入,删除,查找等
}

然后在我的服务中,我会把makePayment或其他东西放在一起

  @Service 
public class AccountService {

@Autowired
私人AccountDao dao;

@Transactional
public void makePayment(Long fromId,Long toId,double amount){
Account = from dao.find(fromId);
from.withdrawl(amount);

账户= dao.find(toId);
to.deposit(amount);

dao.save(from);
dao.save(to);




$ b

在您的服务层使用交易,为您提供更多控制哪些操作需要在同一个事务中。

I have been trying to fully understand and implement a GenericDAO layer in hibernate. I am new to the concept and have been doing a bit of reading and studying. I have found numberous examples on example implementations of a GenericDAO Layer, and this is what I have ended up with.

public class GenericDAOImpl<T, ID extends Serializable> implements GenericDAO<T, ID> {

    private static Logger log = Logger.getLogger(GenericDAOImpl.class.getName());


    private SessionFactory sessionFactory;


    @SuppressWarnings("unchecked")
    public T findById(long id, Class<T> objectClass) {
        log.info("Entered GenericDAOImpl findById(" + id +")");
        T result = (T) getSessionFactory().getCurrentSession().load(objectClass, id);
        if(result != null){
            Hibernate.initialize(result);
            return result;
        }else{ 
            return null;
        }
    }

    public boolean create(T newInstance) {
        log.info("Entered GenericDAOImpl create()");
        if(newInstance == null){
            return false;
        }
        getSessionFactory().getCurrentSession().saveOrUpdate(newInstance);
        return true;        
    }


    public boolean updpate(T updateInstance) {
        log.info("Entered GenericDAOImpl updpate()");
        if(updateInstance == null){
            return false;
        }
        getSessionFactory().getCurrentSession().update(updateInstance); 
        return true;
    }

    public boolean delete(T entity) {
        log.info("Entered GenericDAOImpl delete()");
        if(entity == null){
            return false;
        }
        getSessionFactory().getCurrentSession().delete(entity);
        return true;
    }

    @SuppressWarnings("unchecked")
    public List<T> findByExample(T exampleInstance, Class<T> objectClass){
        log.info("Entered GenericDAOImpl findByExample()");
        Criteria searchCriteria = getSessionFactory().getCurrentSession().createCriteria(objectClass);

        searchCriteria.add(Example.create(exampleInstance));

        return (List<T>)searchCriteria.list();          

    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }       
}

It seems as though this should work in theory (may need some tweaking)

My question is can I use a generic service and view layer to "pass through" the layered architecture approach? I do not fully understand hibernate transactions enough to know if it is safe to do this, with its handling of transactions etc...

Maybe something like this for the service layer for example

public class GenericServiceImpl<T, ID extends Serializable> implements GenericService<T, ID>{

    private GenericDAO<T, ID> genericDao;

    @Override
    public T findById(long id, Class<T> objectClass) {
        return this.getGenericDao().findById(id, objectClass);
    }

    @Override
    public boolean create(T newInstance) {
        return this.getGenericDao().create(newInstance);
    }

    @Override
    public boolean updpate(T updateInstance) {
        return this.getGenericDao().updpate(updateInstance);
    }

    @Override
    public boolean delete(T entity) {
        return this.getGenericDao().delete(entity);
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public List findByExample(T exampleInstance, Class<T> objectClass) {
        return this.getGenericDao().findByExample(exampleInstance, objectClass);
    }


    public GenericDAO<T, ID> getGenericDao() {
        return genericDao;
    }

    public void setGenericDao(GenericDAO<T, ID> genericDao) {
        this.genericDao = genericDao;
    }


}

Then could I go on and do a generic view layer as well?

Please let me know if this approach is acceptable or if there are any concerns with this approach.

Thanks in advance for your thoughts and responses!

解决方案

Your service, as it stands, is simply delegating everything to the underlying DAO. This may be desired sometimes, but typically I put "business logic" in the service layer. Putting logic in the service layer will help keep your controller layer pretty light too.

A service can use one or more DAOs to accomplish the task it needs. So consider a simple bank system, where I have a AccountDao

public class AccountDao implements GenericDao<Account, Long> {
  // your save, insert, delete, find, etc
}

Then in my service, I would put "makePayment" or something

@Service
public class AccountService {

   @Autowired
   private AccountDao dao;

   @Transactional
   public void makePayment(Long fromId, Long toId, double amount) {
      Account from = dao.find(fromId);
      from.withdrawl(amount);

      Account to = dao.find(toId);
      to.deposit(amount);

      dao.save(from);
      dao.save(to);
   }
}

Use transactions on your service layer, to give you more control over which operations need to be in the same transaction.

这篇关于休眠通用DAO,通用服务和通用视图层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆