• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to avoid this hibernate jpa LazyInitializationException exception in Swing application?

 
Kevin Pang
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Caused by: org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:132)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:174)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
at com.yatsun.entity.Country_$$_javassist_4.getCustomers(Country_$$_javassist_4.java)
... 34 more


the entity:
@Entity
@Table(name = "COUNTRY")
public class Country extends ValueObjectImpl implements java.io.Serializable {

// Fields

private Long countryId;
private String countryName;
private String code;
private List<Customer> customers=new ArrayList<Customer> ();

@OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.REFRESH, mappedBy="country")
@JoinTable(name="CUSTOMER",
joinColumns={@JoinColumn(name="COUNTRY")},
inverseJoinColumns={@JoinColumn(name="COUNTRY_ID")})
@OrderBy(value = "customerId ASC")
public List<Customer> getCustomers() {
return customers;
}

public void setCustomers(List<Customer> customers) {
this.customers = customers;
}

if there is any method to solve this without changing the FetchType to EAGER ?
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is any method to solve this without changing the FetchType to EAGER ?


Well, that's sorta what you want, isn't it?

You can call the getter method earlier, within an open session.

You could also extend the Session. Look at the Hibernate "Open Session in View" pattern. That solves many of these types of problems.

-Cameron McKenzie
 
Kevin Pang
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cameron Wallace McKenzie wrote:
there is any method to solve this without changing the FetchType to EAGER ?


Well, that's sorta what you want, isn't it?

You can call the getter method earlier, within an open session.

You could also extend the Session. Look at the Hibernate "Open Session in View" pattern. That solves many of these types of problems.

-Cameron McKenzie


thanks for reply.

it seems that Open Session in View only works in web application ,isn't it?
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
only works in web application


Well, it's really the same idea, regardless of the client type. A request comes in, a Session is opened, the request is processed, the view is updated, the Session is then closed. It is very effective, and very efficient.

-Cameron McKenzie
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic