JPA Criteria Query,如何加入实体 - Hibernate -

时间:2015-04-29 14:31:38

标签: java hibernate jpa criteria-api

我制作了一个JPA项目(使用hibernate)。它比这复杂一点,但这里有两个类:

@Entity
@Table(name= "Persons")
public class Person {
    @Id
    @Column(nullable= false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Integer persId;
    @Column(nullable= false, length = 50)
    private String firstName;
    @Column(nullable= false, length = 50)
    private String surname;
    @Column(nullable= true, length = 50)
    private String emailAddress;

    @OneToMany(orphanRemoval = true, cascade = {CascadeType.REMOVE, CascadeType.MERGE})
    private List<PhoneNumber> phoneNumbers;

    @OneToOne(mappedBy="person", orphanRemoval = true, cascade = {CascadeType.REMOVE, CascadeType.MERGE})
    private Account account;

    //private Account account;
    public Person() {
        // empty
    }

    public Person(String firstName, String surname, String emailAddress) {

        this.firstName = firstName;
        this.surname = surname;
        this.emailAddress = emailAddress;
    }


    public Integer getPersId() {
        return persId;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getSurname() {
        return surname;
    }
    public void setSurname(String surname) {
        this.surname = surname;
    }
    public String getEmailAddress() {
        return emailAddress;
    }
    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }
    //public Account getAccount() {
    //    return account;
    //}
    //public void setAccount(Account account) {
    //    this.account = account;
    //}
    @Override
    public String toString() {
        return "Person [persId=" + persId + ", firstName=" + firstName + ", surname=" + surname + ", emailAddress=" + emailAddress + "]";
    }
}

@Entity
@Table(name="PhoneNumbers")
public class PhoneNumber {
    @Id
    @Column(nullable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer phoneId;
    //@Column(nullable= false)
    @ManyToOne
    private Person person;
    @Column(nullable=false)
    private String phoneNumber;

    //GETSETTERS

    public Integer getPhoneId() {
        return phoneId;
    }
    public Person getPerson() {
        return person;
    }
    public String getPhoneNumber() {
        return phoneNumber;
    }
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    //OVERRIDES

    @Override
    public boolean equals(Object obj)
    {
        if (obj instanceof PhoneNumber) {
            String num = ((PhoneNumber)obj).getPhoneNumber();
            return (num == this.phoneNumber);
        } else {
            return false;
        }
    }
    @Override
    public int hashCode(){
        return phoneNumber.hashCode();
    }

    public PhoneNumber(Person person, String phoneNumber){
        this.person = person;
        this.phoneNumber = phoneNumber;
    }
}

现在,我正在尝试做的是制作一个Criteria Query,它会给我一个电话号码与给定字符串相同的人。 我已经编写了一个测试,添加了一些示例数据并将其恢复,但我真的无法理解联接是如何工作的。

CriteriaQuery<Person> query = personService.criteriaBuilder().createQuery(Person.class);
        Root<Person> pRoot = query.from(Person.class);
Join<Person, PhoneNumber> pNumbers = pRoot.join(Person_.phoneNumbers);

这是正确的路线吗?如果是这样,它做了什么,以及我如何在“where”子句中添加这些字段?提前谢谢every1!

PS。整个项目包含更多文件 - personService是一个界面,您可以假设它的所有用途都是正确的 - 它只是我遇到问题的标准查询

0 个答案:

没有答案