如何使用 XML 在 Hibernate 中映射这种多态性,而不创建额外的父表?
我正在尝试在 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 时,它会创建表 Relationship
、Relationship_Unique
、Activity
和 AssociatedXML
。像下面的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它是这样的:
另请参阅:
It's something like this:
See also: