尝试将 SQL 查询转换为 JPA 查询
Trying to Convert a SQL Query to a JPA Query
我想将此 sql 查询转换为 JPA 查询,但我似乎无法理解它...我应该使用 findByMarinaIdAndMovementGroupMeanId 吗?或 findByMarinaIdAndMovementGroupMeanIdAndMovementMeanId??
Sql:
select m.* from movement_group m
join movement_group_mean mgm on m.id = mgm.movement_group_id
join movement_mean mm on mgm.movement_mean_id = mm.id
where mm.id = 1 and m.marina_id = :marinaId and mm.active = true;
运动组:
@Entity
public class MovementGroup {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String code;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private Boolean active;
private String iconUrl;
@OneToMany(mappedBy = "movementGroup")
private Set<MovementGroupMean> movementGroupMeans;
@JsonIgnore
@ManyToOne()
@JoinColumn(name = "marina_id")
private Marina marina;
MovementGroupMean:
@Entity
public class MovementGroupMean {
@EmbeddedId
@JsonIgnore
private MovementGroupMeanPK movementGroupMeanPK;
@JsonBackReference
@ManyToOne
@JoinColumn(name = "movement_group_id", insertable = false, updatable = false)
private MovementGroup movementGroup;
@ManyToOne
@JoinColumn(name = "movement_mean_id", insertable = false, updatable = false)
private MovementMean movementMean;
运动平均值:
@Entity
public class MovementMean {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@Enumerated(EnumType.STRING)
private MovementMeanType movementMeanType;
private Boolean active;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
@JsonBackReference
@ManyToOne()
@JoinColumn(name = "marina_id")
private Marina marina;
不确定问题出在哪里,所以请原谅SQL->JPQL:
上的冗长解释
用您的实体名称替换您的 table 名称
- movement_group -> 运动组
将您的联接替换为 java 引用,让 JPA 使用您定义的关系映射。
- “在 m.id = mgm.movement_group_id 上加入 movement_group_mean mgm”变为“加入 m.movementGroupMeans mgm”
- "join movement_mean mm on mgm.movement_mean_id = mm.id 变为 "join mgm.movementMean mm"
唯一棘手的地方是您的实体没有为 marina_id 值定义基本映射。因此,要获得 m.marina_id,您必须使用 'marina' 引用并使用其可能的 ID 值:
"m.marina_id = :marinaId" -> "m.marina.id = :marinaId"
给你 JPQL:
"Select m from MovementGroup m join m.movementGroupMeans mgm join mgm.movementMean mm where mm.id = 1 and m.marina.id = :marinaId and mm.active = true"
我想将此 sql 查询转换为 JPA 查询,但我似乎无法理解它...我应该使用 findByMarinaIdAndMovementGroupMeanId 吗?或 findByMarinaIdAndMovementGroupMeanIdAndMovementMeanId??
Sql:
select m.* from movement_group m
join movement_group_mean mgm on m.id = mgm.movement_group_id
join movement_mean mm on mgm.movement_mean_id = mm.id
where mm.id = 1 and m.marina_id = :marinaId and mm.active = true;
运动组:
@Entity
public class MovementGroup {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String code;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private Boolean active;
private String iconUrl;
@OneToMany(mappedBy = "movementGroup")
private Set<MovementGroupMean> movementGroupMeans;
@JsonIgnore
@ManyToOne()
@JoinColumn(name = "marina_id")
private Marina marina;
MovementGroupMean:
@Entity
public class MovementGroupMean {
@EmbeddedId
@JsonIgnore
private MovementGroupMeanPK movementGroupMeanPK;
@JsonBackReference
@ManyToOne
@JoinColumn(name = "movement_group_id", insertable = false, updatable = false)
private MovementGroup movementGroup;
@ManyToOne
@JoinColumn(name = "movement_mean_id", insertable = false, updatable = false)
private MovementMean movementMean;
运动平均值:
@Entity
public class MovementMean {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@Enumerated(EnumType.STRING)
private MovementMeanType movementMeanType;
private Boolean active;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
@JsonBackReference
@ManyToOne()
@JoinColumn(name = "marina_id")
private Marina marina;
不确定问题出在哪里,所以请原谅SQL->JPQL:
上的冗长解释用您的实体名称替换您的 table 名称
- movement_group -> 运动组
将您的联接替换为 java 引用,让 JPA 使用您定义的关系映射。
- “在 m.id = mgm.movement_group_id 上加入 movement_group_mean mgm”变为“加入 m.movementGroupMeans mgm”
- "join movement_mean mm on mgm.movement_mean_id = mm.id 变为 "join mgm.movementMean mm"
唯一棘手的地方是您的实体没有为 marina_id 值定义基本映射。因此,要获得 m.marina_id,您必须使用 'marina' 引用并使用其可能的 ID 值: "m.marina_id = :marinaId" -> "m.marina.id = :marinaId"
给你 JPQL:
"Select m from MovementGroup m join m.movementGroupMeans mgm join mgm.movementMean mm where mm.id = 1 and m.marina.id = :marinaId and mm.active = true"