I am facing issues while retrieving data for entities having bi-directional many-to-many relationship. If I use List
for storing entities, I get unable to fetch multiple bags simultaneously error. If i change my code to use Set
, I get stackoverflow error.
Details :
- Spring 3.0.3
- Hibernate-core : 3.5.1-Final
- Hibernate-annotations : 3.5.1-Final
- hibernate-common-annotations : 3.2.0-Final
- hibernate-entitymanager : 3.5.1-Final
- Mysql database
- Junit 4
User has Many Bank Accounts; Bank Account can have many users
User.java
@ManyToMany(fetch = FetchType.EAGER, mappedBy="user")
private List<BankAccount> bankAccounts = new ArrayList<BankAccount>();
BankAccount.java
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "user_bankaccount",
joinColumns = @JoinColumn(name="bank_account_id"),
inverseJoinColumns = @JoinColumn(name = "user_id")
)
private List<User> user = new ArrayList<User>();
DB Tables
Users
user_id PK
Bankaccount
bank_account_id PK
user_bankaccount
bank_account_id PK ( references bankaccount.bank_account_id )
user_id PK ( references user.user_id )
issues
- when I try to get all the users data (
getAllUsers
) using a JUnit test case, I get unable to fetch multiple bags simultaneously error. - If I use
Set
andHashSet
instead of List andArrayList
respectively, I get stackoverflow error.
Please help me and let me know if code is wrong or its a known hibernate issue with specific version of libs that I am using.