Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign an @EntityGraph annotation to Spring Data JPA repository .findAll()

Annotating the Spring Data JPA repository method findAll() with @EntityGraph:

import org.springframework.data.jpa.repository.JpaRepository;
[...]

public interface OptgrpRepository extends JpaRepository<Optgrp> {

@EntityGraph(value = "Optgrp.sysoptions")
List<Optgrp> findAll();

}

leads to this error message:

org.springframework.data.mapping.PropertyReferenceException: No property findAll found for type Optgrp!

Same error happens when changing findAll() to other names:

findAllWithDetail() --> No property findAllWithDetail found for type Optgrp!

findWithDetailAll() --> No property findWithDetailAll found for type Optgrp!

Question: Is it at all possible to use the @EntityGraph annotation on a Spring Data JPA repository method that finds all entities?

EDIT: as asked in the comment, here's the extract from the Optgrp entity class:

@Entity
@NamedEntityGraph(name = "Optgrp.sysoptions", attributeNodes = @NamedAttributeNode("sysoptions"))
public class Optgrp implements Serializable {
    [...]
    @OneToMany(mappedBy="optgrp", cascade = CascadeType.ALL, orphanRemoval=true)
    @OrderBy(clause = "ordnr ASC")
    private List<Sysoption> sysoptions = new ArrayList<>();
}

And the Sysoption entity class as well:

@Entity
public class Sysoption implements Serializable {
[...]
    @ManyToOne
    @JoinColumn(name = "optgrp_id", insertable=false, updatable=false)
    private Optgrp optgrp;
}
like image 410
Markus Pscheidt Avatar asked Apr 23 '15 13:04

Markus Pscheidt


2 Answers

For all, who are using Stack Overflow as knowledge database too, I record a new status to Markus Pscheidts challenge. Three years and six months later the @EntityGraph annotation works now directly at the findAll() function in Spring Data JpaRepository, as Markus original expected.

@Repository
public interface ImportMovieDAO extends JpaRepository<ImportMovie, Long> {
  @NotNull
  @Override
  @EntityGraph(value = "graph.ImportMovie.videoPaths")
  List<ImportMovie> findAll();
}

Versions used in the test: Spring Boot 2.0.3.RELEASE with included spring-boot-starter-data-jpa.

like image 194
ThirstForKnowledge Avatar answered Jan 04 '23 17:01

ThirstForKnowledge


Using the name findByIdNotNull is one way to combine both findAll() and entity graph:

@EntityGraph(value = "Optgrp.sysoptions")
List<Optgrp> findByIdNotNull();
like image 28
Markus Pscheidt Avatar answered Jan 04 '23 16:01

Markus Pscheidt