Hi I am new to hibernate and am facing problem in mapping marker interface. I have a marker interface.
public interface Item{}
Then there are two classes which implement this interface:
public class Hotel im开发者_开发百科plements Item{
private int id;
private String name;
private String location;
.......
}
public class Restaurant implements Item{
private int id;
private String name;
private String cuisine;
.......
}
There is another class which uses these two classes:
public class ItineraryItem {
private int id;
private Item item;
}
How can I map these classes using annotations.
Code:
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class Item {
@Id
private int id;
}
@Entity
public class Hotel extends Item {
@Column
private String name;
@Column
private String location;
}
@Entity
public class Restaurant extends Item {
@Column
private String name;
@Column
private String cuisine;
}
@Entity
public class ItineraryItem {
@Id
private int id;
@JoinColumn
private Item item;
}
InheritanceType.TABLE_PER_CLASS
will cause Hotel
and Restaurant
to have their own separate tables.
You can find more information here: http://en.wikibooks.org/wiki/Java_Persistence/Inheritance
精彩评论