我有两个类的多对一映射(代码减少):
类别:
@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
我应该怎么做才能解决这个错误?
答案 0 :(得分:1)
在您的类别类中,它应该是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/