-->
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.  [ 5 posts ] 
Author Message
 Post subject: Hibernate 4.1.7: Bug in SessionCache when using Inheritance?
PostPosted: Wed Oct 31, 2012 5:21 am 
Newbie

Joined: Wed Oct 31, 2012 3:59 am
Posts: 3
Hi all,

I think I might've encountered a Bug in Hibernate 4.1.7. Here's a test case I built:

Code:
public class HibernateBugTest {
   
   @Test
   public void test() {
      EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu");
      EntityManager em = emf.createEntityManager();
      
      SubClass1 sc1 = em.find(SubClass1.class, "1");
      SubClass2 sc2 = em.find(SubClass2.class, "1");
      
      assertTrue(sc1.getKeyField().equals(sc2.getKeyField()));
   }
   
   @Entity
   @Table(name = "BUGTEST")
   @Inheritance
   @DiscriminatorColumn(name = "otherField")
   public static class AbstractSuperClass {
      
      @Id
      private String keyField;
      
      public String getKeyField() {
         return keyField;
      }
      
      public void setKeyField(String keyField) {
         this.keyField = keyField;
      }
      
   }
   
   @Entity
   @DiscriminatorValue("SC1")
   public static class SubClass1 extends AbstractSuperClass {
      
   }
   
   @Entity
   @DiscriminatorValue("SC2")
   public static class SubClass2 extends AbstractSuperClass {
      
   }
   
}


Running this case results in a null pointer exception at assertTrue(), because sc2 is null. I've debugged the case to the point where I found that an error occurs during the second call to em.find().

The method org.hibernate.engine.internal.StatefulPersistenceContext#getEntity(EntityKey key) returns the SubClass1 object fetched by the first call to em.find().

I think this might be due to the case that the EntityKey's hashCode() method does not evaluate it's entityName property, but only the rootEntityName (which is 'AbstractSuperClass' for SubClass1 and SubClass2).

Is this really a bug, or did I miss something?

Edit: The test case would work, if I inserted an em.clear() inbetween the two find()s, of course - but this isn't really the point, is it? ;-)


Top
 Profile  
 
 Post subject: Re: Hibernate 4.1.7: Bug in SessionCache when using Inheritance?
PostPosted: Mon Nov 05, 2012 5:02 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi danB,

please specify which data you prepared on your database before running the testcase.
What is the result of "select * BUGTEST" after the testcase set-up?


Top
 Profile  
 
 Post subject: Re: Hibernate 4.1.7: Bug in SessionCache when using Inheritance?
PostPosted: Mon Nov 05, 2012 5:52 am 
Newbie

Joined: Wed Oct 31, 2012 3:59 am
Posts: 3
Hi pb,

thanks for taking a look at this. When running the test case, my "bugtest" table contained this:

Code:
keyField  |  otherField
-----------------------
1         |  SC1
1         |  SC2


Since I specified 'otherField' as a discriminator column, I'd expect to get two different objects, even though the @Id column 'keyField' contains identical values for both rows. I also tried using the @ForceDiscriminator annotation as well as it's newer "DiscriminatorOptions" cousin, but to no avail.

Regards,
danB


Top
 Profile  
 
 Post subject: Re: Hibernate 4.1.7: Bug in SessionCache when using Inheritance?
PostPosted: Thu Nov 08, 2012 3:14 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi danB,

usually a @Id property get mapped as a column with unique index constraint,
so I'm asking myself how you where even able to insert 2 records with the same id 1 !
It seems to me that your idea is to have the ID composed by keyField AND the discriminator otherField.
If you really need to have 2 distinct instances with the same keyField (but different discriminators),
then I suggest you to use table-per-subclass mapping strategy instead to table-per-class-hierarchy strategy


Top
 Profile  
 
 Post subject: Re: Hibernate 4.1.7: Bug in SessionCache when using Inheritance?
PostPosted: Thu Nov 08, 2012 3:36 am 
Newbie

Joined: Wed Oct 31, 2012 3:59 am
Posts: 3
Hi pb,

pb00067 wrote:
It seems to me that your idea is to have the ID composed by keyField AND the discriminator otherField.


yea, maybe I got a bit confused here. However, since the table I'm trying to read from is part of a different system, we can't simply split it up into multiple tables. Anyway, thanks for the heads up; I'll post my final solution when it's done.

Greets from Hamburg, Germany!
danB


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.