JPA Hibernate - Issue with manytomany mapping with extra column in the mapping table

别说谁变了你拦得住时间么 提交于 2019-12-21 06:36:06

问题


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.

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 Method

@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);
}

回答1:


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

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;

}

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.




回答2:


I guess its too late to answer this question but adding for reader, kindly refer to this link

replace @JoinColumn with @PrimaryKeyJoinColumn so your code will look as follows


 @ManyToOne
 @PrimaryKeyJoinColumn(name = "bank_account_id", referencedColumnName = "referencedColumnName ")
 private BankAccount bankAccounts;



回答3:


If you want to save the association class when saving the bank or the user class, then you have to set the casscade attribute to the relatin at bank anx/or user class.



来源:https://stackoverflow.com/questions/4417355/jpa-hibernate-issue-with-manytomany-mapping-with-extra-column-in-the-mapping-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!