Reputation: 161
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
@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);
}
Upvotes: 2
Views: 4395
Reputation: 719
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;
Upvotes: 1
Reputation: 1590
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.
Upvotes: 2
Reputation: 120771
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.
Upvotes: 0