gpt4 book ai didi

java - 具有类继承的 Hibernate Subselect

转载 作者:行者123 更新时间:2023-11-30 10:25:50 25 4
gpt4 key购买 nike

我有两个具有相同字段但来自不同表的对象(foo_something 和 bar_something 实际上不是普通表,有几个连接操作的结果)。我使用 @Subselect hibernate 注释来获取数据。我可以像这样制作类结构,但哪个工作正常?这个类结构不起作用,因为注解不继承。如果我使用 @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 {

}

UPD。

谢谢,@HenryMartens,我用 TABLE_PER_CLASS 策略添加了注释 @Entity@Inheritance,并使类抽象,现在它工作正常:

@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 {

}

关于java - 具有类继承的 Hibernate Subselect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45918095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com