我正在尝试将此代码转换为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;
...
}
如何继续使用子类?
答案 0 :(得分: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;
...
}
在您的子类中,例如&#34; EmbedSquare&#34;:
@Entity
@Table
@PrimaryKeyJoinColumn(name="SQUARE_ID")
public class EmbedSquare extends Square {
...
}
答案 1 :(得分:0)
不要手工制作。使用hbm2ddl = auto从hbm创建表,然后使用Eclipse JPA Tools或类似AppFuse甚至Spring Roo的反向工程Annotated JPA Pojos。
答案 2 :(得分: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 {