Question

I'm trying to turn this code to JPA annotations but I'm totally confused with the subclass and the join.

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;
        ...
}

How can I continue with the subclasses?

Was it helpful?

Solution 3

Now it's works. using a @SecondaryTable in the subclass

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 {

OTHER TIPS

In your Square class, you have to put the annotation @Inheritance(strategy=InheritanceType.JOINED)

Like this

@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;
        ...
}

And in your subclasses like "EmbedSquare":

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

Don't handcraft. Create a tables from the hbm using hbm2ddl=auto and then reverse engineer Annotated JPA Pojos using Eclipse JPA Tools or something like AppFuse or even Spring Roo.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top