gpt4 book ai didi

java - Hibernate 无法访问带注释的属性的值

转载 作者:行者123 更新时间:2023-12-01 12:30:46 24 4
gpt4 key购买 nike

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 无法访问属性 idPart 中的主键 table 。怎么解决?

它被调用:

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和引用文献partpart table 。我必须使用 @OneToMany(mappedBy = "part")

@Entity
@Table (name = "part")
public class Part implements Serializable {

@Id @GeneratedValue
@Column(name = "part")
@OneToMany(mappedBy = "part")
private int part;

更新3

我不明白为什么 Hibernate 尝试访问 Part当我声明我只需要在 product_part 上执行搜索时,表 table 。也没有必要访问其他表,因为我只需要 Product_part 中的字段,该字段为 partproduct (都是外键)给出了值....

更新4:

@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;
}
}

最佳答案

您的产品部件中如何引用您的部件?

应该是这样的

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

关于java - Hibernate 无法访问带注释的属性的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25935430/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com