Spring Boot:如何为每个域类避免太多的JPA存储库 [英] Spring Boot: How to avoid too many JPA repositories for each domain class

查看:71
本文介绍了Spring Boot:如何为每个域类避免太多的JPA存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个包含超过10个域类的Spring Boot应用程序,这些域类需要保留在SQL数据库中. 问题是我需要为每个单个域类创建一个接口,因此为每个域类都这样:

public interface BehandelaarRepo extends CrudRepository<BehandelCentrum, Long> {

} 

是否有任何方法可以通过使用某种设计模式或其他方式来减少存储库的数量?有什么建议吗?

解决方案

实际上,您可以像使用Spring Data JPA一样使用泛型来使自己更轻松一些:

public interface JpaRepository<T extends Serializable, ID extends Serializable> {
    public <S extends T> S save(S object);
}

诀窍是您可以使用所有子类,并且也可以重新获得该类.我总是创建一个超类,因此摆脱了通用的ID:

@MappedSuperclass
public class JpaObject {
    @Id
    @GeneratedValue
    private Long id;
    (.... created, last updated, general stuff here....)
}

我从此JpaObject创建我的@Entity类作为子类.

第二步:创建我的超级接口,以供将来特殊查询的使用:

@NoRepositoryBean
public interface Dao<T extends JpaObject> extends JpaRepository<T, Long> {
}

下一步:通用的Dao,看起来有些愚蠢,并且一直空着

@Repository
public interface GenericDao extends Dao<JpaObject> {
}

现在对CrudRepository/JpaRepository中的该保存方法有一个清晰的了解:

public <S extends T> S save(S object);

现在可以将所有扩展JpaObject的对象(S扩展JpaObject)作为所有方法的参数,并且returntype与您的参数属于同一类.

(阿齐兹(Aziz),阿尔·希特·汉格(al het handiger),在荷兰内特兰(Nederlands uitgelegd worden):普·格罗伊特·兹沃勒(P Groet uit Zwolle)

I'm building a Spring Boot application containing more than 10 domain classes which need to be persisted in a SQL database. The problem is that I need to create an interface for every single domain class, so something like this for each one:

public interface BehandelaarRepo extends CrudRepository<BehandelCentrum, Long> {

} 

Is there any way to decrease the number of repositories by using some kind of design pattern or whatever? Any suggestions?

解决方案

You can actually make it some easier for yourself by using generics the same way Spring Data JPA does it:

public interface JpaRepository<T extends Serializable, ID extends Serializable> {
    public <S extends T> S save(S object);
}

The trick is that you can use all subclasses, and you're getting that class back as well. I always create one superclass, so I get rid of my ID generic:

@MappedSuperclass
public class JpaObject {
    @Id
    @GeneratedValue
    private Long id;
    (.... created, last updated, general stuff here....)
}

I create my @Entity classes as a subclass from this JpaObject.

Second step: create my super interface for future usage of special queries:

@NoRepositoryBean
public interface Dao<T extends JpaObject> extends JpaRepository<T, Long> {
}

Next step: the generic Dao which looks some stupid and remains empty at all times

@Repository
public interface GenericDao extends Dao<JpaObject> {
}

Now have a sharp look at that save method in CrudRepository / JpaRepository:

public <S extends T> S save(S object);

Any object extending JpaObject (S extends JpaObject) now can be given as a parameter to all methods, and the returntype is the same class as your parameter.

(Aziz, als het handiger is, kan het ook in het Nederlands uitgelegd worden :P Groet uit Zwolle)

这篇关于Spring Boot:如何为每个域类避免太多的JPA存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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