JPA Hibernate-在映射表中有多余的列的情况下进行许多映射的问题 [英] JPA Hibernate - Issue with manytomany mapping with extra column in the mapping table

查看:128
本文介绍了JPA Hibernate-在映射表中有多余的列的情况下进行许多映射的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在映射表中有许多额外关系的情况下有一个额外的列时,我在保存记录时遇到了问题.有关详细信息,请参见下面.

I am facing problem while saving a record when i have an extra column in the mapping table in case of manytomany relationship. Please see below for details.

UserAccount和BankAccounts之间存在许多关系.除了User和BankAccount表的主键之外,user_bankaccounts的映射表还有一个名为user_type的额外列.但是,当我尝试保存银行帐户记录时,映射表数据未插入到user_bankaccount表中.仅插入银行帐户表.我相信我自己不应该更新映射表,休眠将自动更新相应的映射表.

There is manytomany relationship between User and BankAccounts. The mapping table for user_bankaccounts has an extra column called user_type, apart from the primary keys of User and BankAccount table. However, when i try to save a bank account record, mapping table data is not inserted in the user_bankaccount table. Only bankaccount table is inserted. I believe i should not update the mapping table myself and hibernate will update the corresponding mapping table automatically.

@Entity
@Table(name = "Users")
public class User implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int userId;

@OneToMany(mappedBy = "user", fetch = FetchType.EAGER)
private Set<UserBankAccounts> userBankSet = new HashSet<UserBankAccounts>();

}

@Entity
@Table(name = "bankaccounts")
public class BankAccount implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "bank_account_id")
private int accountId;

@OneToMany(mappedBy = "bankAccounts", fetch = FetchType.EAGER)
private List<UserBankAccounts> userBankList = new ArrayList<UserBankAccounts>();

}


@Entity
@Table(name = "user_bankaccount")
@IdClass(UserBankAccountAssociationId.class)
public class UserBankAccounts {

 @Id
 @Column(name = "user_id", insertable = false, updatable = false)
 private int userId;

 @Id
 @Column(name = "bank_account_id", insertable = false, updatable = false)
 private int accountId;

 @Column(name = "user_type")
 private String userType;

 @ManyToOne
 @JoinColumn(name="user_id")
 private User user;

 @ManyToOne
 @JoinColumn(name="bank_account_id")
 private BankAccount bankAccounts;

}

public class UserBankAccountAssociationId implements Serializable {

 private int userId;
 private int accountId;
}

测试方法

@Test
@Loggable(value = LogLevel.DEBUG)
public void addBankAccount() {

 Bank bank = bankdao.getBankByName("bank name").get(0);
 User u1 = userdao.getUserByName("xxx", "yyy").get(0);

 BankAccount ba = new BankAccount();
 ba.setAccountType("Savings");
 ba.setBank(bank);
 ba.setBranch("xxx");
 ba.setAccountNumber("112233445566");
 ba.setOpMode("xxx");

 UserBankAccounts userBankAcct = new UserBankAccounts();

 userBankAcct.setUser(u1);
 userBankAcct.setBankAccounts(ba);
 userBankAcct.setUserId(u1.getUserId());
 userBankAcct.setAccountId(ba.getAccountId());
 userBankAcct.setUserType("Primary User");

 u1.getUserBankSet().add(userBankAcct);
 ba.getUserBankList().add(userBankAcct);

 bankAccountDAO.addBankAccount(ba);
}

推荐答案

我认为您对Idclass注释有误解.您应该看一下这个例子. 多对多示例

I think you have a misunderstanding of Idclass annotation. You should take a look at this example. Many To Many sample

如果您使用示例中的映射,则hibernate将自动为您创建一个联接表.但是,如果要在联接表上增加额外的列(例如user_type),则必须自己创建联接表.

If you use a mapping as in the example, hibernate will automatically create a join table for you. However, if you want to have extra columns on your join table (like user_type), you have to create join table yourself.

@Entity
@Table(name = "user_bankaccount")
public class UserBankAccounts {

 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 @Column(name = "id")
 private Long id;

 @Column(name = "user_type")
 private String userType;

 @ManyToOne
 @JoinColumn(name="user_id")
 private User user;

 @ManyToOne
 @JoinColumn(name="bank_account_id")
 private BankAccount bankAccounts;

}

不需要userId和accountId字段.并且,如果您希望在银行帐户或用户持续存在时保存/删除/更新用户银行帐户,请在您的银行帐户或用户实体中使用级联注释.

There is no need for userId and accountId fields. And if you want userbankaccounts to be saved/deleted/updated when bankaccount or user persisted, use cascade annotation in your bankaccount or user entities.

这篇关于JPA Hibernate-在映射表中有多余的列的情况下进行许多映射的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆