我正在使用带有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;
答案 0 :(得分:0)
解决:
需要使用Employee对象而不是Long employeeId:
ParameterExpression<Employee> param = criteriaBuilder.parameter(Employee.class);
..
queryRating.setParameter(param, employee);