簡體   English   中英

PropertyAccessException:無法通過反射獲取器獲取字段值

[英]PropertyAccessException: could not get a field value by reflection getter

我有兩個類的多對一映射(減少了代碼):

類別:

@Entity
public class Category {

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

    @NotNull
    String name;

類別:

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

當我嘗試將子類別添加到現有類別時,我得到

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

我應該怎么做才能避免這個錯誤?

在您的類別類中,它應該是一個OneToMany注釋,如下所示:

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

您可能還想簽出:
www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example-annotation/

可能發生此異常的情況:

1)數據庫表名稱不正確或丟失:

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

2)欄位類別與目標類別不符:

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

在您的情況下, categoryId的類型為Long ,並且hibernate試圖將Category.class字段插入categoryId但不能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM