[英]Retrieve List of "hibernate proxy objects" with "Spring Data Jpa query"
Can I somehow retrieve a list of Hibernate proxy objects with Spring Data Jpa query?我可以通过Spring Data Jpa 查询以某种方式检索Hibernate 代理对象列表吗?
I have object ids from request, and I don't want to retrieve them (because there are many OneToOne relations in entity), but I want to retrieve their proxies which will contain their ids ( I need this for relation ).我有来自请求的对象 id,我不想检索它们(因为实体中有许多 OneToOne 关系),但我想检索它们的代理,其中包含它们的 id (我需要这个作为关系)。 I want something like
Hibernate findOne()
or getOne()
but for list result.我想要像
Hibernate findOne()
或getOne()
但对于列表结果。
Any suggestion will be appreciated, thanks!任何建议将不胜感激,谢谢!
If I get your questions right you want to do something like this:如果我问对了你的问题,你想做这样的事情:
Geo-spatial repository queries (does not require @Query annotation)地理空间存储库查询(不需要@Query 注释)
public interface PersonRepository extends JpaRepository<Person, String>
List<Person> findByLocationNear(String location, Integer distance);
List<Person> findByCity(String city);
}
However You may also use JQL Query Methods in you repository但是,您也可以在存储库中使用 JQL 查询方法
@Query("SELECT p FROM Person t WHERE p.name =?1 AND p.location=?2 ")
List<Person> findPersonByLocation(String fName, String location);
The correct solution is using an EntityManager
instance.正确的解决方案是使用
EntityManager
实例。
entityManager.getReference(YourClass.class, id);
Being that this method accepts only a single ID, you'd need to write a custom SQL
query (using HQL
or native SQL
) to retrieve only the IDs of the needed objects.由于此方法仅接受单个 ID,因此您需要编写自定义
SQL
查询(使用HQL
或本机SQL
)以仅检索所需对象的 ID 。
Having a List<T> ids
, you can than有一个
List<T> ids
,你可以比
final List<YourClass> proxyValues =
ids.stream()
.map(id -> entityManager.getReference(YourClass.class, id))
.collect(Collectors.toList());
Speaking about performance, this is perfectly fine, as the database access would be done a single time, retrieving a small amount of data.说到性能,这完全没问题,因为数据库访问将完成一次,检索少量数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.