-->
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: Inheritance is ignored in case of associations
PostPosted: Mon Dec 13, 2010 10:36 am 
Newbie

Joined: Mon Dec 13, 2010 10:10 am
Posts: 5
It seems that the associations like one-to-one or one-to-many ignore the inheritance and always fetch the base class. Let's suppose that there are 3 model classes, A, B and C. B extends A, and C has a one-to-one relations with A:
Code:
@Entity
@DiscriminatorColumn(name="dtype")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@Table(name="test_entities_a")
public class EntityA {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String someValue;

    // Getters and setters are omitted
}

Code:
@Entity
public class EntityB extends EntityA{
}

Code:
@Entity
@Table(name="test_entities_c")
public class EntityC {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @OneToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
    EntityA entity;
    // Getters and setters are omitted

And here's the test case:
Code:
// Code to start the session is omitted
        EntityB entityB=new EntityB();
        entityB.setSomeValue("test");
        EntityC entityC=new EntityC();
        entityC.setEntity(entityB);
        session.save(entityC);
        session.flush();
        long id=entityC.getId();
        session.close();
// Code to reopen the session is omitted
        entityC=(EntityC) session.get(EntityC.class, id);
        System.out.println(entityC.getEntity() instanceof EntityB);

Now, the data is saved correctly, the discriminator column in the DB has the "EntityB" value. However the last line prints "false" because the entityC.getEntity() call actually returns an instance of the EntityA class wrapped in a proxy. I have found a couple of similar bug reports, here and here, is it the same issue? Is there a known workaround? I tried to evict the associated entity and re-read it by ID in the real code, but I got a non unique object exception.
The issue was discovered on Hibernate-3.6.0-Final, PostgreSQL 9.0, JDK 1.6.


Top
 Profile  
 
 Post subject: Re: Inheritance is ignored in case of associations
PostPosted: Tue Dec 14, 2010 12:05 pm 
Newbie

Joined: Mon Dec 13, 2010 10:10 am
Posts: 5
Any thoughts? Should I open a new ticket in JIRA?
Sorry for bumping it up, but we're blocked by this issue.


Top
 Profile  
 
 Post subject: Re: Inheritance is ignored in case of associations
PostPosted: Wed Dec 15, 2010 2:21 pm 
Newbie

Joined: Tue Aug 03, 2010 2:06 pm
Posts: 5
Hi,

By default many-to-one will have the lazy attribute as "proxy". This will cause issues when u have polymorphic associations. The object fetched will always be a subtype (proxy) of the mapped class and u can never cast it to any of the discriminator types.

Eager fetching will solve it. Or change lazy to "no-proxy" with byte code enhancement.

Here is the documentation.

http://docs.jboss.org/hibernate/core/3. ... ng-proxies

Hope this helps

Regards,
Jijo


Top
 Profile  
 
 Post subject: Re: Inheritance is ignored in case of associations
PostPosted: Thu Dec 16, 2010 6:34 am 
Newbie

Joined: Mon Dec 13, 2010 10:10 am
Posts: 5
The eager fetching does the trick, thanks.


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.