org.hibernate.LazyInitializationException:无法延迟初始化role:ticketCircuitInfo的集合,无法初始化代理-没有会话

时间:2018-10-09 08:44:24

标签: java hibernate

我有2个bean类(ticketEntityticketCircuitInfo)分别映射到1个表

我也有1个ticketEntity(primary key),可以有多个ticketCircuitInfo(forign key)

ticketEntity bean具有以下属性:

@Id
@Column(name = "ticket_id", unique = true, nullable = false)
private String ticketId;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "ticket", cascade      =CascadeType.ALL)
private Set<TicketCircuitInfo> ticketCircuitInfo;

ticketCircuitInfo bean具有以下属性:

@GenericGenerator(name = "generator", strategy = "foreign",
parameters = @Parameter(name = "property", value = "ticket"))
@Id
@Column(name = "ticket_id", unique = true, nullable = false)
private String ticketId;
@ManyToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn
private TicketEntity ticket;`

我得到:

  

com.sun.jdi.InvocationException发生了调用方法。

ticket.ticketCircuitInfo()行上调试时。

要打印的其他异常是:

  

org.hibernate.LazyInitializationException:无法延迟初始化role:ticketCircuitInfo的集合,无法初始化代理-没有会话

2 个答案:

答案 0 :(得分:1)

您应该查看服务层,并将@Transactional添加到服务层方法。

请参考:How to solve the “failed to lazily initialize a collection of role” Hibernate exception

答案 1 :(得分:1)

您可以从

进行更改
@ManyToOne(fetch = FetchType.LAZY)

@ManyToOne(fetch = FetchType.EAGER)