开发者

How can I map this polymorphism in Hibernate, using XML, without creating extra parent tables?

I'm tryi开发者_StackOverflow中文版ng to map a tree of POJOs in Hibernate so that I can (a) concisely use UUIDs for primary keys everywhere and (b) externally impose set-like relationships between otherwise unrelated tables. This seems to work great using annotations, but for the life of me I can't get it to work the same way using HBM XML mapping.

For example, given the following (abbreviated) classes:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Unique {
    private String uuid;
}

@Entity
public class Relationship extends Unique {
    @OneToMany
    private Set<Unique> members;
}

@Entity
public class Activity extends Unique {
    private String name;
}

@Entity
public class AssociatedXML extends Unique {
    @Lob
    private String xml;
}

...easy peasy. When I run hbm2ddl it creates the tables Relationship, Relationship_Unique, Activity, and AssociatedXML. Queries like the following HQL seem to work great:

session.createQuery("select xml "
    + "from AssociatedXML as xml, Relationship as rel "
    + "left join rel.members as m "
    + "where m.uuid = :uuid").setString("uuid", activity.getUuid());

Meanwhile, I am trying to move to XML configuration. The POJOs are being generated from an XML schema. Since the source is generated, I am trying to avoid hand-editing it to add the annotations.

I have tried every XML configuration I can think of (as well as fooling with the output of <hbm2hbmxml/> in the Hibernate tools). I can't come up with a configuration that doesn't either create an additional Unique parent table involving an extra join, or fail in the session factory with the error:

Association references unmapped class: Unique

Does anybody have a suggestion as to what my XML config files should look like? Or am I going down a bad path?


It's something like this:

<class name="Unique" abstract = "true">
    <id name="uuid" />

    <union-subclass name="Relationship">
        <set name="members" table = "Relationship_Unique" >
            <key />
            <many-to-many class = "Unique" unique="true"/>
        </set>
    </union-subclass>

    <union-subclass name="Activity">
        <property name = "name" />
    </union-subclass>

    <union-subclass name="AssociaXML">
        <property name = "xml" />
    </union-subclass>
</class>

See also:

  • 8.3.1. One-to-many
  • 10.1.5. Table per concrete class
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜