0

I Have Object Master Location and MasterCountries

MasterLocation.java

@Entity
@Table(name = "master_location", schema = "public", catalog = "master_db")
@NamedEntityGraphs({
        @NamedEntityGraph(name = "MasterLocation.joinsWithMasterCountries",attributeNodes = {
                @NamedAttributeNode("masterCountriesByCountryCode")
        }),
        @NamedEntityGraph(name = "MasterLocation.noJoins",attributeNodes = {})
})
public class MasterLocation {
    private String locationCode;
    private String locationName;
    private String countryCode;
    private Integer agencyCode;
    private String port;
    private String place;
    private String userId;
    private Timestamp dateCreated;
    private Timestamp lastModified;
    private String portRefFrom;
    private String portRefTo;
    private String valid;
    private String regionCode;
    private String bookingRef;

    private MasterCountries masterCountriesByCountryCode;
    @ManyToOne(fetch = FetchType.LAZY)
    @NotFound(action = NotFoundAction.IGNORE)
    @JoinColumn(referencedColumnName = "iso_3166_1_alpha_2_code", name = "country_Code")
    public MasterCountries getMasterCountriesByCountryCode() {
        return masterCountriesByCountryCode;
    }
}

MasterLocationRepository.java

@Repository
public interface MasterLocationRepository extends JpaRepository<MasterLocation, Integer> {

    @EntityGraph(value = "MasterLocation.noJoins",type = EntityGraph.EntityGraphType.LOAD)
    Page<MasterLocation> findByLocationNameLikeIgnoreCase(String locationName, Pageable pageable);
}

Why Object MasterCountries still load on findByLocationNameLikeIgnoreCase? How to disable Fetch MasterCountries in this repository?

Thanks

  • Is that your actual entity? Where is the `@Id`? There should also be more getters... Please add the full entity not a snippet. – M. Deinum Nov 22 '16 at 13:19

1 Answers1

0

I suppose you are using Hibernate as your JPA provider. By default, all single properties are loaded eagerly, even one-to-one or many-to-one relationships annotated as Lazy.

To be able to lazy load single properties, you have to use hibernate's bytecode enhacement support. In this Vlad Mihalcea's post you'll find how to activate it using Maven. You have to add just the following plugin to your pom.xml file:

<plugin>
    <groupId>org.hibernate.orm.tooling</groupId>
    <artifactId>hibernate-enhance-maven-plugin</artifactId>
    <version>${hibernate.version}</version>
    <executions>
        <execution>
            <configuration>
                <enableLazyInitialization>true</enableLazyInitialization>
            </configuration>
            <goals>
                <goal>enhance</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Also try changing the @EntityGraph annotation type parameter to FETCH, instead of LOAD, or annotate the masterCountriesByCountryCode with FetchType.LAZY. With your current configuration (EntityGraphType.LOAD) the properties not included in the EntityGraph definition maintain their default FETCH/LOAD configuration. Take a look here for more details

In another post Vlad talks again about bytecode enhacement and provides another option: subentities. I haven't tried the subentities option myself, so I can't tell you how well it works.

And finally another option, which requires a bit more work, but I think is the best one, is to use projection DTOs to select only the data you need on each case.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
Cèsar
  • 1,206
  • 11
  • 22