通用或特定的DAO使用来自多个表的信息记录交付? [英] General or specific DAO to record delivery with information from multiple tables?

查看:83
本文介绍了通用或特定的DAO使用来自多个表的信息记录交付?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Web应用程序,它允许用户通过使用spring和hibernate的GUI存储和检索数据库中的信息。



创建DAO和服务层。我想创建一个可以添加新交付的方法。在我的投放表中,我有 >客户名称,客户国家



我遇到的问题是,我希望最终用户通过输入产品类型,产品名称,客户名称,客户国家和日期。 (1)使用添加包含这些对象的新交付的方法创建DAO





(2)创建一个将一般对象持久化到数据库的DAO,然后使用服务方法为每个单独的对象实现DAO?



感谢您的帮助!

/ D

解决方案

这是我的通用dao的片段,我将其注入到每个服务图层类中。


$ @Component(Dao)
public class Dao实现了IDao {
@Resource(name = sessionFactory)
private SessionFactory sessionFactory;

@Override
public< T> T save(final T o){
return(T)sessionFactory.getCurrentSession()。save(o);
}

@Override
public void delete(final Object object){
sessionFactory.getCurrentSession()。delete(object);
}

@Override
public< T> T get(final Class< T> type,final Long id){
return(T)sessionFactory.getCurrentSession()。get(type,id);
}

@Override
public< T>列表与LT; T> getFieldsEq(final Class< T>类型,final Map< String,Object>限制){
final Session session = sessionFactory.getCurrentSession();
final Criteria crit = session.createCriteria(type); (Map.Entry< String,Object> entry:restrictions.entrySet()){
crit.add(Restrictions.eq(entry.getKey(),entry.getValue()));

}

return crit.list();


$ / code>

哪些可以用于你的服务层:

  @Transactional(readOnly = true)
public List< City> getCities(){
return dao.getAll(City.class);
}

当然,您可以扩展特定复杂查询的dao。拥有一个通用的dao遵循单点责任原则 DRY ,并且使其更易于测试。交易应该在服务层上,并且直接与工作单元相关。

I am creating a web application that lets the user store and retrieve information from a DB through a GUI using spring and hibernate.

I have gotten stuck when it comes to creating the DAO and service layer. I want to create a method that can add a new delivery. In my delivery table i have Product Id and Customer Id which both are mapped to their own tables that contain Product Name, Product Type and Customer Name, Customer Country respectively.

The part that I have trouble with is that I want the end user to record a delivery by entering the product type, product name, customer name, customer country and date. Do I,

(1) Create a DAO with a method for adding a new delivery that includes these objects

or

(2) Create a DAO that just persist a general object to the DB and then use a service method to implement the DAO for each separate object?

Thank you for your help!

/D

解决方案

here is a snippet of my generic dao, which I inject into each service layer class.

@Component("Dao")
public class Dao implements IDao  {
    @Resource(name = "sessionFactory")
    private SessionFactory sessionFactory;

    @Override
    public <T> T save(final T o){
        return (T) sessionFactory.getCurrentSession().save(o);
    }

    @Override
    public void delete(final Object object) {
        sessionFactory.getCurrentSession().delete(object);
    }

    @Override
    public <T> T get(final Class<T> type, final Long id) {
        return (T) sessionFactory.getCurrentSession().get(type, id);
    }

    @Override
    public <T> List<T> getFieldsEq(final Class<T> type, final Map<String, Object> restrictions) {
        final Session session = sessionFactory.getCurrentSession();
        final Criteria crit = session.createCriteria(type);
        for (Map.Entry<String, Object> entry : restrictions.entrySet()) {
            crit.add(Restrictions.eq(entry.getKey(), entry.getValue()));
        }

        return crit.list();
    }
}

Which can hen be used in your service layer like so :

@Transactional(readOnly = true)
public List<City> getCities() {
  return dao.getAll(City.class);
}

And of course you could extend the dao for specific complex queries. Having one generic dao obeys single point of responsibility principle, DRY, and makes it easier to test. The transactions should be on serivce layer and directly relate to units of work.

这篇关于通用或特定的DAO使用来自多个表的信息记录交付?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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