-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Proxy loaded on getId-call when using annotations on fields
PostPosted: Sun Dec 16, 2007 8:46 am 
Newbie

Joined: Tue Aug 08, 2006 5:00 am
Posts: 6
Hi there.

I think I've found a problem with Hibernate Annotations regarding proxied entities. If the proxied entity has its @Id-annotation on the id-field (instead of the getId-method), the proxied entity are fetched when getId() is called.

Hibernate version:
3.2.5.ga, Annotations: 3.3.0.ga

Test case
Parent.java:
Code:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Parent{
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(final Long id) {
        this.id = id;
    }
}


Child.java:
Code:
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

@Entity
public class Child {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    private Parent parent;

    public Long getId() {
        return id;
    }

    public void setId(final Long id) {
        this.id = id;
    }

    public Parent getParent() {
        return parent;
    }

    public void setParent(final Parent parent) {
        this.parent = parent;
    }
}


LazyLoadingWithFieldLevelAnnotationsTest:
Code:
import junit.framework.TestCase;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hsqldb.jdbcDriver;

public class LazyLoadingWithFieldLevelAnnotationsTest extends TestCase {
    SessionFactory sessionFactory;

    public Session getSession() throws HibernateException {
        return sessionFactory.openSession();
    }

    @Override
    protected void setUp() throws Exception {
        // load the hsqldb jdbc driver
        Class.forName(jdbcDriver.class.getName());

        // setup session-factory
        sessionFactory = new AnnotationConfiguration().addAnnotatedClass(Child.class).addAnnotatedClass(Parent.class).setProperty("hibernate.dialect",
                "org.hibernate.dialect.HSQLDialect").setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:temptest").setProperty("hibernate.hbm2ddl.auto", "create-drop")
                .buildSessionFactory();
    }

    public void testLazyLoadingWithFieldLevelAnnotations() throws Exception {
        Session session = getSession();
        session.beginTransaction();
        // save a parent and a child, connected to that parent

        final Parent p = new Parent();
        session.save(p);

        Child c = new Child();
        c.setParent(p);
        session.save(c);

        assertNotNull(c.getId());

        // close the session and open a new one
        session.getTransaction().commit();
        session.close();
        session = getSession();

        // get the child from the database
        c = (Child) session.get(Child.class, c.getId());

        assertNotNull(c);
        assertNotNull(c.getParent());

        // assert that the parent is currently not initialized
        assertFalse(Hibernate.isInitialized(c.getParent()));
        assertNotNull(c.getParent().getId());

        // assert that the parent still is not initialized
        assertFalse(Hibernate.isInitialized(c.getParent()));
    }
}


Test result:
Code:
junit.framework.AssertionFailedError
   at junit.framework.Assert.fail(Assert.java:47)
   at junit.framework.Assert.assertTrue(Assert.java:20)
   at junit.framework.Assert.assertFalse(Assert.java:34)
   at junit.framework.Assert.assertFalse(Assert.java:41)
   at LazyLoadingWithFieldLevelAnnotationsTest.testLazyLoadingWithFieldLevelAnnotations(LazyLoadingWithFieldLevelAnnotationsTest.java:58)

(...)


If the @Id-annotation on Parent.java are moved down from the field to the getId-method, the test runs without any problems.


Aleksander BlomskĂžld


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 17, 2007 9:56 am 
Newbie

Joined: Tue Aug 08, 2006 5:00 am
Posts: 6
BTW, line 58 (where the assertion fails) is the last assertion in the test:

Code:
(...)
57:        // assert that the parent still is not initialized
58:        assertFalse(Hibernate.isInitialized(c.getParent()));
59:    }
(...)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 22, 2007 4:56 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
That is unfortunate but expected. That's one of the limitations of field level access.
Basically we have no way to know that getId() indeed only go and access the id field. So we need to load the entire object to be safe.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: Proxy loaded on getId-call when using annotations on fields
PostPosted: Thu Mar 31, 2016 5:07 am 
Newbie

Joined: Thu Mar 21, 2013 3:15 am
Posts: 5
Is this limitation still valid for more current versions of Hibernate?

Regards,
Niko


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.