JPA with Hibernate 3 - Multipoint stack overflow and multiple sum errors

I am having problems retrieving data for objects that have a bi-directional many-to-many relationship. If I use Listto store entities, I cannot retrieve multiple amounts at the same time. If I change my code to use Set, I get a 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

The user has many bank accounts; A 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 )

the questions

  • (getAllUsers) JUnit, .
  • Set HashSet List ArrayList , stackoverflow.

, , libs, .

+5
3

( fetch). " ". JPA 2.0:

2.10.4 ManyToMany

...

:

@Entity
public class Project {
    private Collection<Employee> employees;

    @ManyToMany
    public Collection<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(Collection<Employee> employees) {
        this.employees = employees;
    }
    ...
}

@Entity
public class Employee {
    private Collection<Project> projects;

    @ManyToMany(mappedBy="employees")
    public Collection<Project> getProjects() {
        return projects;
    }
    public void setProjects(Collection<Project> projects) {
        this.projects = projects;
    }
    ...
}

:

  • Entity Project Entity Employee.
  • Entity Employee Entity Project.
  • Entity Project .

...

EAGER ( ?), JPA , . , .

Hibernate , Hibernate , Emmanuel Bernard

LAZY EAGER . Hibernate ,

, ( ). , , Hibernate - ( , EAGER ).

, : , , , Jira.

+2

, hibernate , , .. .

+1

, Lombok, Vjeetje. @Data, hashCode ToString , Hibernate. , , @EqualsAndHashCode (exclude = "_") @ToString (exclude = "_ "). .

0

Source: https://habr.com/ru/post/1770727/


All Articles