• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do you explain this Lazy Loading Exception - There is no association at all - LAZY OR CRAZY?

 
Mustafa Garhi
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class called HibernateCRUD that has the following two methods (in addition to more code):

public void save(Object transientObject) {
try {
openSession();
beginTransaction();
s.save(transientObject);
commitTransaction();
} catch (Exception e) {
e.printStackTrace();
rollBackTransaction();
}
closeSession(); // This is part of the problem according to me
}

public Object load(Class class_, int id) {
openSession();
Object returnObj = s.load(class_, id);
closeSession();
return returnObj;
}

From my code i call save first and then when load the object and try to use it i get a Lazy Initialization Exception

I kind of knew the rules for Lazy but this is what i did not expect.

Note that the Entity i am using is a very simple one having no other entity reference into it.

However if i comment the closeSession() of save and openSession() of load its all perfect.

How do we explain this?


Here is my complete HibernateCRUD :

package common;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;


public class HibernateCRUD {

private static HibernateCRUD instance;
private static String mutex = "lockk";
private Session s;
private Transaction t;
private SessionFactory f;
private HibernateCRUD() {}

public static HibernateCRUD getInstance(String configFile) {
synchronized (mutex) {
if(instance == null) {
instance = new HibernateCRUD();
}
}
instance.setUp(configFile);
return instance;

}

private void setUp(String configFile) {
Configuration c = new Configuration();
Configuration cfg = c.configure(configFile);
f = cfg.buildSessionFactory();
}

public void save(Object transientObject) {
try {
openSession();
beginTransaction();
s.save(transientObject);
commitTransaction();
} catch (Exception e) {
e.printStackTrace();
rollBackTransaction();
}
closeSession();
}

public Object load(Class class_, int id) {
openSession();
Object returnObj = s.load(class_, id);
closeSession();
return returnObj;
}

private void beginTransaction() {
t = s.beginTransaction();
}

private void commitTransaction() {
t.commit();
}

private void rollBackTransaction() {
t.rollback();
}
private void openSession() {
s = f.openSession();
}
private void closeSession() {
s.close();
}

}


My Entity Class :

public class Mouth {

private int id;
private String description;

// yes i have getter/setters

}


My CRUD Client :

package com.hibe.app.onetoone.unidirectional;

import org.hibernate.Session;
import org.hibernate.Transaction;

import common.HibernateCRUD;

public class CRUD {
static Session s = null;
static Transaction t = null;
public static void main(String a[]) throws Exception {

String configFile = "/com/hibe/app/onetoone/unidirectional/config/hibernate.cfg.xml";
HibernateCRUD hibernateUtil = HibernateCRUD.getInstance(configFile);

Mouth mouth = new Mouth();
mouth.setDescription("BIG mouth !!");

hibernateUtil.save(mouth);

Mouth mouthLoaded = (Mouth)hibernateUtil.load(Mouth.class, 1);
System.out.println(mouthLoaded); // LAZY CRAZY
}
}

Mapping file :


<class name="com.hibe.app.onetoone.unidirectional.Mouth" table="mouth">
<id name="id" column="mouth_id" type="integer">
<generator class="native"/>
</id>
<property name="description" column="description" type="string" length="100"/>
</class>

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic