Saturday, 31 August 2013

Hibernate Inheritance SingleTable Subclass Joins

Hibernate Inheritance SingleTable Subclass Joins

Ive been working with Hibernate since a couple of weeks. Well its a very
helpful tool but i cannot resolve following task:
Table:
Create Table `Product`
(
`product_id` INT(10) PRIMARY KEY,
`package_id` INT(10) NULL,
`product_type` VARCHAR(50) NOT NULL,
`title` VARCHAR(255) NOT NULL,
`desc` VARCHAR(255) NULL,
`price` REAL(10) NOT NULL,
...
);
in Java i have 3 Classes
@Entity
@Table(name = "Product")
@DiscriminatorColumn(name = "product_type")
public abstract class Product {
...
}
there are two types of instances, where an "Item" could but may not always
deserve to a "Bundle". "Bundles" have at least one "Item"
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorValue(value = "Item")
public class Item extends Product {
Bundle bundle;
....
@ManyToOne (fetch=FetchType.LAZY, targetEntity=Bundle.class)
@JoinColumn (name="album_id")
public Bundle getBundle() {...}
public void setBundle(Bundle bundle) {...}
....
}
and:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorValue(value = "Bundle")
public class Bundle extends Product {
Set<Item> items;
....
@OneToMany (mappedBy="album", targetEntity=MusicSong.class)
@OrderBy ("track")
public Set<Item> getItems() {...}
public void setItems(Set<Item> items) {...}
....
}
At Runtime its not possible to call any data, error: Expected type:
org.blah.Bundle, actual value: org.blah.Item
does anyone have an idea or hint. isearching google up&down but i cannot
find this specific issue.

No comments:

Post a Comment