java - JPA Criteria Query, how to join entities - Hibernate- -
i have made jpa project (using hibernate). it's bit more complex this, here 2 classes:
@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 + "]"; } }
and
@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; } }
now, i'm trying do, make criteria query, give me person who's telephone number same given string. have written test adding sample data , getting back, can't understand how join works here.
criteriaquery<person> query = personservice.criteriabuilder().createquery(person.class); root<person> proot = query.from(person.class); join<person, phonenumber> pnumbers = proot.join(person_.phonenumbers);
is correct line? , if so, do, , how later addres fields in "where" clause? in advance every1!
ps. whole project contains more files- personservice interface, can assume it's uses correct- criteria queries i'm having problem with
Comments
Post a Comment