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 !