0

I've got this mappings in Hibernate (through JPA, using EntityManager and such):

 public ChildClass {

     ....
      @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "parentClassId")
        private ParentClass parentClass;
     ....
    }

And a parent class that has a list of ChildClass elements:

 public ParentClass {
    ...
    @OneToMany(mappedBy = "parentClass", fetch = FetchType.LAZY)
        private List<ChildClass> childElements;
    ...

    }   

Now I have this method in my Service layer:

@Transactional
public ChildClassDTO consultarPuntuacionPrueba(Long parentClassId) {
            ChildClass result= childClassDAO.getChildClassByParentId(parentClassId);



            ParentClass parentClass = result.getParentClass();

            System.out.println("UNITIALIZED: " + ((HibernateProxy)prueba).getHibernateLazyInitializer().isUninitialized());


            Long parentClassId = parentClass.getParentClassId();
            Boolean anAttribute = parentClass.getBooleanAttribute();

            return map(result, ChildClassDTO.class);
        }

The problem is -- parentClass is a javassist proxy, and even though when accessing a getter of parentClass, I can see a query getting executed (it shows in my console), the proxy never gets initialized and holds null for all attributes... I have tried said query directly towards my database, and it returns the expected data.

I know Hibernate must return a proxy when I call childClass.getParentClass(), but why is it never initialized afterwards?

5
  • Is "parentClassId" the fk column name in the database? Commented Feb 11, 2015 at 16:42
  • Yes, mapping works okay in other instances... Weird thing is, this is happening with most of my relations Commented Feb 11, 2015 at 16:43
  • Do you use the final keyword somewere in your entities? Commented Feb 11, 2015 at 16:56
  • As a matter of fact -- I do! And now I think I can see what's happening -- Proxies cannot inject anything because they cannot override the setter methods right? Commented Feb 11, 2015 at 17:04
  • Yes, that's what I had in mind. Added it as an answer. Commented Feb 11, 2015 at 17:14

2 Answers 2

2

Make sure your don't use the final keyword for your entities getters and setters otherwise the proxies will not be able to override the methods and may not work properly.

Sign up to request clarification or add additional context in comments.

Comments

0

As you defined in the mapping, Hibernate loads the mapped entity lazily. That means the mapped entity is only loaded when you access it (otherwise you only have a proxy instance). So you could change the FetchType to EAGER if you would like that the mapped entity is eagerly loaded (without a proxy)

2 Comments

That's not my question -- as I stated in my OP, I know it's lazily loaded and Im okay with that, the problem is after accessing the proxies' properties, I still get null values cause the proxy won't get initialized even after the proper query has been made
That is indeed very strange

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.