java - How to search mySql database table by foreign key using JPA? -
i have 2 classes (businessaccount , projects (shown below) mapped mysql database) 1:m relationship exists between businessaccounts , projects. inserting data database having problem when comes querying database. problem having have no getter or setter foreign key, 'contractor_id' in projects class. query want carry out return list of names of projects given businessaccount, searching foreign key reference in projects table. can no problem in mysql there no reference contractor_id java entity in projects class, i'm not sure how search within java class. (note: tried declare foreign key along getters , setters in projects class have these mapped 1:many relationship in class already, wouldn't compile flagged duplicate entities.) i'm sure it's obvious i'm missing appreciated!
public list<projects> getprojectlist() { factory = persistence.createentitymanagerfactory(persistence_unit_name); entitymanager em = factory.createentitymanager(); list<projects> projectlist = new arraylist<projects>(); em.gettransaction().begin(); string sessionemail=util.getemail(); query myquery = em.createquery("select u businessaccount u u.email=:email"); myquery.setparameter("email", sessionemail); list<businessaccount> userlist=myquery.getresultlist(); businessaccount account =userlist.get(0); query myquery2 = em.createquery("select distinct p.* businessaccount u " + "inner join projects p on p.contractor_id=:userid"); /*note p.contractor_id above refers entity in mysql database (and won't work obviously), want refer it's java equivalent not sure how that*/ myquery2.setparameter("userid", account.getid()); projectlist=myquery2.getresultlist(); em.gettransaction().commit(); em.close(); return projectlist; } @entity @table(name = "business_accounts") public class businessaccount { @id @generatedvalue(strategy = generationtype.identity) private int id; @column(name = "first_name") private string firstname; @column(name = "surname") private string surname; @onetomany(mappedby = "businessaccount", fetch = fetchtype.eager, cascade = { cascadetype.all }) private list<projects> projects; public int getid() { return id; } public void setid(int id) { this.id = id; } public string getfirstname() { return firstname; } public void setfirstname(string firstname) { this.firstname = firstname; } public string getsurname() { return surname; } public list<projects> getprojects() { if (projects == null) { projects = new arraylist<projects>(); } return projects; } public void setprojects(list<projects> projects) { this.projects = projects; } } @entity @table(name = "projects") public class projects { @id @generatedvalue(strategy = generationtype.identity) private int project_id; @column(name = "project_name") private string projectname; @manytoone(fetch = fetchtype.lazy) @joincolumns({ @joincolumn(name = "contractor_id", referencedcolumnname="id") }) private businessaccount businessaccount; public businessaccount getbusinessaccount() { if (businessaccount == null) { businessaccount = new businessaccount(); } return businessaccount; } public void setbusinessaccount(businessaccount businessaccount) { this.businessaccount = businessaccount; } public int getproject_id() { return project_id; } public void setproject_id(int project_id) { this.project_id = project_id; } public string getprojectname() { return projectname; } public void setprojectname(string projectname) { this.projectname = projectname; } }
the jpa query (you need use relation property, no need foreign key - please try, may need tweaking):
select p businessaccount u, in(u.projects) p u.id=:userid
but need query? can related projects property:
businessaccount account = ... list<projects> projectlist = account.getprojects();
Comments
Post a Comment