Hello,
First I want to say that I am new to Hibernate and Java development in general, so please go easy on me ;-)
I am creating DAOs that all extend from a base
BasicBean class. This class has a
store() method that looks like
Code:
public void store()
{
mapper.store(this);
}
mapper is a
DataMapper object that defines store as
Code:
public void store(AbstractBasicBean bean) {
hibernateUtil.saveObject(bean);
}
hibernateUtil is an instance of a custom class. It stores a hibernate session object that allows me to share one session among all my mapper classes. The
saveobject() method in
hibernateUtil looks like
Code:
public void saveObject(Object obj) {
Transaction trans = session.beginTransaction();
session.saveOrUpdate(obj);
trans.commit();
}
The
session object is a member of the hibernateUtil class and is set up earlier in the code (I am sure this is working, I can use it to select objects out).
The problem I'm encountering is this: I have some code that looks like...
Code:
HibernateUtil hibernateUtil = new HibernateUtil(); //creates hibernate session
MapperFactory mapperFactory = new MapperFactory(hibernateUtil);
UserMapper userMapper = mapperFactory.getUserMapper();
User user= userMapper.findByUserName("aroyer");
user.setLastName("royer2");
if (user.mapper == null)
System.out.println("MAPPER IS NOT NULL);
else
System.out.println("MAPPER IS NULL");
user.store();
hibernateUtil.closeSession(); //closes hibernate session
Just for reference, The mapperFactory returns mapper objects that are given a reference to the hibernateUtil object passed to it. This way all the mapper classes automatically have a reference to the single hibernate session.
When I run this code, the user is retrieved fine, and
MAPPER IS NOT NULL is printed to the console as expected. However, when user.store() is called, I get a
null reference exception indicating that user.mapper is in fact null. I've stepped through the code in Eclipse debugger and it seems that the store() method I wrote is not actually called, but instead a Hibernate Proxy store() method. I'm guessing this is the root of my problem, but I am not sure how to fix it. Is there a way for me to tell Hibernate to use my store() method and override the proxying? Or am I an idiot and should be going about this whole thing a different way? :-)
Here is the exception
Code:
Exception in thread "main" java.lang.NullPointerException
at com.mediaedge.util.businessobjects.AbstractBasicBean.store(AbstractBasicBean.java:29)
at com.mediaedge.util.businessobjects.AbstractBasicBean$$FastClassByCGLIB$$e9e03990.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:137)
at com.mediaedge.util.businessobjects.User$$EnhancerByCGLIB$$faaa6519.store(<generated>)
I can post more code if you need further clarification. Thank you for any help you can provide.