簡體   English   中英

JPQL ManyToMany查詢問題(聯接類)

[英]JPQL ManyToMany Query issue (join Class)

我有關於ManyToMany關聯生成的mysql表。 但是當我運行這個查詢

select e from Employe e join e.listeRole c inner join c.Role r where r.IdRole = 1

我得到這個錯誤

13:11:51,959錯誤[org.jboss.as.ejb3.tx.CMTTxInterceptor](http-localhost-127.0.0.1-8080-6)javax.ejb.EJBTransactionRolledbackException:org.hibernate.QueryException:無法解析屬性:listEmploye作者:metier.entities.Employe [從metier.entities.Employe中選擇e,然后加入e.listEmploye c內部聯接c.Role r,其中r.IdRole = 1] 13:11:51,960錯誤[org.jboss.ejb3.invocation] (http-localhost-127.0.0.1-8080-6)JBAS014134:方法公用抽象java.util.List metier.sess.IUtilisateurLocal.RoleEmploye():javax.ejb.EJBTransactionRolledbackException:org.hibernate的組件Utilisateur上的EJB調用失敗。 QueryException:無法解析屬性:listEmploye,屬於:metier.entities.Employe [從metier.entities.Employe中選擇e加入e.listEmploye c內部連接c.Role r,其中r.IdRole = 1]

我的課是

 @Entity
 public class Role {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer IdRole;
private String Intitule;

@ManyToMany(fetch=FetchType.EAGER,mappedBy="listeRole")
private List<Employe> listEmploye=new ArrayList<Employe>();

public Integer getIdRole() {
    return IdRole;
}

public void setIdRole(Integer idRole) {
    IdRole = idRole;
}

public String getIntitule() {
    return Intitule;
}

public void setIntitule(String intitule) {
    Intitule = intitule;
}

public List<Employe> getListEmploye() {
    return listEmploye;
}

public void setListEmploye(List<Employe> listEmploye) {
    this.listEmploye = listEmploye;
}

public Role(String intitule) {
    super();
    Intitule = intitule;
}


public Role() {
    super();
    // TODO Auto-generated constructor stub
}
}

@Entity
@DiscriminatorValue(value="employe")
public class Employe extends Utilisateur{
private String TypeEmploye;
private Integer authentification;
private Date logindate;
private Date logoutDate;    
//Employe class
@ManyToMany(fetch=FetchType.EAGER)
private List<Role> listeRole=new ArrayList<Role>();
public String getTypeEmploye() {
    return TypeEmploye;
}
public void setTypeEmploye(String typeEmploye) {
    TypeEmploye = typeEmploye;
}
public Integer getAuthentification() {
    return authentification;
}
public Date getLogindate() {
    return logindate;
}
public void setLogindate(Date logindate) {
    this.logindate = logindate;
}
public Date getLogoutDate() {
    return logoutDate;
}
public void setLogoutDate(Date logoutDate) {
    this.logoutDate = logoutDate;
}
public void setAuthentification(Integer authentification) {
    this.authentification = authentification;
}
public List<Role> getListeRole() {
    return listeRole;
}
public void setListeRole(List<Role> listeRole) {
    this.listeRole = listeRole;
}
public Employe(String username, String password, String email,
        boolean statut, String typeEmploye, Integer authentification,
        List<Role> listeRole) {
    super(username, password, email, statut);
    TypeEmploye = typeEmploye;
    this.authentification = authentification;
    this.listeRole = listeRole;
}
public Employe() {
    super();
    // TODO Auto-generated constructor stub
}}

謝謝你的幫助

對於多對多,您應該在擁有方上定義一個可連接項,如下所示:

 @ManyToMany
 @JoinTable(name = "emp_roles", joinColumns = {@JoinColumn(name = "emp_id",
               referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "role_id",
               referencedColumnName = "id")})
  private List<Role> listeRole;

在您的情況下,該雇員實體應啟用此映射。

為什么從角色到員工都有參考? 我看不到為什么要使用它,因此建議您將雙向映射更改為單向映射(因此只需將角色列表中的雇員列表刪除)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM