我试图将这段代码转换为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>


Square.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 {
...
}

08-06 16:32