Hibernate 中的通用 DAO 模式

发布于 2024-10-09 09:36:29 字数 253 浏览 2 评论 0原文

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

因此,据此,我们目前维持两个平行的等级制度 1) 对于接口 2)对于实现,

因此如果我们以这种方式工作,即使除了标准持久性方法之外没有提出新方法,我们也需要为该条目及其实现创建一个标记接口。

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

我的问题是是否有更好的方法/替代方法来实现此目的

提前致谢

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

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

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

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

做个少女永远怀春 2024-10-16 09:36:29

Umesh 我将向您展示如何实现此功能

接口

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);

}

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

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.");
    }

}

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

public class PersonRepository extends AbstractRepository<Person, Integer> {

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

}

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

Criteria 只是标记界面

public interface Criteria {}

某些方法定义参数类 fetchingStrategy 的目的是为了匹配外部化命名查询。这样,我就避免了容易出错的手工编码字符串。例如,JSR-303 bean 验证使用此方法来验证属性。请参阅 这里

public class Person {
    public static interface PERSON_WITH_ADDRESS {}
}

外部化命名查询如下所示

<?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>

所以当我想检索所有具有地址的人时,我调用

PersonRepository<Person, Integer> respository ...

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

findAll 可以写为

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.");
    }

}

Umesh I will show you how we implement this functionality

The interface

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
          */    
    }

}

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

Criteria is just a marker interface.

public interface Criteria {}

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 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.");
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文