JPA存储库失败,具有良好的属性名称,并使用错误的名称

时间:2017-03-02 12:10:16

标签: spring jpa repository

我有一个名为isChecked的属性类,其类型为boolean。在Jpa存储库中,我编写了一个方法来查找isChecked = false的所有行;

public interface ReservationReminderRepository extends JpaRepository<ReservationReminder, Integer> {
ReservationReminder findByReservationReminderId(Integer id);

//@Query("select r from ReservationReminder r where r.isChecked = :checked")
List<ReservationReminder> findByChecked(boolean checked);


}

虽然我试图在Jpa reporistory中调用一个方法findByChecked(),但是当我尝试运行一个具有正确命名属性的方法时 - 因为它在jpa doc中找到了findByIsChecked()我很奇怪失败了:

Caused by: java.lang.IllegalArgumentException: Unable to locate Attribute  with the the given name [isChecked] on this ManagedType [com.carwash.domains.ReservationReminder]
at org.hibernate.jpa.internal.metamodel.AbstractManagedType.checkNotNull(AbstractManagedType.java:128)
at org.hibernate.jpa.internal.metamodel.AbstractManagedType.getAttribute(AbstractManagedType.java:113)
at org.springframework.data.jpa.repository.query.QueryUtils.toExpressionRecursively(QueryUtils.java:566)
at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.getTypedPath(JpaQueryCreator.java:334)
at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.build(JpaQueryCreator.java:277)
at org.springframework.data.jpa.repository.query.JpaQueryCreator.toPredicate(JpaQueryCreator.java:182)
at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:109)
at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:49)
at org.springframework.data.repository.query.parser.AbstractQueryCreator.createCriteria(AbstractQueryCreator.java:109)
at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:88)
at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:73)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.<init>(PartTreeJpaQuery.java:118)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$CountQueryPreparer.<init>(PartTreeJpaQuery.java:241)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:68)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:103)
... 104 more

谁能告诉我为什么会收到那种失败?当我想查看属性checkedDate时,方法名称会是什么样子?

package com.carwash.domains;

import javax.persistence.*;
import java.util.Date;

/**
* Created by mbi on 01.03.2017.
*/
@Entity
public class ReservationReminder {
private int reservationReminderId;
private Reservation reservation;
private boolean isChecked;
private Date checkedDate;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getReservationReminderId() {
    return reservationReminderId;
}

public void setReservationReminderId(int reservationReminderId) {
    this.reservationReminderId = reservationReminderId;
}

@OneToOne(mappedBy = "reservationReminder")
public Reservation getReservation() {
    return reservation;
}

public void setReservation(Reservation reservation) {
    this.reservation = reservation;
}

public boolean getChecked() {
    return isChecked;
}

public void setChecked(Boolean checked) {
    isChecked = checked;
}

public Date getCheckedDate() {
    return checkedDate;
}

public void setCheckedDate(Date checkedDate) {
    this.checkedDate = checkedDate;
}

@Override
public String toString() {
    return "ReservationReminder{" +
            "reviewId=" + reservationReminderId +
            ", isChecked=" + isChecked +
            ", checkedDate=" + checkedDate +
            '}';
}

public ReservationReminder() {
}

public ReservationReminder(Boolean isChecked, Date checkedDate) {
    this.isChecked = isChecked;
    this.checkedDate = checkedDate;
}

public ReservationReminder(int reservationReminderId, Reservation reservation, boolean isChecked, Date checkedDate) {
    this.reservationReminderId = reservationReminderId;
    this.reservation = reservation;
    this.isChecked = isChecked;
    this.checkedDate = checkedDate;
}
}

1 个答案:

答案 0 :(得分:0)

似乎问题与该属性的命名有关。

正如你告诉Spring寻找findByChecked而属性名称为isChecked

您可以尝试使用findByIsChecked并将吸气剂更改为isChecked

但实际上我会将属性更改为checked,getter更改为isChecked并保留jpa查询方法。