覆盖使用EntityGraph注释的Spring-Data-JPA默认方法会导致QueryException

时间:2014-12-15 23:23:07

标签: spring-data-jpa querydsl

我尝试使用Data-JPA实现EntityGraph,因为使用QueryDslPredicateExecutor<T>公开了我需要的方法findAll(Predicate, Pageable),我试图用{{1}覆盖它来注释然后麻烦就开始了:

@EntityGraph

当我使用默认方法时,即使使用相同的org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=appointment,role=com.physioclinic.entity.Appointment.createdBy,tableName=user,tableAlias=user5_,origin=appointment appointmen0_,columns={appointmen0_.createdBy_id ,className=com.physioclinic.entity.User}}] [select count(appointment) from com.physioclinic.entity.Appointment appointment where lower(concat(concat(appointment.patient.person.name,?1),appointment.patient.person.surname)) like ?2 escape '!']; nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=appointment,role=com.physioclinic.entity.Appointment.createdBy,tableName=user,tableAlias=user5_,origin=appointment appointmen0_,columns={appointmen0_.createdBy_id ,className=com.physioclinic.entity.User}}] 也没有什么不妥,但是我无法使用EntityGraph,我的实现是否有问题或者谓词?

遵循方案中的对象:

实体

Predicate

存储库

@Table(name = "appointment")
@Entity
@Getter
@Setter
@NamedEntityGraphs({@NamedEntityGraph(name = "graph.Appointment.default", includeAllAttributes = true,
    attributeNodes = {@NamedAttributeNode(value = "physiotherapist"),
        @NamedAttributeNode(value = "patient"),
        @NamedAttributeNode(value = "care")})})
public class Appointment extends PersistableAuditable<User, Long> {

    private static final long serialVersionUID = -4325126792470516159L;

    @DateTimeFormat(pattern = "dd/MM/yyyy")
    @NotNull
    @Column(name = "date")
    private LocalDate date;

    @DateTimeFormat(pattern = "HH:mm")
    @NotNull
    @Column(name = "schedule")
    private LocalTime schedule;

    @ManyToOne
    @JoinColumn(name = "physiotherapist", referencedColumnName = "id")
    private Physiotherapist physiotherapist;

    @ManyToOne
    @JoinColumn(name = "service", referencedColumnName = "id")
    private Service service;

    @ManyToOne
    @JoinColumn(name = "patient", referencedColumnName = "id")
    private Patient patient;

    @ManyToOne(cascade = CascadeType.REMOVE)
    @JoinColumn(name = "care", referencedColumnName = "id")
    private Care care;

    public Appointment(long id) {
        setId(id);
    }

    public Appointment() {
    }

    /**
     * Helpers
     */

    public boolean isSpecialPrice() {
        return care.getPrivateCare() && care.getSpecial() && care.getSpecialPrice() != null;
    }

    public boolean isPrivatePrice() {
        return care.getPrivateCare() && care.getHealthCare() == null;
    }

    public boolean isHealthCarePrice() {
        return !care.getPrivateCare() && care.getHealthCare() != null;
    }
}

谓词

public interface AppointmentRepository extends JpaRepository<Appointment, Long>,
                                               QueryDslPredicateExecutor<Appointment> {

    @EntityGraph(value = "graph.Appointment.default")
    Page<Appointment> findAll(Predicate predicate, Pageable pageable);
}

1 个答案:

答案 0 :(得分:5)

我遇到了完全相同的问题,并且在调试spring源代码时有很多乐趣。 因此根本原因是:spring正在对计数查询应用提示,从而导致该错误。

QueryDslJpaRepository.java:

DisplayName

解决方法是:为您的存储库界面创建一个支持接口,并实现它覆盖查询创建逻辑。这是我的午夜实施(它只是一个必须改进的原型)

b.Value

看起来这是一个错误,我将把它提交到春季jpa jira。