JPQL ManyToMany查询问题(连接类)

时间:2015-12-21 12:23:56

标签: java mysql hibernate jpa jboss

我有关于ManyToMany关联生成的mysql的表。 但是当我运行这个查询时

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

我收到此错误

  

13:11:51,959 ERROR [org.jboss.as.ejb3.tx.CMTTxInterceptor](http-localhost-127.0.0.1-8080-6)javax.ejb.EJBTransactionRolledbackException:org.hibernate.QueryException:无法解析property:listEmploye of:metier.entities.Employe [select e from metier.entities.Employe e join e.listEmploye c inner join c.Role r where r.IdRole = 1]   13:11:51,960 ERROR [org.jboss.ejb3.invocation](http-localhost-127.0.0.1-8080-6)JBAS014134:EJB Invocation在组件Utilisateur上失败,方法public abstract java.util.List metier.sess.IUtilisateurLocal .RoleEmploye():javax.ejb.EJBTransactionRolledbackException:org.hibernate.QueryException:无法解析属性:listEmploye:metier.entities.Employe [select e from metier.entities.Employe e join e.listEmploye c inner join 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
}}

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

对于多对多,您应该在拥有方定义一个连接表,如下所示:

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

在您的情况下,这是应该启用此映射的员工实体。

为什么您有从角色到员工的参考?我不明白为什么你会想要使用它所以我建议你将双向映射改为单向映射(所以只需删除角色实体中的雇员列表)