简体   繁体   中英

Is there a way to use Spring Data JPA Projections to avoid the join on a @SecondaryTable in some cases?

I have a JPA entity class that uses a @SecondaryTable relationship to retrieve some of its fields. There are a few queries against this entity that only need a couple of fields returned, all from the main table.

I was hoping to use a Spring Data JPA Projection interface with a query on my Repository to automatically avoid joining on to the secondary table when it isn't needed. In my testing, I haven't been able to achieve this yet, the query generated by Hibernate always joins to the secondary table, even if none of the fields from the secondary table aren't on the Projection. Is this behavior supported, or is there a better approach, such as using a @OneToOne mapping?

I am using Spring Boot 2.2.6 (Hibernate 5.4) and a Postgres database.

I found that using a class-based projection instead of an interface projection avoided the join. I'm still not sure if this is the intended behavior or not, but it works for my purposes, so I plan on using it.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM