• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic