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.  [ 6 posts ] 
Author Message
 Post subject: Can't make polymorphism to work
PostPosted: Thu Nov 18, 2010 9:33 am 
Newbie

Joined: Thu Nov 18, 2010 9:30 am
Posts: 4
Hi everyone,

I'm using Hibernate 3.3.1, MySql and JPA annotations.
I've a problem when trying to implement a model that uses polymorphism.

Here's the data model I'm trying to implement :

Code:
@Entity
@Table(name = "baseFruit")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.INTEGER)
class BaseFruit {
   
   @Column(name = "type", nullable = false)
   @Enumerated(EnumType.ORDINAL)
   int type;      // Could be 0 (Orange), 1 (Apple)....etc.
   ....
}

@Entity
@Table(name = "orange")
@DiscriminatorColumn(name = "type")
@DiscriminatorValue("0")
@PrimaryKeyJoinColumn(name = "id_fruit", referencedColumnName = "id")
class Orange extends BaseFruit {

   ...
}

@Entity
@Table(name = "apple")
@DiscriminatorColumn(name = "type")
@DiscriminatorValue("1")
@PrimaryKeyJoinColumn(name = "id_fruit", referencedColumnName = "id")
class Apple extends BaseFruit {

   ...
}

@Entity
@Table(name = "owner")
class Owner {

   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "id_fruit")
   BaseFruit preferredFruit;
}


Here are the associated tables :

Code:
baseFruit
   - long id
   - int type
   - ....

apple
   - long id
   - long id_fruit
   - ....

orange
   - long id
   - long id_fruit
   - ....

owner
   - long id
   - long id_fruit
   - ...


The problem is, the mapping always set a BaseFruit instance as the preferredFruit. Is it possible for hibernate to map specialized fruits as the preferredFruit (i.e I would like to have for example an Apple instance as preferredFruit) ?

I cannot make it work.

Thanks in advance for your help !


Top
 Profile  
 
 Post subject: Re: Can't make polymorphism to work
PostPosted: Fri Nov 19, 2010 9:07 am 
Newbie

Joined: Thu Nov 18, 2010 9:30 am
Posts: 4
Any idea ?

Thank you.


Top
 Profile  
 
 Post subject: Re: Can't make polymorphism to work
PostPosted: Tue Nov 23, 2010 6:32 am 
Newbie

Joined: Thu Nov 18, 2010 9:30 am
Posts: 4
Please, any idea ?

Thanks.


Top
 Profile  
 
 Post subject: Re: Can't make polymorphism to work
PostPosted: Tue Nov 23, 2010 11:45 am 
Beginner
Beginner

Joined: Sat Sep 24, 2005 11:04 pm
Posts: 21
BaseFruit should be abstract. Not sure about using the discriminator with a table per subclass approach. Definitely looks odd specifying a discriminator column of "type" when the column doesn't even exist in the subclass tables. You should be able to drop it entirely, Hibernate can determine the type based on the existence of the values in the subclass table.

As for the preferred fruit, the declared type is correct... if the join matches on apple, then the object should be of type Apple, etc.


Top
 Profile  
 
 Post subject: Re: Can't make polymorphism to work
PostPosted: Tue Nov 23, 2010 12:37 pm 
Newbie

Joined: Thu Nov 18, 2010 9:30 am
Posts: 4
Setting BaseFruit as abstract does the trick !!

Concerning the table per subclass approach, I'm not sure to understand what you mean. If I drop the "type" column, how Hibernate will manage to identify what's the type of each fruit ? Where would be stored the discriminator values ?

Thanks a lot !!


Top
 Profile  
 
 Post subject: Re: Can't make polymorphism to work
PostPosted: Tue Nov 23, 2010 1:28 pm 
Beginner
Beginner

Joined: Sat Sep 24, 2005 11:04 pm
Posts: 21
Hibernate is able to identify the proper type merely by the existence of the values in the joined tables. The discriminator wouldn't be stored anywhere in the database. The SQL would look something like this:

Code:
select blah, blah from baseFruit
left outer join apple on (apple.id_fruit = baseFruit.id)
left outer join orange on (orange.id_fruit = baseFruit.id)

I'm guessing Hibernate examines the SQL result set and interprets a non-null apple.id_fruit value as an instance of Apple, a non-null orange.id_fruit value as an instance of Orange, etc.

If you haven't already, I'd recommend enabling the show_sql config param if you want to get a feel for what's going on behind the scenes:

http://docs.jboss.org/hibernate/core/3. ... n-optional


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