Hibernate映射1到多个

时间:2016-04-22 03:52:29

标签: java hibernate

假设我有一个应用程序,用户可以注册然后创建资产集合,因此,我可以假设主要有2个实体UserAsset1-to-ManyUserAsset的关系。

那么,对于上述场景,最合适的hibernate-mapping应该是什么,而且,如果我必须在一次通话中列出所有资产及其创建者详细信息,

这也应该是最好的方法?

请提供解决方案

1 个答案:

答案 0 :(得分:2)

如果您的项目中启用了注释。我们可以这样做:

//In User Entity class
@OneToMany(mappedBy="user") // "user" is the name of instance variable in Asset class
@LazyCollection(LazyCollectionOption.FALSE) // To avoid the LazyFetchException
private List<Asset> assetList;

// in Asset Entity class
@ManyToOne
@JoinColumn(name="user_id") // name specifies the name of column name in DB
private User user;