[英]Use @Query annotation not in the JpaRepository
I have the User
entity in the database and it's JPA entity. 我在数据库中有
User
实体,它是JPA实体。 I also have the UserDescription
class which hasn't table in the database. 我也有
UserDescription
类,该类在数据库中没有表。 I want to instantiate UserDescription
class using different tables and entities. 我想使用不同的表和实体实例化
UserDescription
类。 But I don't want to use EntityManager for it. 但是我不想使用EntityManager。 So I found
SELECT new ...
pattern which can be used in @Query
annotation, it's the perfect solution. 因此,我发现可以在
@Query
批注中使用的SELECT new ...
模式,这是理想的解决方案。
But I have simple issue - I need to annotate some method declaration which should be in interface. 但是我有一个简单的问题-我需要注释一些应该在接口中的方法声明。 Usually it's done in
JpaRepository
, but UserDescription
- isn't entity, so I can't do this! 通常它是在
JpaRepository
完成的,但是UserDescription
不是实体,所以我不能这样做!
Also, I tried to create POJO interface( UserDescriptionService
) with method declaration which annotated using @Query
annotation, tried to create field with this interface in the controller(), and annotated it with Autowired
and of course got: 此外,我尝试使用方法声明创建POJO接口(
UserDescriptionService
),该方法声明使用@Query
注释进行注释,尝试在controller()中使用此接口创建字段,并使用Autowired
对其进行注释,当然得到了:
Field userDescriptionService in UserController required a bean of type 'UserDescriptionService' that could not be found.
UserController中的字段userDescriptionService需要找不到“ UserDescriptionService”类型的Bean。
So, how can I use @Query
annotation not in the JpaRepository
or how can I get JpaRepository
for the non-entity class? 那么,如何不在
JpaRepository
使用@Query
注释,或者如何为非实体类获取JpaRepository
?
As M. Deinum already mentioned. 正如Deinum先生已经提到的。 You are not forced to return only Entities in JpaRepositories.
您没有被迫仅返回JpaRepositories中的实体。
You can use the Constructor Expression (aka select new) or also projections. 您可以使用构造函数表达式(也可以选择新的)或投影。 Find more about projections here:
在此处找到有关投影的更多信息:
https://docs.spring.io/spring-data/jpa/docs/2.0.9.RELEASE/reference/html/#projections https://docs.spring.io/spring-data/jpa/docs/2.0.9.RELEASE/reference/html/#projections
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.