aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate InheritanceType.SINGLE_TABLE with different eager OneToOne relations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate InheritanceType.SINGLE_TABLE with different eager OneToOne relations" Watch "Hibernate InheritanceType.SINGLE_TABLE with different eager OneToOne relations" New topic
Author

Hibernate InheritanceType.SINGLE_TABLE with different eager OneToOne relations

Marco Masi
Greenhorn

Joined: Apr 18, 2009
Posts: 7
Hi to all!

I was trying to implements relations in a entity hierarchy build using single table strategy but i had some strange problems that i can't resolve

I'm using:
hibernate3-3.4.0.GA
hibernate-annotations-3.4.0.GA
hibernate-entitymanager-3.4.0.GA
mysql-connector-java-5.1.6

Running a MySQL server 5.1.11.


This is the structure:

abstract MyBean with @Inheritance ( strategy = InheritanceType.SINGLE_TABLE )
MyBeanWithA extends MyBean and add a OneToOne eager relation with ABean (inverse side)
MyBeanWithB extends MyBean and add a OneToOne eager relation with BBean (inverse side)
ABean with a OneToOne eager relation with MyBeanWithA (right side)
BBean with a OneToOne eager relation with MyBeanWithB (right side)

Foreign keys on db need to be on ABean and BBean.
I need always to access MyBeanWithA when I access ABean.
I need always to access ABean when i access MyBeanWithA.
The same is for BBean and MyBeanWithB.

What I'm not capable to do is have an entity correctly loaded with his relation using polimorphism.

Here's the code (it isn't so beautiful but it's only demonstrative)

MyBean:


MyBeanWithA:

MyBeanWithB:

ABean:

BBean:

persistence.xml:

Main:

As you can see versions A and B are practically the same (much copy/paste )

Running this all (without log just to see) I get this (complete log in attach, try_1.log ):

loadedMyBean_A.getClass().getSimpleName(): MyBeanWithA
((MyBeanWithA) loadedMyBean_A).getBean(): ABean

loadedMyBean_BMyBean.getClass().getSimpleName(): MyBeanWithB
((MyBeanWithB) loadedMyBean_B).getBean(): null

loadedMyBeanWithA.getClass().getSimpleName(): MyBeanWithA
loadedMyBeanWithA.getBean(): ABean

loadedMyBeanWithB.getClass().getSimpleName(): MyBeanWithB
loadedMyBeanWithB.getBean(): BBean


I can't uderstand why using polymophism for MyBeanWithB I get a null reference, just for control loading MyBeanWithB without polymophism i get BBean correctly loaded

Looking for SQL it seems that BBean isn't considerated:

/* load my.entity.MyBean */ select
mybean0_.myBean_id as myBean2_2_2_,
mybean0_.bean_name as bean1_2_2_,
abean1_.id as id0_0_,
abean1_.myBean_id as myBean2_0_0_,
mybeanwith2_.myBean_id as myBean2_2_1_
from
MyBean mybean0_
left outer join
ABean abean1_
on mybean0_.myBean_id=abean1_.myBean_id
left outer join
MyBean mybeanwith2_
on abean1_.myBean_id=mybeanwith2_.myBean_id
where
mybean0_.myBean_id=?


Instead setting relations on MyBeanWithX (i've changed optional from false to true)

The result is even worse in SQL BBean is now consierated but beans reference are null both for MyBeanWithA and MyBeanWithB (complete log in attach, try_2.log )

You have any suggestions?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate InheritanceType.SINGLE_TABLE with different eager OneToOne relations