Java PropertyAccessException:无法通过反射获取器获取字段值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18034822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:56:41  来源:igfitidea点击:

PropertyAccessException: could not get a field value by reflection getter

javahibernatemany-to-one

提问by Wit Mesh

I have a many to one mapping with two classes (code is reduced):

我有两个类的多对一映射(代码减少了):

Category:

类别:

@Entity
public class Category {

    @Id
    @Column(name = "CATEGORY_ID")
    Long id;

    @NotNull
    String name;

Subcategory:

子类别:

@Entity
public class Subcategory {

    @Id
    @Column(name = "SUBCATEGORY_ID")
    Long id;

    @NotNull
    @ManyToOne(targetEntity = Category.class)
    @JoinColumn(name = "CATEGORY_ID")
    Long categoryId;

    @NotNull
    String name;

When I try to add subcategory to existing category, I get

当我尝试将子类别添加到现有类别时,我得到

ERROR [org.jboss.ejb3.invocation] JBAS014134: EJB Invocation failed on component SubcategoryController for method public void %package%.SubcategoryController.add(%package%.Subcategory): javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of %package%.Category.id
...
    at %package%.SubcategoryController$$$view1.add(Unknown Source)
...
    at %package%.SubcategoryController$Proxy$_$$_Weld$Proxy$.add(SubcategoryController$Proxy$_$$_Weld$Proxy$.java)
    at %package%.SubcategoryService.add(SubcategoryService.java:30)
    at %package%.SubcategoryService$Proxy$_$$_WeldClientProxy.add(SubcategoryService$Proxy$_$$_WeldClientProxy.java)
...
Caused by: javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of %package%.Category.id

What should I do to awoid this error?

我应该怎么做才能避免这个错误?

回答by Dragos Stratulat

In your category class it should be an OneToMany annotation like this :

在您的类别类中,它应该是这样的 OneToMany 注释:

@Entity
public class Category {    
    @Id
    @Column(name = "CATEGORY_ID")
    Long id;    
    @NotNull
    String name;        
    @OneToMany(mappedBy = "category")
    List<Subcategory> subcategories;    
}

You might also want to check out:
www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example-annotation/

您可能还想查看:
www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example-annotation/

回答by Alexandru Severin

Situations where this exception might occur:

可能发生此异常的情况:

1)name of database table is incorrect or missing:

1)数据库表名不正确或缺失:

@Entity
@Table(name = "category_table")    // name of database table
public class Category

2)Field class doesn't match the target class:

2)字段类与目标类不匹配:

@ManyToOne(targetEntity = Category.class)   // Target class
@JoinColumn(name = "CATEGORY_ID")
Category categoryId;                        // Target field

In your case categoryIdis of type Long, and hibernate is trying to insert Category.classfields into categoryIdbut can't.

在您的情况下categoryId是 type Long,并且 hibernate 试图将Category.class字段插入categoryId但不能。