我有两个具有相同字段的对象,但来自不同的表(foo_something和bar_something实际上不是普通表,有几次连接操作的结果)。我使用休眠注释@Subselect
以获取数据。我可以像这样制作类结构吗,但是哪个工作正常?此类结构不起作用,因为批注不会继承。如果我使用@Inheritance
注释,则会出现错误Unknown column foo_somethingca0.DTYPE
class Something {
@Id @Column Long id;
@Column String name;
@Column String description;
//getters, setters, constructors
}
@Entity
@Immutable
@Subselect("SELECT id, name, description FROM foo_something")
class FooSomething extends Something {
}
@Entity
@Immutable
@Subselect("SELECT id, name, description FROM bar_something")
class BarSomething extends Something {
}
上。
谢谢,@HenryMartens,我添加了注释@Entity
和@Inheritance
TABLE_PER_CLASS
策略,并使类抽象,现在它工作正常:
@Entity
@Inheritance(strategy = TABLE_PER_CLASS)
abstract class Something {
@Id @Column Long id;
@Column String name;
@Column String description;
//getters, setters, constructors
}
@Entity
@Immutable
@Subselect("SELECT id, name, description FROM foo_something")
class FooSomething extends Something {
}
@Entity
@Immutable
@Subselect("SELECT id, name, description FROM bar_something")
class BarSomething extends Something {
}
Hibernate中的DTYPE
是鉴别器列。它用于区分保留在同一数据库表中的某些对象,如您的示例所示。 您可以添加带有@DiscriminatorColumn
注释的鉴别器列。
在具体类上,您可以使用@DiscriminatorValue
为带注释的类设置鉴别器列的值。
例如:
@Entity
@Inheritance( strategy = InheritanceType.TABLE_PER_CLASS )
@DiscriminatorColumn(name="DISC", discriminatorType=STRING, length=20)
class abstract Something {
@Id
@Column
Long id;
@Column
String name;
@Column
String description;
//getters, setters, constructors
}
@Entity
@Table("FOO_SOMETHING")
@Immutable
@DiscriminatorValue("FooSomething")
class FooSomething extends Something {
}
@Entity
@Table("BAR_SOMETHING")
@Immutable
@DiscriminatorValue("BarSomething")
class BarSomething extends Something {
}