在我的Spring Boot应用程序(基于JHipster 4)中,我尝试使用属性表达式按Spring文档中所述的相关实体的属性过滤查询: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-property-expressions
我想获取所有约会,并且其AppointmentSchedules中有特定的EmployeeAccount,但只收到一个空列表。
@Repository
public interface AppointmentRepository extends JpaRepository<Appointment, Long> {
public List<Appointment> findByScheduledAppointmentsEmployeeAccountsId(Long id);
}
相关实体及其关系:
@Entity
@Table(name = "appointment")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Appointment implements Serializable {
@OneToMany(mappedBy = "appointments")
@JsonIgnore
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<AppointmentSchedule> scheduledAppointments = new HashSet<>();
...
}
@Entity
@Table(name = "appointment_schedule")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class AppointmentSchedule implements Serializable {
@ManyToMany
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@JoinTable(name = "appointment_schedule_employee_accounts", joinColumns = @JoinColumn(name = "appointment_schedules_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "employee_accounts_id", referencedColumnName = "id"))
private Set<EmployeeAccount> employeeAccounts = new HashSet<>();
...
}
@Entity
@Table(name = "employee_account")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class EmployeeAccount implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
private Long id;
...
}
执行查询时的休眠控制台输出:
Hibernate: select appointmen0_.id as id1_1_, appointmen0_.institution_id as institut2_1_, appointmen0_.user_account_id as user_acc3_1_ from appointment appointmen0_ where appointmen0_.user_account_id=?
如果有人能告诉我我在做什么错,我将不胜感激。
编辑: 请注意,约会表不包含用于约会时间表的列,因为它是一个一对多的关系。也许那是原因吗?我以为JPA会为我处理这种逻辑...
表结构:
APPOINTMENT;
ID ...
APPOINTMENT_SCHEDULE;
ID APPOINTMENTS_ID ...
APPOINTMENT_SCHEDULE_EMPLOYEE_ACCOUNTS;
EMPLOYEE_ACCOUNTS_ID APPOINTMENT_SCHEDULES_ID
EMPLOYEE_ACCOUNT;
ID ...
编辑2: 我能够解决问题。这是一个经典的PEBCAK错误,因为我在服务类 facepalm 中意外地调用了一个类似命名的方法。非常感谢所有帮助我的人! Gaurav Srivastav和Dean的答案都可以使用,但不是必需的,因为问题的原始代码已经可以使用
答案 0 :(得分:1)
在引用嵌套字段之前,必须使用下划线。试试:
public List<Appointment> findByScheduledAppointments_EmployeeAccounts_Id(Long id)
答案 1 :(得分:1)
是的,在您的方法中添加单词In
,并提供Set<Long>
作为employeeAccountIds。
Set<Person> findByScheduledAppointmentsEmployeeAccountsIdIn(Set<Long> employeeIds);