1

I'm trying 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?

1 Answer 1

1

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:

Sign up to request clarification or add additional context in comments.

1 Comment

Aha! That creates the exact same tables as the annotations, and passes the same query tests. Merry Christmas (or Kwanzaa or Festivus or whatever)!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.