JPA 存储库属性表达式 [英] JPA repository property expression

查看:31
本文介绍了JPA 存储库属性表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Spring Boot 应用程序(基于 JHipster 4)中,我尝试使用属性表达式通过相关实体的属性过滤我的查询,如 Spring 文档中所述:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-property-expressions

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

我想获取所有约会,在他们的 AppointmentSchedules 中有某个 EmployeeAccount,但只收到一个空列表.

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);
}

相关实体及其关系:

@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.

请注意,Appointment 表不包含 AppointmentSchedules 的列,因为它是 OneToMany 关系.也许这就是原因?我以为 JPA 会为我处理这个逻辑...

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...

表结构:

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 的回答都有效但不是必需的,因为问题中的原始代码已经有效

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

推荐答案

是的,在您的方法中添加词 In 并提供 Set 作为 employeeAccountIds.>

Yes add the word In to your method and provide Set<Long> as employeeAccountIds.

Set<Person> findByScheduledAppointmentsEmployeeAccountsIdIn(Set<Long> employeeIds);

这篇关于JPA 存储库属性表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆