获取错误org.hibernate.QueryException:非法尝试取消引用集合

时间:2015-09-02 03:59:54

标签: java spring hibernate jpa hibernate-mapping

我有一个有很多员工的复习班。

Review.java中的关联是

    @ManyToMany(targetEntity = Employee.class, cascade = { CascadeType.ALL })
    @JoinTable(name = "review_employee", 
            joinColumns = { @JoinColumn(name = "reviewid") }, 
            inverseJoinColumns = { @JoinColumn(name = "empid") })
    private Set<Employee> employees;

当我尝试从Review中访问员工时,我收到以下错误

   org.hibernate.QueryException: illegal attempt to dereference collection [review0_.id.employees] with element property reference [id] [FROM com.abc.model.Review r  LEFT JOIN fetch r.employees WHERE r.id = 1 AND ( r.employees.id = 11 )]
   org.hibernate.QueryException.generateQueryException(QueryException.java:137)

感谢您的帮助。

更新

  

Employee.java

public class Employee  {
/**
 * 
 */
private static final long serialVersionUID = 1L;

@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "empfirstname", nullable = false)
private String empFirstName;

@Column(name = "emplastname", nullable = false)
private String empLastName;

@Column(name = "empdesignation", nullable = false)
private String empDesignation;

@OneToOne
@JoinColumn(name = "employeetypeid", nullable = true)
private EmployeeType employeeType;

@Column(name = "employeeImageFileName", nullable = false)
private String employeeImageFileName;

private Boolean deleted;

...Getters & Setters...
}
  

Review.java

public class Review {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToMany(targetEntity = Employee.class, cascade = { CascadeType.ALL })
@JoinTable(name = "review_employee", 
            joinColumns = { @JoinColumn(name = "reviewid") }, 
            inverseJoinColumns = { @JoinColumn(name = "empid") })
private Set<Employee> employees;

@Column(name = "customerfirstname", nullable = false)
private String customerFirstName; 

@Column(name = "customerlastname", nullable = false)
private String customerLastName;

@OneToOne
@JoinColumn(name = "starratingid", nullable = false)
private StarRating starRating;

@Column(name = "reviewtitle", nullable = false)
private String reviewTitle;

@Column(name = "review", nullable = false)
@Type(type="text")
private String review;

 ...Getters & Setters...
}

2 个答案:

答案 0 :(得分:3)

您可以尝试JOIN'ed员工的员工参考,而不是评论员工。试试这个:

FROM com.abc.model.Review r LEFT JOIN fetch r.employees reviewEmp WHERE r.id = 1 AND reviewEmp.id = 11

答案 1 :(得分:1)

您的以下查询

 FROM com.abc.model.Review r LEFT JOIN fetch r.employees WHERE r.id = 1 AND ( r.employees.id = 11 )

应该像这样修改

FROM com.abc.model.Review r LEFT JOIN fetch r.employees emp WHERE r.id = 1 AND ( emp.id = 11 )