无法将java.lang.Long字段com.jobs.spring.domain.Employee.id设置为java.lang.Long

时间:2016-08-22 21:17:16

标签: java sql hibernate jpa

我正在使用带有JPA2 / Hibernate5和MySQL的Java 8。

我收到以下错误,感谢任何帮助。

PropertyAccessException: Error accessing field [private java.lang.Long com.jobs.spring.domain.Employee.id] by reflection for persistent property [com.jobs.spring.domain.Employee#id] : 31

IllegalArgumentException: Can not set java.lang.Long field com.jobs.spring.domain.Employee.id to java.lang.Long

+--------+    +----------------+    +----------+ 
| Rating |    | Rating_Employee|    | Employee |
+--------+    +----------------+    +----------+ 
| ID     |    | RAT_ID         |    | ID       |
| REVIEW |    | EMP_ID         |    +----------+ 
+--------+    +----------------+    

@Override
public List<Rating> findAll(int firstResult, int maxResults, Long employeeId) {
    final EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory();
    final CriteriaBuilder criteriaBuilder = entityManagerFactory.getCriteriaBuilder();
    CriteriaQuery<Rating> criteria = criteriaBuilder.createQuery(Rating.class);
    Root<Rating> root = criteria.from(Rating.class);
    ParameterExpression<Long> param = criteriaBuilder.parameter(Long.class);
    criteria.select(root).where(criteriaBuilder.equal(root.get("employee"), param));
    TypedQuery<Rating> queryRating = entityManager.createQuery(criteria);
    queryRating.setParameter(param, employeeId);
    List<Rating> results = queryRating.getResultList();

    return results;
}

模型(Rating.java)

@OneToOne(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
@JoinTable
(
    name="rating_employee",
    joinColumns={ @JoinColumn(name="RAT_ID", referencedColumnName="ID") },
    inverseJoinColumns={ @JoinColumn(name="EMP_ID", referencedColumnName="ID") }
)
private Employee employee;

模型(Employee.java)

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

1 个答案:

答案 0 :(得分:0)

解决:

需要使用Employee对象而不是Long employeeId:

ParameterExpression<Employee> param = criteriaBuilder.parameter(Employee.class);
..
queryRating.setParameter(param, employee);