Java 筛选出JPA查询中的嵌套对象
我正在努力筛选JPA存储库查询中的第三级嵌套对象 我有以下对象,其中我希望接收id=5的旅程中的所有PROM,并且只接收来自id=12的用户的PROM测量对象Java 筛选出JPA查询中的嵌套对象,java,spring-boot,jpa,spring-data-jpa,Java,Spring Boot,Jpa,Spring Data Jpa,我正在努力筛选JPA存储库查询中的第三级嵌套对象 我有以下对象,其中我希望接收id=5的旅程中的所有PROM,并且只接收来自id=12的用户的PROM测量对象 proms: [ { id: 1, name: "test", journeys: [ { id: 5 }, { id: 6 }, ] promMeasurements: [ {
proms: [
{
id: 1,
name: "test",
journeys: [
{
id: 5
},
{
id: 6
},
]
promMeasurements: [
{
id: 101,
value: 6,
user: {
id: 12
}
},
{
id: 102,
value: 2,
user: {
id: 13
}
},
{
id: 103,
value: 8,
user: {
id: 14
}
},
]
},
{
id: 2,
name: "test 2",
journeys: [
{
id: 5
},
{
id: 6
},
]
promMeasurements: [
{
id: 223,
value: 1,
user: {
id: 12
}
},
{
id: 224,
value: 5,
user: {
id: 13
}
},
{
id: 225,
value: 3,
user: {
id: 14
}
},
]
},
{
id: 3,
name: "test 3",
journeys: [
{
id: 7
}
]
promMeasurements: [
{
id: 223,
value: 1,
user: {
id: 26
}
},
{
id: 224,
value: 5,
user: {
id: 33
}
},
{
id: 225,
value: 3,
user: {
id: 4
}
},
]
}
]
因此,这一切的结果将是最终实现以下目标:
proms: [
{
id: 1,
name: "test",
journeys: [
{
id: 5
}
]
promMeasurements: [
{
id: 101,
value: 6,
user: {
id: 12
}
}
]
},
{
id: 2,
name: "test 2",
journeys: [
{
id: 5
}
]
promMeasurements: [
{
id: 223,
value: 1,
user: {
id: 12
}
}
]
}
]
现在,我已经能够使用以下查询对旅程进行过滤:
List<Prom> findAllByJourneysId(Long id);
当我手动执行这个函数时,我得到以下结果。
如您所见,每个“prom”显示两次,这已经不正确了
有人能帮我吗
实体:
public class Prom extends DateAudit {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "prom_journey", joinColumns = @JoinColumn(name = "prom_id"),
inverseJoinColumns = @JoinColumn(name = "journey_id"))
@JsonBackReference(value = "prom-journeys")
private Set<Journey> journeys = new HashSet<>();
@OneToMany(mappedBy = "prom")
@JsonManagedReference(value = "prom-prom-measurements")
private List<PromMeasurement> promMeasurements;
}
public class PromMeasurement extends DateAudit {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.EAGER)
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
@JsonBackReference(value = "prom-prom-measurements")
private Prom prom;
@ManyToOne(fetch = FetchType.LAZY)
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
@JsonBackReference(value = "patient-prom-measurements")
private User patient;
@ManyToOne(fetch = FetchType.LAZY)
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
@JsonBackReference(value = "journey-prom-measurements")
private Journey journey;
private Integer value;
}
public class User extends DateAudit {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
}
public class Journey extends DateAudit {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
}
公共类Prom扩展日期审核{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长id;
私有字符串名称;
@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(name=“prom\u旅程”,joinColumns=@JoinColumn(name=“prom\u id”),
inverseJoinColumns=@JoinColumn(name=“旅程id”))
@JsonBackReference(value=“prom行程”)
私有集行程=新HashSet();
@OneToMany(mappedBy=“prom”)
@JsonManagedReference(value=“prom测量”)
私人名单;
}
公共类审计{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长id;
@manytone(fetch=FetchType.EAGER)
@JsonIgnoreProperties({“HibernateLazInitializer”,“handler”})
@JsonBackReference(value=“prom测量”)
私人舞会;
@manytone(fetch=FetchType.LAZY)
@JsonIgnoreProperties({“HibernateLazInitializer”,“handler”})
@JsonBackReference(value=“患者prom测量”)
私人用户病人;
@manytone(fetch=FetchType.LAZY)
@JsonIgnoreProperties({“HibernateLazInitializer”,“handler”})
@JsonBackReference(value=“行程prom测量”)
私人旅行;
私有整数值;
}
公共类用户审计{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长id;
}
公共类审计{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长id;
}
不确定您使用的是哪个版本的spring boot,但在版本2.x中,下划线是必需的
问题可能来自于这样一个事实,即您混合了两个下划线:
PromMeasurements_Patient_Id
不带下划线的部分查询:
findAllByJourneysId
尽量保持查询语言使用的一致性。您可以使用@query注释进行尝试
@Query(value = "SELECT distinct p FROM Prom p JOIN FETCH p.journeys pj JOIN FETCH p.promMeasurements pp JOIN FETCH pp.patient ppp WHERE pj.id = :journeyId AND ppp.id = :patientId")
列出FindDintintByJourneySid和PromMeasurementsPatientId(Long-journeyId,Long-patientId) 有什么异常吗?没有,只是收到了与查询findAllByJourneysId相同的结果。好的,你能添加实体定义吗?我添加了它们:)你能添加执行的查询日志吗?好的,谢谢你澄清了这一部分,但不幸的是,省略下划线并不能起到作用。另外,当我将鼠标悬停在该方法上时,它会显示以下内容:“预期参数类型:旅程,测量”
findAllByJourneysId
@Query(value = "SELECT distinct p FROM Prom p JOIN FETCH p.journeys pj JOIN FETCH p.promMeasurements pp JOIN FETCH pp.patient ppp WHERE pj.id = :journeyId AND ppp.id = :patientId")