【发布时间】:2019-07-22 02:49:37
【问题描述】:
我有实体:
第一
@Entity
@Getter
@Setter
@NoArgsConstructor
public class Technic implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String gosNumber;
private String invNumber;
private String shassisNumber;
private String engineNumber;
@Column(length = 100)
private String yearOfMake;
@ManyToOne
private Farm farm;
@JsonManagedReference
@ManyToOne
private TechGroup techGroup;
@JsonManagedReference
@ManyToOne
private TechType techType;
@JsonManagedReference
@ManyToOne
private TechMark techMark;
@JsonIgnore
@CreationTimestamp
@Column(name = "creation_date", updatable = false)
private LocalDateTime createdDate;
@JsonIgnore
@Column(name = "updated_date")
@UpdateTimestamp
private LocalDateTime updatedDate;
@JsonIgnore
@Column(columnDefinition = "Bool default false")
private Boolean isDel;
@JsonManagedReference
@OneToMany(mappedBy = "technic")
private List<TechnicStatus> technicStatusList = new ArrayList<>();
public List<TechnicStatus> getTechnicStatusList() {
return technicStatusList;
}
public void setTechnicStatus(TechnicStatus technicStatus) {
this.technicStatusList = new ArrayList<>();
this.technicStatusList.add(technicStatus);
}
第二:
@Entity
@Getter
@Setter
@NoArgsConstructor
public class TechnicStatus implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "technic_status_id")
private Long id;
@JsonBackReference
@ManyToOne
private Technic technic;
@JsonManagedReference
@ManyToOne
private Status status;
private Boolean isGarantia;
private Boolean isLizing;
private LocalDate visitedDate;
private LocalDate notWorkDate;
private String description;
@JsonIgnore
private boolean isActive;
@JsonIgnore
@CreationTimestamp
@Column(name = "creation_date", updatable = false)
private LocalDateTime createdDate;
}
我想从我的数据库中获取结果,其中包含每个对象技术中的列表我有列表 technicStatusList = new ArrayList(),我希望在其中只有值为 isActive=true 的 TechnicStatus。
为此,我对相同的 JPQL 查询:
TypedQuery<Technic> query = em.createQuery("Select t from Technic t join TechnicStatus ts on t.id = ts.technic.id where t.isDel=false and ts.isActive=true and t.farm.id=:farmId order by t.techGroup.name, t.techType.name, t.techMark.name", Technic.class);
但是得到一个包含 TechnicStatus 的结果,它返回一个带有真假的 TechnicStatus (TechnicStatus.isActive=true, TechnicStatus.isActive=false)。
我想得到这个原生查询的结果:
SELECT
*
FROM
technic
JOIN
technic_status ON technic.id = technic_status.technic_id
WHERE
technic.is_del = FALSE
AND technic_status.is_active = TRUE
AND technic.farm_id = 1722
【问题讨论】:
-
对于您的 JPA 查询,您需要执行
SELECT t, ts FROM ...广告从结果中提取t和ts。请注意,当您仅加载Technic时,这些实体的technicStatusList将包含 all 关联的状态实体 - 由于技术原因(例如 Hibernate 不知道该列表中的元素是否会表示全部或只是一个过滤的子集 - 这会在尝试将该列表的更改写回数据库时导致问题)。
标签: java mysql hibernate jpa jpql