gpt4 book ai didi

java - 从 Hibernate hbm 到 JPA 注释

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

我试图将此代码转换为 JPA 注释,但我对子类和连接完全感到困惑。

xxx.hbm.xml

<class name="com.domain.square" table="square" discriminator-value="0">
<id name="id" column="id">
<generator class="native" />
</id>
<discriminator column="squareType" type="integer" />
<property name="name" />
<property name="image" />
<property name="type" column="squareType" type="integer" insert="false" update="false" />
<property name="keywords" />

<subclass name="com.domain.Widget" discriminator-value="1">
<property name="periodical" />
</subclass>

<subclass name="com.domain.WidgetContainer" discriminator-value="2" />

<subclass name="com.more.domain.EmbedSquare" discriminator-value="3">
<join table="square_embed">
<key column="squareId"/>
<property name="objUrl" />
<property name="title" />
</join>
</subclass>

<subclass name="com.domain.social.SocialWidget" discriminator-value="4" />

</class>

方形.java

@Entity
@Table(name= "square")
@DiscriminatorColumn(columnDefinition = "squareType", discriminatorType = DiscriminatorType.INTEGER)
@DiscriminatorValue("0")
public class Square implements Indexable, Serializable{
@Id
@Column(length = 11)
@GeneratedValue
private int id;
...
}

我怎样才能继续学习子类?

最佳答案

在您的 Square 类中,您必须添加注释 @Inheritance(strategy=InheritanceType.JOINED)

像这样

@Entity
@Table
@DiscriminatorColumn(columnDefinition = "squareType", discriminatorType = DiscriminatorType.INTEGER)
@DiscriminatorValue("0")
@Inheritance(strategy=InheritanceType.JOINED)
public class Square implements Indexable, Serializable{
@Id
@Column(length = 11)
@GeneratedValue
private int id;
...
}

在你的子类中,比如“EmbedSquare”:

@Entity
@Table
@PrimaryKeyJoinColumn(name="SQUARE_ID")
public class EmbedSquare extends Square {
...
}

关于java - 从 Hibernate hbm 到 JPA 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19790661/

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