简体   繁体   中英

Construct JPA query for multiple OneToMany relations

I am struggling with the following issue, I am trying to search by multiple fields in a single method, with the option to only input one field or less than all fields:

This next piece of code works:

    @Query("select s from Person s join s.socialMediaRecordsCollection t where (t.iDSocialMediaAccount.acountLink = :account)")
        List<Person> findByAccountLink(@Param("account")String account);

While this next code returns an empty list:

@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);

I don't know what I am doing wrong. My entities are:

@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;

I generated the entities from the database with Netbeans. I am completely lost... The query looks like this:

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=?)

Try using left join instead of join:

@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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM