【发布时间】:2016-01-25 16:10:55
【问题描述】:
我有 2 个使用 OpenJPA 映射的类。一类是User,它与AppProfile 有ManyToMany 关系。在数据库中,我有表关系 USER_APP_PROFILES (ID,User_ID,App_ID)。
我的班级打开 JPA 用户
@Table(name = "USER_PROFILE",schema = "BPMS")
public class UserProfile {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="USER_ID")
private Integer userID;
@ManyToMany(mappedBy="listUserProfile", fetch=FetchType.EAGER)
private List<AppProfile> listAppProfile;
}
我的班级AppProfile
@Table(name = "APP_PROFILE",schema = "BPMS")
public class AppProfile {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="APP_ID")
private Integer appID;
@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(
name="USER_APP_PROFILES",
joinColumns={@JoinColumn(name="APP_ID", referencedColumnName="APP_ID")},
inverseJoinColumns={@JoinColumn(name="USER_ID", referencedColumnName="USER_ID")})
private List<UserProfile> listUserProfile;
}
添加 List AppProfile 后,我通过 EntityManager 将用户提取到数据库中。
例子:
UserProfile userProfile //(populate with fetch in database)
AppProfile app = new App();
app.setAppID(11);
List<AppProfile> listApp = new ArrayList<AppProfile>();
listApp.add(app);
userProfile.setListAppProfile(listApp);
em.merge(userProfile)
如果我需要 JPA 自动插入表 USER_APP_PROFILES 中,我该如何合并:
User_App_Profile_ID : new register
UserID : 1
AppID : 11
【问题讨论】:
标签: java jpa many-to-many openjpa