-->
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.  [ 3 posts ] 
Author Message
 Post subject: Join inheritance type issue
PostPosted: Tue Oct 03, 2006 1:12 pm 
Newbie

Joined: Fri Jul 28, 2006 2:31 pm
Posts: 4
Hi,

Should the find method of entity manager return null if a subclass is not found (although the instance exists in the base class)?

Let me explain better. I have the following entities:
Code:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class BaseClass implements Serializable {
   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   private int id;
   
   @Basic
   private String text;
   
   public BaseClass() {   
   }
   
   public BaseClass(String text) {
      this.text = text;      
   }
   
   public int getId() {
      return id;
   }
   
   public String getText() {
      return text;
   }   
}

@Entity
public class B extends A {
   @Basic
   private String extraText;

   public B() {      
   }
   
   public B(String baseText, String extraText) {
      super(baseText);
      this.extraText = extraText;
   }

   public String getExtraText() {
      return extraText;
   }
}


Next I create to records.
Code:

A a = new A("some text");
manager.persist(a);
int id1 = a.getId();
B b = new B("some text", "another extra text");
int id2 = b.getId();

// Try to locate a B instance with id1
// Should return null but instead it returns ClassCastException
b = manager.find(B.class, id1);


My question is: shouldn't the last line returns null instead of ClassCastException. The specification states that the find method returns null if no instance of type B exists in the persistence store. But in the above code it returns an A instance instead of null (and the consequent ClassCastException)...

I checked the SQL generated by Hibernate and it seems that for the Joined inheritance type it uses a Left Outer Join. Shouldn't be a Inner Join instead?

Best regards,

Vítor Carreira

PS. Using hibernate 3.2.0.cr4 / hibernate EM 3.2.0CR2 / postgresql 8


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 09, 2006 4:50 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I like the current behavior. It avoids hidden application bugs

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 11, 2006 2:12 am 
Newbie

Joined: Fri Jul 28, 2006 2:31 pm
Posts: 4
I'm sorry but I don't share the same opinion. If I want to locate a specific instance of type B using the find method of the EntityManager, using the current Hibernate implementation I must surround the code in a try-catch block looking for a ClassCastException (instead of testing for a clean null return value).

I can't see the kinds of bugs avoided using the current approach...

But thanks for replying. At least I known it was an option maded by the hibernate team and not a "bug".

Best regards,

Vítor Carreira


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