2015-12-21 48 views
1

我有ManyToMany關聯生成的MySQL表。 但是當我運行此查詢JPQL ManyToMany查詢問題(加入類)

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

我得到這個錯誤

13:11:51959 ERROR [org.jboss.as.ejb3.tx.CMTTxInterceptor(HTTP-本地主機-127.0。 0.1-8080-6)javax.ejb.EJBTransactionRolledbackException:org.hibernate.QueryException:無法解析屬性:的listEmploye:metier.entities.Employe [從metier.entities.Employe E選擇é聯接e.listEmploye℃的內部將C。角色r其中r.IdRole = 1] 13:11:51960 ERROR [org.jboss.ejb3.invocation](HTTP-本地主機 - 127.0.0.1-8080-6)JBAS014134:EJB調用失敗有關組件Utilisateur爲方法public AB加強海峽java.util.List的metier.sess.IUtilisateurLocal.RoleEmploye():javax.ejb.EJBTransactionRolledbackException:org.hibernate.QueryException:無法解析屬性:的listEmploye:metier.entities.Employe [從metier.entities.Employe選擇e Ë加入e.listEmploye℃的內部加入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 
}} 

感謝您的幫助

+0

當你問sql查詢問題,請發表您的架構也。 – Thanga

回答

2

對於一個多對多的,你應該定義一個jointable上擁有側如下:

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

這是你的情況,這映射應該是對員工的實體。

爲什麼你從角色到員工的參考?我不明白爲什麼你會不會想使用它,所以我建議你改變這種雙向映射到單向的(所以剛落員工列表中的角色實體)

+0

非常感謝你,你是一個救生員 – Mustapha

+0

沒問題。我還有另外1個關於你的代碼的評論,雖然如果你會聽到我的話..不要EAGER獲取角色列表。這是一個不好的做法。 – steelshark

+0

謝謝你很多的你的話IVE改成了懶惰 – Mustapha