Hibernate抛出异常:
org.hibernate.PropertyAccessException:
could not get a field value by reflection getter of test.model.Part.part
部分实体类:
@Table(name = "part")
@Entity
public class Part implements Serializable {
@Id @GeneratedValue
@Column(name = "part")
private int part;
public int getPart() {
return part;
}
public void setPart(int part) {
this.part = part;
}
@OneToMany(mappedBy = "part")
private Collection<ProductPart> productPartsByPart;
public Collection<ProductPart> getProductPartsByPart() {
return productPartsByPart;
}
.....
}
似乎实体类具有正确的注释,但Hibernate无法访问id
表中Part
和主键的属性。怎么解决?
它被称为:
ProductPart productPart = (ProductPart) sessionFactory
.getCurrentSession()
.createCriteria(ProductPart.class)
.add(Restrictions.eq("part", cPartId))
.add(Restrictions.eq("product", productId)).uniqueResult();
更新
Caused by: java.lang.IllegalArgumentException:
Can not set int field Part.part to java.lang.Integer
更新2:
我有product_part
表,其中 fk 并在part
表中引用了part
。我必须使用@OneToMany(mappedBy = "part")
吗?
@Entity
@Table (name = "part")
public class Part implements Serializable {
@Id @GeneratedValue
@Column(name = "part")
@OneToMany(mappedBy = "part")
private int part;
UPDATE3
当我声明我只需要在Part
表上执行搜索时,我不明白为什么Hibernate 会尝试访问 product_part
表。它也没有必要访问其他表,因为我只需要product_part中的字段,该字段为 part 和 product (这两个都是外键)赋值。 ..
UPDATE4:
@Entity
@Table(name = "product_part")
public class ProductPart implements Serializable {
@Id
@GeneratedValue
@Column(name = "id")
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@ManyToOne
@JoinColumn(name = "product", referencedColumnName = "product", nullable = false)
private Product product;
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
@JoinColumn(name = "part", referencedColumnName = "part", nullable = false)
@ManyToOne
private Part part;
public Part getPart() {
return part;
}
public void setPart(Part partByPart) {
this.part = partByPart;
}
}
答案 0 :(得分:1)
您的产品零件中如何引用您的零件?
应该是像
这样的东西ProductPart{
@ManyToOne
private Part part;
}
猜是这样
将.add(Restrictions.eq("part", cPartId))
更改为.add(Restrictions.eq("part.part", cPartId))
修改强>
ProductPart productPart = (ProductPart) sessionFactory
.getCurrentSession()
.createCriteria(ProductPart.class)
.add(Restrictions.eq("part.part", cPartId))
.add(Restrictions.eq("product", productId)).uniqueResult();//here similarly access your Product's @Id
//something like product.productId