开发者

Hibernate marker interface mapping using jpa

开发者 https://www.devze.com 2023-03-07 01:09 出处:网络
Hi I am new to hibernate and am facing problem in mapping marker interface. I have a marker interface.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消