Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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

 
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 ?
 
author and cow tipper
Posts: 5000
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
Posts: 5000
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
 
Whatever. Here's a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic