Project$$EnhancerByCGLIB$$67a694bd appears in Hibernate
Asked Answered
C

2

8

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?

Craft answered 9/11, 2011 at 9:15 Comment(0)
K
12

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!

Kitty answered 9/11, 2011 at 9:22 Comment(6)
thank you for answer. Im actually not sure if i want original object. I want to retrieve values from its fields. How do i pretend? Could you give me an advice? thanks in advance.Craft
The proxy will load the actual data from database once the field is accessed via the accessor methods. So basically there are two ways. Calling o.getField() in the code (then you can see the actual value in the debugger window) or adding o.getField() to the watchedExpressions window. Both ways should give you the actual data.Kitty
List docs = criteria.list(); MyDocument myDocument =(MyDocument) docs.get(0); String toReturn = null; toReturn=myDocument.getProject().getMiddleName(); to return is null. where am i wrong?Craft
Looks perfectly allright. Are you absolutely certain, that the middlename is not null? Have you checked in the database directly? Oh and: Do you execute your code within one transaction?Kitty
Maybe i need to add something to mapping files to make o.getField() work?Craft
I got it finally. Thank you for help and explanations. You are just awesome.Craft
B
1

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()
Bogey answered 21/2, 2013 at 22:13 Comment(3)
@LimitedAtonement, an error message containing something like Project$$EnhancerByCGLIB$$67a694bd, as mentioned in the question. Unfortunately I don't remember the details of the message.Bogey
The question doesn't talk about an error, or error message. It looks like the original poster is using a debugger to investigate some values.Ralston
Yeah, looks like you're right. I was new to Grails and to Stack Overflow when I posted this answer over a year ago. I'll go ahead and edit the answer to be more clear.Bogey

© 2022 - 2024 — McMap. All rights reserved.