如何制作通用jpa存储库?我应该这样做吗?为什么? [英] How to make generic jpa repository? Should I do this? Why?

查看:73
本文介绍了如何制作通用jpa存储库?我应该这样做吗?为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是堆栈溢出的新手,并且使用hibernate和mysql处理spring jpa数据。我为每个实体类创建了一个JpaRepository。但现在我觉得我应该为所有实体使用一个存储库,因为在我所有的存储库中都有通用的CRUD操作方法。


  1. 保存()

  2. li>
  3. findOne()


  4. findAll()

  5. ol>

    除了上述方法之外,我的应用程序中还有其他自定义方法。



    我的目标是实现像GenericRepo ,

      public interface MyGenericRepo扩展JpaRepository< GenericEntity,Integer> 
    {

    }

    我的实体将如下所示:

      class Place延伸GenericEntity 
    {
    private event event;
    }

    class Event延伸GenericEntity
    {

    }

    类提供延伸GenericEntity
    {
    私人地方;
    }

    类用户扩展GenericEntity
    {
    private地方;
    }

    当我打电话时:

      MyGenericRepo myRepo; 

    GenericEntity place = new Place();

    myRepo.save(place);

    它应该可以节省。



    <$ p $ $> $ $ $ $ $ $ $

    我已经提到了上面的链接,我发现Jpa继承与Joined和Table-Per-Class策略类似于我我在寻找,但这些都有一定的局限性。所以请告诉我,我应该尝试实现这个通用的东西。如果我得到任何演示代码,那么我会非常感谢......



    谢谢..

    如何制作通用jpa存储库?我应该这样做吗?为什么?

    解决方案

    如果你想创建你自己的Repos(而不是Spring数据,它为你做了一些工作),你的例子isn不错,我在一个应用程序中使用了类似的策略。

    这里有一些想法来改进通用方法:
    我在我的基本域中添加了ID信息,它由所有域对象实现:

      public interface UniqueIdentifyable< T extends Number> {
    T getId();
    void setId(T id);
    }

    下一步我创建了一个通用的CRUDRepo:

      public interface CRUDRepository< ID extends Number,T extends UniqueIdentifyable ID> {
    ID insert(T entity);
    void delete(T entity);
    ....
    }

    我使用抽象类CRUDRepo:

      public abstract class AbstractCRUDRepo< ID extends Number,T extends UniqueIdentifyable< ID>>实现CRUDRepo< ID,T> ;, {...} 

    域回购API现在看起来像:

      public interface UserRepo extends CRUDRepo< Integer,User> {
    User mySpecificQuery(..);
    }

    最后您可以通过以下方式实现您的回购:

      public class UserRepoImpl extends AbstractCRUDRepo< Integer,User>实现UserRepo {
    public User mySpecificQuery(..){..}
    }


    I am new to stack overflow and working on spring jpa data with hibernate and mysql. I have created One JpaRepository for each entity class. But now I feel that I should use One repository for all entities because In all my repositories has common CRUD operation methods.

    1. save()

    2. update()

    3. delete()

    4. findOne()

    5. findAll()

    Besides of above methods, I have other custom methods also in my applications.

    my aim is to implement GenericRepo like,

    public interface MyGenericRepo extends JpaRepository<GenericEntity,Integer>
    {
    
    }
    

    my entities will be like:

    class Place extends GenericEntity
    {
        private Event event;
    }
    
    class Event extends GenericEntity
    {  
    
    }
    
    class Offer extends GenericEntity
    {
         private Place place;
    }
    
    class User  extends GenericEntity
    {
         private Place place;
    }
    

    when I call:

        MyGenericRepo myRepo;
    
        GenericEntity place=new Place();
    
        myRepo.save(place);
    

    It should save place.

    [http://openjpa.apache.org/builds/1.0.2/apache-openjpa-1.0.2/docs/manual/jpa_overview_mapping_inher.html#jpa_overview_mapping_inher_joined][1]
    

    I have referred above link and I found that Jpa Inheritance with Joined and Table-Per-Class strategies are similar to what I am looking for, but these all have certain limitations.So please tell me should I try to implement this generic thing.If I get any demo code then I will be very greatful...

    Thanks..

    How to make generic jpa repository? Should I do this? Why?

    解决方案

    If you want to create your own Repos (and not spring data which does some work for you) your example isn't bad, i am using a similar strategy in one application.

    Here a few thoughts to improve the generic way: I've added the ID-information in my basic domain which is implemented by all domain objects:

    public interface UniqueIdentifyable<T extends Number> {
        T getId();
        void setId(T id);
    }
    

    In the next step i've created a generic CRUDRepo:

    public interface CRUDRepository<ID extends Number, T extends UniqueIdentifyable<ID>>{
       ID insert(T entity);
       void delete(T entity);
       ....
    } 
    

    And I am using an abstract class for the CRUDRepo:

    public abstract class AbstractCRUDRepo<ID extends Number, T extends UniqueIdentifyable<ID>> implements CRUDRepo<ID, T>, {...}
    

    a domain repo api will now look like:

    public interface UserRepo extends CRUDRepo<Integer, User > {
       User mySpecificQuery(..);
    } 
    

    and finally you can implement your repo via:

    public class UserRepoImpl extends AbstractCRUDRepo<Integer, User > implements UserRepo {
       public User mySpecificQuery(..){..}
    }
    

    这篇关于如何制作通用jpa存储库?我应该这样做吗?为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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