As it stands at the moment, no.
This is because you have references to two separate objects, identified by two different PK's.
You would have to write a strange custom query to query for the 2 primary keys, return both objects, write logic to separate the fromLocation
and the toLocation
objects, then set each object to their respective object within the Trip
. You'd probably be looking at more overhead doing this than letting Hibernate just run the 2 queries and give you back the entire Trip
object.
If you are super-worried about performance, I'd suggest setting up a second-level cache for these objects. Trying to retrieve the two objects in one custom query, is just unnecessary complication of something that Hibernate does (relatively) efficiently enough on its own.