Java 在 Spring Data JPA 中处理实体继承的最佳方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27543771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way of handling entities inheritance in Spring Data JPA
提问by Andrea
I've three JPA entityclasses A
, B
and C
with the following hierarchy:
我有三个JPA 实体类A
,B
并且C
具有以下层次结构:
A
|
+---+---+
| |
C B
That is:
那是:
@Entity
@Inheritance
public abstract class A { /* ... */ }
@Entity
public class B extends A { /* ... */ }
@Entity
public class C extends A { /* ... */ }
Using Spring Data JPA, what is the best way to write repositoriesclasses for such entities?
使用Spring Data JPA,为此类实体编写存储库类的最佳方法是什么?
I know that I can write these:
我知道我可以写这些:
public interface ARespository extends CrudRepository<A, Long> { }
public interface BRespository extends CrudRepository<B, Long> { }
public interface CRespository extends CrudRepository<C, Long> { }
but if in the class A
there is a field name
and I add this method in the ARepository
:
但是如果在类中A
有一个字段name
并且我在ARepository
:
public A findByName(String name);
I've to write such method also in the other two repositories, and this is a bit annoying.. Is there a better way to handle such situation?
我也必须在其他两个存储库中编写这样的方法,这有点烦人..有没有更好的方法来处理这种情况?
Another point I would like to have is that ARespository
should be a read-only repository (i.e. extend the Repository
class) while the other two repositories should expose all the CRUD operations.
我想要的另一点是它ARespository
应该是一个只读存储库(即扩展Repository
类),而其他两个存储库应该公开所有 CRUD 操作。
Let me know possible solutions.
让我知道可能的解决方案。
采纳答案by Andrea
I used the solution also described in this postfrom Netgloo's blog.
我使用了Netgloo 博客中这篇文章中描述的解决方案。
The idea is to create a genericrepository class like the following:
这个想法是创建一个通用的存储库类,如下所示:
@NoRepositoryBean
public interface ABaseRepository<T extends A>
extends CrudRepository<T, Long> {
// All methods in this repository will be available in the ARepository,
// in the BRepository and in the CRepository.
// ...
}
then I can write the three repositories in this way:
那么我可以这样写三个存储库:
@Transactional
public interface ARepository extends ABaseRepository<A> { /* ... */ }
@Transactional
public interface BRepository extends ABaseRepository<B> { /* ... */ }
@Transactional
public interface CRepository extends ABaseRepository<C> { /* ... */ }
Moreover, to obtain a read-only repository for ARepository
I can define the ABaseRepository
as read-only:
此外,要获取只读存储库,ARepository
我可以将其定义ABaseRepository
为只读:
@NoRepositoryBean
public interface ABaseRepository<T>
extends Repository<T, Long> {
T findOne(Long id);
Iterable<T> findAll();
Iterable<T> findAll(Sort sort);
Page<T> findAll(Pageable pageable);
}
and from BRepository
extend also the Spring Data JPA's CrudRepository
to achieve a read/write repository:
并BRepository
扩展 Spring Data JPACrudRepository
以实现读/写存储库:
@Transactional
public interface BRepository
extends ABaseRepository<B>, CrudRepository<B, Long>
{ /* ... */ }