如何使用 XML 在 Hibernate 中映射这种多态性,而不创建额外的父表?

发布于 2024-10-08 23:00:35 字数 1320 浏览 5 评论 0原文

我正在尝试在 Hibernate 中映射 POJO 树,以便我可以 (a) 简洁地在各处使用 UUID 作为主键,以及 (b) 在其他不相关的表之间从外部强加类似集合的关系。使用注释似乎效果很好,但在我的一生中,我无法让它使用 HBM XML 映射以同样的方式工作。

例如,给定以下(缩写)类:

@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。当我运行 hbm2ddl 时,它会创建表 RelationshipRelationship_UniqueActivityAssociatedXML。像下面的 HQL 这样的查询似乎效果很好:

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());

同时,我正在尝试转向 XML 配置。 POJO 是从 XML 模式生成的。由于生成了源代码,我试图避免手动编辑它以添加注释。

我已经尝试了我能想到的所有 XML 配置(以及欺骗 Hibernate 工具中 的输出)。我无法想出一个配置,它不会创建涉及额外连接的额外Unique父表,在会话工厂出现错误:

关联引用未映射的类:Unique

有人对我的 XML 配置文件应该是什么样子有建议吗?还是我正在走一条糟糕的路?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

注定孤独终老 2024-10-15 23:00:35

它是这样的:

<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>

另请参阅:

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文