Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Project$$EnhancerByCGLIB$$67a694bd appears in Hibernate

I have a document entity mapped many to one to project entity.

When I call document.getProject, in debugger, in project field of document object I see something about Project$$EnhancerByCGLIB$$67a694bd.

How do I retrieve actual project object?

like image 346
bunnyjesse112 Avatar asked Nov 09 '11 09:11

bunnyjesse112


2 Answers

What You are seeing is the Hibernate-Proxy-Object, which enables hibernate to do lazy-instantiation.

First thing to ask yourself is, whether you really want to access the original object. Usually you should be better off pretending the proxy is your actual object and let hibernate do all the magic.

If for some reason you really need the object itself (e.g. if you need the exact type), the following code should work:

if (object instanceof HibernateProxy) {
   return ((HibernateProxy) object).getHibernateLazyInitializer().getImplementation();
}

You should be aware that the result of abovewritten code will give you a detached object which is no longer under hibernate control, so changes to the object will not be synchronized with the database!

like image 174
Jonathan Avatar answered Nov 04 '22 23:11

Jonathan


I was getting an error message with that string in it because I forgot to add the parentheses to a method call. Make sure you don't have this:

document.getProject

When you really mean this:

document.getProject()
like image 1
Charles Wood Avatar answered Nov 04 '22 21:11

Charles Wood