Spring JPA loading - OneToMany relation causes an infinite loop

I have two objects with a simple @OneToMany relation that looks like this:

Parent:

@Entity public class ParentAccount { @Id @GeneratedValue private long id; private String name; @OneToMany(fetch = FetchType.EAGER, mappedBy = "parentAccount") private Set<LinkedAccount> linkedAccounts; } 

child:

 @Entity public class LinkedAccount { @Id @GeneratedValue private long id; @ManyToOne(optional = false) private ParentAccount parentAccount; private String name; // empty constructor for JPA public LinkedAccount() { } } 

I ma using Spring CrudRepository to work with these objects. However, when calling ParentAccount parent = parentAccountRepository.findOne(id); some kind of endless cycle and sleeping spam begins throughout the console:

 Hibernate: select linkedacco0_.parent_account_id as parent_a6_1_0_, linkedacco0_.id as id1_0_0_, linkedacco0_.id as id1_0_1_, linkedacco0_.aws_id as aws_id2_0_1_, linkedacco0_.key_id as key_id3_0_1_, linkedacco0_.name as name4_0_1_, linkedacco0_.parent_account_id as parent_a6_0_1_, linkedacco0_.secret_key as secret_k5_0_1_ from linked_account linkedacco0_ where linkedacco0_.parent_account_id=? 

I tried changing the sample type to LAZY, but then I get this error:

 org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.berrycloud.scheduler.model.ParentAccount.linkedAccounts, could not initialize proxy - no Session 

(He seems to be trying to do a lazy load outside of the transactional context).

This is my CRUD repository:

 @Repository public interface ParentAccountRepository extends CrudRepository<ParentAccount, Long> { } 

Can someone tell me how to solve this problem? I would prefer a solution with EAGER fetch. Thanks for any advice.

EDIT: here is the circuit I'm using

 CREATE TABLE parent_account ( id BIGINT auto_increment, name VARCHAR(80) null, PRIMARY KEY (`id`) ); CREATE TABLE linked_account ( id BIGINT auto_increment, parent_account_id BIGINT, name VARCHAR(80) null, FOREIGN KEY (`parent_account_id`) REFERENCES `parent_account` (`id`), PRIMARY KEY (`id`) ); 
+5
source share
3 answers

The problem is resolved. I used the native @toString method in LinkedAccount that referenced ParentAccount. I had no idea that this could cause any problems, and therefore I did not include toString in my question.

Apparently, this caused an endless loop of lazy loading and deleting this link fixed the problem.

+5
source

As follows from the first answer:

Do not use the Lombok @Data annotation in @Entity classes.

Reason: @Data generates hashcode() , equals() and toString() methods that use the generated getters. Using getter tools, of course, retrieving new data, even if the property was marked FetchType = LAZY .

Somewhere along the way, hibernate is trying to write data using toString() and it will work.

+7
source

Does something like this not work?

 @Entity public class Account { @Id @GeneratedValue private long id; private String name; @ManyToOne(cascade={CascadeType.ALL}) @JoinColumn(name="manager_id") private Account manager; @OneToMany((fetch = FetchType.EAGER, mappedBy="manager") private Set<Account> linkedAccounts = new HashSet<Account>(); } 
0
source

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


All Articles