[英]Construct JPA query for multiple OneToMany relations
我正在努力解决以下问题,我正在尝试使用一种方法按多个字段进行搜索,可选择仅输入一个字段或少于所有字段:
下一段代码有效:
@Query("select s from Person s join s.socialMediaRecordsCollection t where (t.iDSocialMediaAccount.acountLink = :account)")
List<Person> findByAccountLink(@Param("account")String account);
虽然下一个代码返回一个空列表:
@Query("select DISTINCT s from Person s " +
"join s.telephoneRecordsCollection t " +
"join s.socialMediaRecordsCollection a where " +
"(?1 is null or s.firstName like %?1%) and " +
"(?2 is null or s.lastName like %?2%) and " +
"(?3 is null or t.iDTelephone.number = ?3) and " +
"(?4 is null or a.iDSocialMediaAccount.acountLink = ?4)")
List<Person> findAllByAllFields(String firstName, String lastName, String telephone, String account);
我不知道我做错了什么。 我的实体是:
@Table(name = "Person")
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "ID")
private Integer id;
@Column(name = "FirstName")
private String firstName;
@Column(name = "LastName")
private String lastName;
@JsonIgnore
@OneToMany(cascade = CascadeType.ALL, mappedBy = "idperson")
private Collection<SocialMediaRecords> socialMediaRecordsCollection;
@JsonIgnore
@OneToMany(cascade = CascadeType.ALL, mappedBy = "idperson")
private Collection<TelephoneRecords> telephoneRecordsCollection;
@Table(name = "SocialMediaRecords")
public class SocialMediaRecords implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "ID")
private Integer id;
@JoinColumn(name = "IDSocialMediaAccount", referencedColumnName = "ID")
@ManyToOne(optional = false)
private SocialMediaAccount iDSocialMediaAccount;
@JsonIgnore
@JoinColumn(name = "IDPERSON", referencedColumnName = "ID")
@ManyToOne(optional = false)
private Person idperson;
@Table(name = "TelephoneRecords")
public class TelephoneRecords implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "ID")
private Integer id;
@JoinColumn(name = "IDPERSON", referencedColumnName = "ID")
@ManyToOne(optional = false)
private Person idperson;
@JoinColumn(name = "IDTelephone", referencedColumnName = "ID")
@ManyToOne(optional = false)
private Telephone iDTelephone;
我用 Netbeans 从数据库中生成了实体。 我完全迷路了......查询看起来像这样:
Hibernate: select distinct person0_.ID as id1_9_, person0_.FirstName as firstnam4_9_, person0_.LastName as lastname6_9_ from PERSON person0_ inner join TelephoneRecords telephoner1_ on person0_.ID=telephoner1_.IDPERSON inner join SocialMediaRecords socialmedi3_ on person0_.ID=socialmedi3_.IDPERSON cross join Telephone telephone4_ cross join SocialMediaAccount socialmedi9_ where telephoner1_.IDTelephone=telephone4_.ID and socialmedi3_.IDSocialMediaAccount=socialmedi9_.ID and (? is null or person0_.FirstName like ?) and (? is null or person0_.LastName like ?) and (? is null or telephone4_.Number=?) and (? is null or socialmedi9_.AcountLink=?)
尝试使用左连接而不是连接:
@Query("select DISTINCT s from Person s " +
"left join s.telephoneRecordsCollection t " +
"left join s.socialMediaRecordsCollection a " +
"left join t.iDTelephone tel " +
"left join a.iDSocialMediaAccount acc " +
"where (?1 is null or s.firstName like %?1%) and " +
"(?2 is null or s.lastName like %?2%) and " +
"(?3 is null or tel.number = ?3) and " +
"(?4 is null or acc.acountLink = ?4)")
List<Person> findAllByAllFields(String firstName, String lastName, String telephone, String account);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.