【问题标题】:How insert automatic ManyToMany with OpenJPA如何使用 OpenJPA 插入自动多对多
【发布时间】: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


【解决方案1】:

尝试将@JoinTable 移动到UserProfile(相应地更改参数)并将cascade={CascadeType.ALL} 添加到@ManyToMany 注释中。

这应该可以解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-25
    • 2019-04-10
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    • 2011-03-05
    相关资源
    最近更新 更多