gpt4 book ai didi

java - JPA 存储库属性表达式

转载 作者:行者123 更新时间:2023-12-02 02:04:02 24 4
gpt4 key购买 nike

在我的 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 控制台输出:

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

最佳答案

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

public List<Appointment> findByScheduledAppointments_EmployeeAccounts_Id(Long id)

关于java - JPA 存储库属性表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51080713/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com