Hibernate中的通用DAO模式 [英] Generic DAO pattern in Hibernate

查看:238
本文介绍了Hibernate中的通用DAO模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Hibernate工作时,我们遵循Hibernate Doc中提到的通用Hibernate DAO模式。

While working on hibernate we are following generic Hibernate DAO pattern as mentioned in Hibernate Doc also.

所以我们目前正在维护两个平行的资产
1)对于接口
2)for Implimentation

So as per this we are currently maintaining two parallel hirarchies 1) for interfaces 2) for Implimentation

所以如果我们以这种方式工作,即使没有提出新的方法,我们需要标准的persistencence方法为这个entiry创建一个标记界面以及它的实现。

so if we work on this the way even if there is no proposed new method beside standard persistannce methods we need to create a marker interface for that entiry as well its Implimentation.

尽管这种方法似乎没有问题,它的明确分离。

Though there seems no problem in this approach and its clear seperation.

我的问题是如果有更好的方式/替代方式来实现这个

my question is if there any better way/alternate way to achieve this

提前感谢

推荐答案

Umesh我会告诉你我们如何实现这个功能

Umesh I will show you how we implement this functionality

接口

public interface Repository<INSTANCE_CLASS, PRIMARY_KEY_CLASS> {

    void add(INSTANCE_CLASS instance);
    void merge(INSTANCE_CLASS instance);
    void remove(PRIMARY_KEY_CLASS id);
    INSTANCE_CLASS findById(PRIMARY_KEY_CLASS id);
    INSTANCE_CLASS findById(PRIMARY_KEY_CLASS id, Class fetchingStrategy, Object... args);
    List<INSTANCE_CLASS> findAll();
    List<INSTANCE_CLASS> findAll(Class fetchingStrategy, Object... args);
    List<INSTANCE_CLASS> findAll(int pageNumber, int pageSize);
    List<INSTANCE_CLASS> findAll(int pageNumber, int pageSize, Class fetchingStrategy, Object... args);
    List<INSTANCE_CLASS> findAllByCriteria(Criteria criteria);
    List<INSTANCE_CLASS> findAllByCriteria(Criteria criteria, Class fetchingStrategy, Object... args);
    List<INSTANCE_CLASS> findAllByCriteria(int pageNumber, int pageSize, Criteria criteria);
    List<INSTANCE_CLASS> findAllByCriteria(int pageNumber, int pageSize, Criteria criteria, Class fetchingStrategy, Object... args);

}

因为你通常不需要上面显示的所有方法,我们创建一个抽象类,目的是作为一个虚拟实现

Because you usually will not need all of methods shown above, we create an abstract class with the purpose of being a dummy implementation

public abstract class AbstractRepository<INSTANCE_CLASS, PRIMARY_KEY_CLASS> implements Repository<INSTANCE_CLASS, PRIMARY_KEY_CLASS> {

    public void add(INSTANCE_CLASS instance) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void merge(INSTANCE_CLASS instance) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void remove(PRIMARY_KEY_CLASS id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }


    public INSTANCE_CLASS findById(PRIMARY_KEY_CLASS id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public INSTANCE_CLASS findById(PRIMARY_KEY_CLASS id, Class fetchingStrategy, Object... args) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<INSTANCE_CLASS> findAll() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<INSTANCE_CLASS> findAll(Class fetchingStrategy, Object... args) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<INSTANCE_CLASS> findAll(int pageNumber, int pageSize) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<INSTANCE_CLASS> findAll(int pageNumber, int pageSize, Class fetchingStrategy, Object... args) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<INSTANCE_CLASS> findAllByCriteria(Criteria criteria) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<INSTANCE_CLASS> findAllByCriteria(Criteria criteria, Class fetchingStrategy, Object... args) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<INSTANCE_CLASS> findAllByCriteria(int pageNumber, int pageSize, Criteria criteria) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<INSTANCE_CLASS> findAllByCriteria(int pageNumber, int pageSize, Criteria criteria, Class fetchingStrategy, Object... args) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}

现在,例如,如果你想要一个仅需要添加方法的存储库,可以使用

Now, for instance, if you want a repository which needs only add method, you can use

public class PersonRepository extends AbstractRepository<Person, Integer> {

    public void add(Person instance) {
        /**
          * person implmentatiuon goes here
          */    
    }

}

如果其他开发人员尝试访问除了添加方法之外,他或她将获得 UnsupportedOperationException

If other developer try to access other than add method, he or she will get UnsupportedOperationException

条件只是一个标记界面

public interface Criteria {}

某些方法的目的是定义一个参数Class fetchingStrategy 是匹配外部化的命名查询。这样我就避免了手写字符串的错误。 JSR-303 bean验证使用这种方法,例如,验证属性的。请参阅这里

The purpose of some methods define a parameter Class fetchingStrategy is to match externalized named queries. This way, I avoid hand-coded string which is error-prone. This approach is used by JSR-303 bean validation, for instance, to validate groups of properties. See here

public class Person {
    public static interface PERSON_WITH_ADDRESS {}
}

外部化命名查询如下所示:

The externalize named query is shown as follows

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <query name="PERSON_WITH_ADDRESS">
        <![CDATA[
            from 
                Person p
            left join fetch 
                p.address
        ]]>
    </query>
</hibernate-mapping>

所以当我想检索所有的地址的人,我打电话给

So when i want to retrieve all of person with address, i call

PersonRepository<Person, Integer> respository ...

List<Person> personList = repository.findAll(PERSON_WITH_ADDRESS.class);

findAll可以写成

findAll can be written as

public class PersonRepository extends AbstractRepository<Person, Integer> {

    List<Person> findAll(Class fetchingStrategy, Object... args) {
        if(fetchingStrategy.isAssignableFrom(PERSON_WITH_ADDRESS.class)) {
            sessionFactory.getCurrentSession()
                          .getNamedQuery(fetchingStrategy.getSimpleName())
                          .list();
        }

        throw new UnsupportedOperationException("Not supported yet.");
    }

}

这篇关于Hibernate中的通用DAO模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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