[英]How to search mySql database table by foreign key using JPA?
我有2個類(BusinessAccount和Projects(如下所示)映射到MySql數據庫),其中BusinessAccounts和Projects之間存在1:M關系。 我成功地將數據插入數據庫,但在查詢數據庫時遇到了問題。 我遇到的問題是我沒有外鍵的getter或setter,在Projects類中沒有'contractor_id'。 我想要執行的查詢是通過在Projects表中搜索外鍵引用來返回給定BusinessAccount的所有項目的名稱列表。 我可以在mySQL中做到這一點沒有問題,但由於沒有引用contractor_id作為Projects類中的java實體,我不知道如何在我的java類中進行搜索。 (注意:我嘗試在Projects類中聲明外鍵以及getter和setter,但是因為我已經通過類中的1:Many關系映射了這些,所以它們不會被編譯,因為它們被標記為重復實體。)我確信這是顯而易見的,我很遺憾,但是非常感謝任何幫助!
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 FROM BusinessAccount u WHERE u.email=:email");
myQuery.setParameter("email", sessionEmail);
List<BusinessAccount> userList=myQuery.getResultList();
BusinessAccount account =userList.get(0);
Query myQuery2 = em.createQuery("SELECT distinct p.* FROM BusinessAccount u "
+ "INNER JOIN Projects p ON p.contractor_id=:userID");
/*Note p.contractor_id above refers to the entity in the
mysql database (and won't work obviously), I want to refer
to it's java equivalent but am not sure how to do 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;
}
}
JPA查詢就像(你需要使用關系屬性,但不需要外鍵本身 - 請嘗試,它可能需要一些調整):
SELECT p FROM BusinessAccount u, IN(u.projects) p WHERE u.id=:userId
但你真的需要查詢嗎? 您可以從酒店獲取相關項目:
BusinessAccount account = ...
List<Projects> projectList = account.getProjects();
嘗試這個:
Query myQuery2 = em.createQuery("SELECT distinct p.* FROM BusinessAccount u "
+ "INNER JOIN Projects p ON p.businessAccount=:businessAccount");
myQuery2.setParameter("businessAccount", account);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.