使用 jpa 映射 Hibernate 标记接口

发布于 2024-11-08 18:09:36 字数 617 浏览 0 评论 0原文

您好,我是休眠新手,在映射标记接口方面面临问题。 我有一个标记界面。

    public interface Item{}

然后有两个类实现这个接口:

    public class Hotel implements Item{
         private int id;
         private String name;
         private String location;
         .......
    }
    public class Restaurant implements Item{
         private int id;
         private String name; 
         private String cuisine;
         ....... 
    }

还有另一个类使用这两个类:

    public class ItineraryItem {
          private int id;
          private Item item;
    }

如何使用注释映射这些类。

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 implements 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一百个冬季 2024-11-15 18:09:36

代码:

@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 将导致 HotelRestaurant 拥有自己单独的表。

您可以在这里找到更多信息:http://en.wikibooks.org/wiki/Java_Persistence/Inheritance< /a>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文