2

我正在尝试将此代码转换为 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;
        ...
}

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

4

3 回答 3

1

在您的 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 {
...
}
于 2013-11-05T14:13:41.133 回答
0

不要手工。使用 hbm2ddl=auto 从 hbm 创建一个表,然后使用 Eclipse JPA Tools 或 AppFuse 甚至 Spring Roo 等工具对 Annotated JPA Pojos 进行反向工程。

于 2013-11-05T14:12:55.853 回答
0

现在它的作品。在子类中使用 @SecondaryTable

Square.java

@Entity
@Table(name = "square")
@DiscriminatorColumn(name = "squareType", discriminatorType = DiscriminatorType.INTEGER)
@DiscriminatorValue("0")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Square implements Indexable, Serializable{
  ...
}

EmbedSquare.java

@Entity
@DiscriminatorValue("2")
@SecondaryTable(name = "square_embed",
    pkJoinColumns = @PrimaryKeyJoinColumn(name = "squareId", referencedColumnName = "id"))
public class EmbedSquare extends Square {
于 2013-11-06T12:16:23.477 回答