【问题标题】:JPA repository property expressionJPA 存储库属性表达式
【发布时间】:2018-12-07 09:47:24
【问题描述】:

在我的 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=?

如果有人能告诉我我在这里做错了什么,我将不胜感激。

编辑: 请注意,Appointment 表不包含 AppointmentSchedules 列,因为它是 OneToMany 关系。也许这就是原因?我以为 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 的答案都有效,但不是必需的,因为问题中的原始代码已经有效

【问题讨论】:

  • 根据上面提供的信息,计划 ID 与约会 ID 不正确匹配。这导致空结果集。请再次检查预约和日程关系。

标签: java spring-boot spring-data-jpa


【解决方案1】:

在引用嵌套字段之前必须使用下划线。试试:

public List<Appointment> findByScheduledAppointments_EmployeeAccounts_Id(Long id)

【讨论】:

  • 感谢您的回复,我已经尝试过了,它仍然返回一个空列表(即使数据库中有条目)
  • 尝试下划线的末尾带有“In”。见stackoverflow.com/a/51081157/9080066
  • 只有在名称冲突时才需要下划线。
【解决方案2】:

是的,将单词 In 添加到您的方法中,并提供 Set&lt;Long&gt; 作为employeeAccountIds。

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

【讨论】:

    猜你喜欢
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 2019-04-04
    相关资源
    最近更新 更多