如何在Hibernate中将bean属性映射为一对多

时间:2014-09-24 09:49:47

标签: java hibernate jpa

我有两个表vehicleregistergroupdetails,因为我有VehicleRegisterBean的bean,GroupDetails公共字段是groupid。这里有1组可以包含许多工具,我正在执行OnetoMany关系,因为我将bean定义为,

VehicleRegiserBean,

@Entity
@Table(name = "vehicle_register")
public class VehicleRegisterBean {
 // somefields

@ManyToOne
@JoinColumn(name="groupid", nullable=false)
private GroupDetails groupDetails;

//getter setters
}

GroupDetails,

@Entity
@Table(name = "group_details")
public class GroupDetails {

//some fields

@OneToMany(mappedBy="GroupDetails")
private Set<VehicleRegisterBean> vehicleRegisterBean;

//getters setters
}

但我得到例外,

org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.abc.its.beans.VehicleRegisterBean.GroupDetails in com.abc.its.beans.GroupDetails.vehicleRegisterBean
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:685)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:645)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:65)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1716)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1423)

任何人都可以帮我解决这个问题。

2 个答案:

答案 0 :(得分:3)

mappedBy属性的值代表VehicleRegisterBean类中groupDetails的字段,但您在GroupDetails中使用mappedBy

因此,hibernate试图在GroupDetails中查找属性VehicleRegisterBean,并说它无法找到该属性。这就是错误所说的:

mappedBy reference an unknown target entity property: 
com.abc.its.beans.VehicleRegisterBean.GroupDetails

答案 1 :(得分:2)

变化

@OneToMany(mappedBy="GroupDetails")

@OneToMany(mappedBy="groupDetails")

这是您在此声明的名称,

private GroupDetails groupDetails;