Can I somehow retrieve a list of Hibernate proxy objects with Spring Data Jpa query?
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 ). I want something like Hibernate findOne()
or getOne()
but for list result.
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)
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
@Query("SELECT p FROM Person t WHERE p.name =?1 AND p.location=?2 ")
List<Person> findPersonByLocation(String fName, String location);
Also, you may use HQL
The correct solution is using an EntityManager
instance.
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.
Having a List<T> ids
, you can than
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.