问题
In my Spring Boot application (based on JHipster 4) I am trying to use property expressions to filter my query by attributes of related entities as described in the Spring docs: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-property-expressions
I want to get all Appointments, with a certain EmployeeAccount in their AppointmentSchedules but only receive an empty list.
@Repository
public interface AppointmentRepository extends JpaRepository<Appointment, Long> {
public List<Appointment> findByScheduledAppointmentsEmployeeAccountsId(Long id);
}
the relevant entities and their relationships:
@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;
...
}
The hibernate console output when executong the query:
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=?
I would much appreciate if someone could tell me what I'm doing wrong here.
Edit: Please note that the Appointment table doesn't contain a column for the AppointmentSchedules as it's a OneToMany relationship. Maybe that's the reason? I thought JPA would handle that logic for me...
The table structure:
APPOINTMENT;
ID ...
APPOINTMENT_SCHEDULE;
ID APPOINTMENTS_ID ...
APPOINTMENT_SCHEDULE_EMPLOYEE_ACCOUNTS;
EMPLOYEE_ACCOUNTS_ID APPOINTMENT_SCHEDULES_ID
EMPLOYEE_ACCOUNT;
ID ...
EDIT 2: I was able to resolve the issue. It was a classical PEBCAK error because I accidentally called a similarly named method in my service class facepalm. Thanks a lot to everyone who helped me! Both Gaurav Srivastav's and Dean's answers work but are not neccessary, as the original code from the question already works
回答1:
You must use an underscore before referencing the nested fields. Try:
public List<Appointment> findByScheduledAppointments_EmployeeAccounts_Id(Long id)
回答2:
Yes add the word In
to your method and provide Set<Long>
as employeeAccountIds.
Set<Person> findByScheduledAppointmentsEmployeeAccountsIdIn(Set<Long> employeeIds);
来源:https://stackoverflow.com/questions/51080713/jpa-repository-property-expression