将多个类映射到Hibernate中的一个表,没有DTYPE列 [英] Mapping Multiple Classes to a Table in Hibernate, Without a DTYPE Column

查看:605
本文介绍了将多个类映射到Hibernate中的一个表,没有DTYPE列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个hibernate类:一个基类和一个扩展类,它具有附加字段。 (这些字段由其他表格映射)。例如,我有:

  $ b $ p  @Entity 
@Table(name =Book)
public class A {
private String ID;
私人字符串名称;
// ...
}

@Entity
@Table(name =Book)
public class B extends A {
public String node_ID;
// ...
}

公共类节点{
public String ID; //映射到B.node_ID
// ...
}

我如何在Hibernate中映射它? hibernate文档陈述了三种类型的继承配置:每个类一个表,一个表类型列和一个连接表 - 这些都不适用于此。



我需要这样做的原因是因为class A来自通用框架,它被重用于多个项目,而B类(和节点)是特定于一个项目的扩展 - 它们不会再被使用。在未来,我可能会有一个C类,带有house_ID或其他字段。



编辑:如果我尝试上面的伪 - 代码配置(映射到同一个表的两个实体)我得到一个DTYPE列不存在的错误。 HQL有一个where DTYPE =A追加。

解决方案

通过映射 @DiscriminatorColumn @DiscriminatorValue 为两个类的相同值;这可以来自您使用的具有相同数据的任何列,而不管哪种类型(不知道它是否适用于空值)。

类应如下所示:

  @Entity 
@Table(name =Book)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name =published)
@DiscriminatorValue(value =true)
public class A {
private String ID;
private String Name;
// ...
}

@Entity
@Table(name =Book)
@ DiscriminatorValue(value =true)
public class B extends A {
public String node_ID;
// ...
}


I have two hibernate classes: a base class, and an extended class that has additional fields. (These fields are mapped by other tables.)

For example, I have:

@Entity
@Table(name="Book")
public class A {
    private String ID;
    private String Name;
    // ...
}

@Entity
@Table(name="Book")
public class B extends A {
    public String node_ID;
    // ...
}

public class Node {
    public String ID; // maps to B.node_ID
    // ...
}

How do I map this in Hibernate? The hibernate documentation states three types of inheritence configurations: one table per class, one table with a type column, and a join table -- none of which apply here.

The reason I need to do this is because class A is from generic framework that's reused over multiple projects, and class B (and Node) are extensions specific to one project -- they won't be used again. In the future, I may have perhaps a class C with a house_ID or some other field.

Edit: If I try the above pseudo-code configuration (two entities mapped to the same table) I get an error that the DTYPE column doesn't exist. The HQL has a "where DTYPE="A" appended.

解决方案

This is possible by mapping the @DiscriminatorColumn and @DiscriminatorValue to the same values for both classes; this can be from any column you use that has the same data regardless of which type (not sure if it works with null values).

The classes should look like so:

@Entity
@Table(name="Book")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="published")
@DiscriminatorValue(value="true")
public class A {
    private String ID;
    private String Name;
    // ...
}

@Entity
@Table(name="Book")
@DiscriminatorValue(value="true")
public class B extends A {
    public String node_ID;
    // ...
}

这篇关于将多个类映射到Hibernate中的一个表,没有DTYPE列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆