Friends, all good?
I have a problem that is famous: org.hibernate.LazyInitializationException: .
I know this is because the session has been closed and hibernate can not connect to make the selects and so on, but then how do I get the data after the session is closed?
For example: I have these classes:
Status :
public class State {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@OneToOne
@JoinColumn(name = "id_country")
private Country country;
private String description;
private String uf;
@OneToOne
@JoinColumn(name = "id_timezone")
private TimeZone timeZone;
@OneToMany(mappedBy = "state", targetEntity = City.class, fetch = FetchType.LAZY)
@OrderBy("description asc")
private Set<City> city;
/get e set
}
City :
public class City implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@OneToOne
@JoinColumn(name = "id_state")
private State state;
private String description;
//get e set
}
Person :
public class Pessoa {
private Long id;
private String name;
private City city;
/get e set
}
If I leave in EAGER mode the list of cities in the state entity and every time I pick up a new person it will end up making several selects picking up all the cities of that state. However, if I leave in LAZY mode and need access to my city list I get the ** org.hibernate.LazyInitializationException ** exception because the connection is closed.
How to work around this problem? Does anyone give a hand?