Hibernate“无表” 枚举映射?
我正在处理以下场景:
我们使用 每个子类一个表的继承,这意味着具体表的主键是抽象表上的外键引用。 超类是 Product,子类是 Book、DVD、AudioCD...
现在在 Java 超类中,比如说Product.java
,我们有一个产品类型的enum
:书籍、DVD、音乐等。
我们在抽象表中没有鉴别器列,也没有额外的表类型。
是否可以根据具体对象将 Product.java
中的 type enum
映射到正确的值? 或者是否需要鉴别器或额外的表?
... ....
每个子类继承映射表摘录:
<class name="Product" table="PRODUCT">
<id name="id" column="IDPRODUCT" type="int">
<generator class="native" />
</id>
...
<joined-subclass name="Book" table="BOOK">
<key column="IDPRODUCT" />
<property ...
</joined-subclass>
...
... ...
Product.java
public class Product {
public enum Type { book, dvd, music }
...
private Type type;
...
听起来很奇怪? 或许。 两个独立的团体设计了 OO 部分和 DB 部分......
I am dealing with the following scenario:
We use table-per-subclass inheritance, meaning the concrete tables' primary keys are foreign key references on the abstract table. Superclass is Product, subclasses are Book, DVD, AudioCD, ...
Now in the Java superclass, say Product.java
, we have an enum
of the types of products: book, dvd, music, etc.
We have no discriminator column in the abstract table and no extra table for the types.
Is it possible to map the type enum
in Product.java
to the correct values, depending on the concrete object? Or is a discriminator or an extra table necessary?
... ....
Table per subclass inheritance mapping excerpt:
<class name="Product" table="PRODUCT">
<id name="id" column="IDPRODUCT" type="int">
<generator class="native" />
</id>
...
<joined-subclass name="Book" table="BOOK">
<key column="IDPRODUCT" />
<property ...
</joined-subclass>
...
... ...
Product.java
public class Product {
public enum Type { book, dvd, music }
...
private Type type;
...
Sounds odd? Maybe. Two separate parties designed the OO part and the DB part ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可能遗漏了一些东西,但为什么你要把
Type
作为字段呢? 在Product
中使getType()
抽象,并在子类中实现它以返回适当的值。I may be missing something, but why would you even have
Type
as a field? MakegetType()
abstract inProduct
and implement it in subclasses to return appropriate value.