问题描述
我有一个简单的@OneToMany关系的两个对象,如下所示:
父:
@Entity
public class ParentAccount {
@Id
@GeneratedValue
private long id;
私人字符串名称;
@OneToMany(fetch = FetchType.EAGER,mappedBy =parentAccount)
private Set< LinkedAccount>关联账户;
$ b
儿童:
@Entity
public class LinkedAccount {
@Id
@GeneratedValue
private long id;
@ManyToOne(可选= false)
私人ParentAccount parentAccount;
私人字符串名称;
// JPA的空构造函数
public LinkedAccount(){
}
}
我使用Spring CrudRepository
来操作这些实体。但是,当调用 ParentAccount parent = parentAccountRepository.findOne(id);
时,某种无限循环开始发生,并且休眠在整个控制台中传播:
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_ ,将linked0_.name作为name4_0_1_,linkedacco0_.parent_account_id作为parent_a6_0_1_,linkedacco0_.secret_key作为secret_k5_0_1_,从linked_account linkedacco0_,其中linkedacco0_.parent_account_id =?
我尝试将获取类型更改为LAZY,但随后出现此错误:
org.hibernate.LazyInitializationException:无法懒惰地初始化一个角色集合:com.berrycloud.scheduler.model.ParentAccount.linkedAccounts,无法初始化代理 - 没有会话
(它似乎试图在事务性上下文之外执行延迟加载)。
这是我的CRUD存储库:
@Repository
公共接口ParentAccountRepository扩展CrudRepository< ParentAccount,Long> {
}
有人能告诉我如何解决这个问题吗?我更喜欢EAGER获取的解决方案。感谢您的任何提示
编辑:这里是我正在使用的模式
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`)参考`parent_account`(`id`),
PRIMARY KEY(`id`)
);
问题解决了。我在引用ParentAccount的LinkedAccount中使用了自定义的 @toString
方法。我不知道这可能会导致任何问题,因此我没有在我的问题中包含toString。
I have a two objects with simple @OneToMany relationship which looks as follows:
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 operate with these entities. However, when calling ParentAccount parent = parentAccountRepository.findOne(id);
, some kind of infinite loop starts happening and hibernate spams this all over 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 changed the fetch 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
(It seems that it is trying to do the lazy load outside of the transactional context).
This is my CRUD repository:
@Repository
public interface ParentAccountRepository extends CrudRepository<ParentAccount, Long> {
}
Could someone tell me how to resolve this issue? I would prefer the solution with EAGER fetch. Thank you for any tips
EDIT: here is the schema I am 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`)
);
Problem solved. I was using a custom @toString
method in the LinkedAccount which was referencing the ParentAccount. I had no idea that this could cause any problem and therefor I did not include the toString in my question.
Apparently, this was causing an infinite loop of lazy loading and removing this reference fixed the problem.
这篇关于Spring启动JPA - OneToMany关系导致无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!