Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate generated query for a inheritance mapping

 
Kumar Raja
Ranch Hand
Posts: 547
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a inheritance mapping between Product and Electronics objects. I have this defined as
single table for subclass model
and using one mapping file for both product and electronics using joined-subclass tag.



After that, I have successfully inserted few records into both the tables, by creating Electronics objects and persisting them.

Now I see two different queries generated by Hibernate, when I try to query Products and Electronics seperately.

If I say

generated query is


from this, I can understand that InnerJoin is used to retrieve Product Name, Product Desc and Product Price, as Electronics extends Product

But, If I say

generated query is


From the above query, I did not understand why do we need to get Category, as Category is an attribute of Electronics and Product does not have any information about its subclass. Also I did not understand the usage of Case here and why do we need to do a left outer join of electronics.

Can't simply Hibernate query against Product alone, instead of complicating with the usage of joins and unnecessary tables. Please advice, if I need to change anything here.
 
Kumar Raja
Ranch Hand
Posts: 547
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any thoughts on this
 
Justin Chi
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If Hibernate does not left join Electronics, what will happen if you do a session.delete(product) ?

This is how Hibernate works, please note that lazy loading does not work here.

As Electronics object is selected, all it's properties will be in select clause including the ones inherited from its parent and its own attributes like category.

Use plain SQL instead of HQL if you want a clean Product.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic