2

I have 2 bean classes(ticketEntity, ticketCircuitInfo) mapped to 1 table each

I also have 1 ticketEntity(primary key) that can have multiple ticketCircuitInfo(forign key)

The ticketEntity bean has the following properties:

@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;

The ticketCircuitInfo bean has the following properties:

@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;`

I'm getting:

com.sun.jdi.InvocationException occurred invoking method.

While debugging on the line ticket.ticketCircuitInfo().

further exception being printed is:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role:ticketCircuitInfo, could not initialize proxy - no Session

Draken
  • 3,134
  • 13
  • 34
  • 54
Myra
  • 25
  • 1
  • 1
  • 6
  • How are you connecting to your DB to retrieve your entities? What's happening is that you are taking out your entity from the DB and then your session is closed. When you try and query the Lazy property, it fails as the session is unavailable. Knowing how you connect to the DB is key to providing you suitable solution. – Draken Oct 09 '18 at 16:16

2 Answers2

2

You should look into your service layer and add @Transactional to the service layer method..

Please refer to : How to solve the “failed to lazily initialize a collection of role” Hibernate exception

mkane
  • 880
  • 9
  • 16
  • 1
    You've assumed they are using Spring, but they haven't tagged that. The error message just warns the Session is closed that could be caused by many things. I would wait until the OP clarifies that they are using Spring before suggesting using @Transactional. On another note, if the other question also already answers the OP's question, then it's better to flag as a duplicate rather than posting an answer with a link to the other questions. – Draken Oct 09 '18 at 16:03
-3

You can change from

@ManyToOne(fetch = FetchType.LAZY)

to

@ManyToOne(fetch = FetchType.EAGER)
Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54