Hibernate:我可以使用 XML 和自定义生成器覆盖标识符生成器吗?

发布于 2024-08-26 10:31:39 字数 520 浏览 8 评论 0原文

我想在我的应用程序中使用自定义序列生成器,但该实体位于与其他应用程序共享的域模型 jar 中。显然,实体注释可以在 orm.xml 中被覆盖,但我无法找出正确的 XML 咒语来使其正常工作。

我可以像这样修改实体中的注释:

@GenericGenerator(name = "MYGEN", strategy = "MyCustomGenerator")
@GeneratedValue(generator = "MYGEN")

但我需要以某种方式将其映射到 orm.xml 以便覆盖原始注释。在此处查看 orm.xml 架构 看来我什至无法指定除了“序列”和“表”之外的生成类型。

我应该提到,如果这很重要的话,我正在将 JPA 与 Hibernate 一起使用。

I want to use a custom sequence generator in my application, but the entity is located in a domain model jar that is shared with other applications. Apparently entity annotations can be overridden in orm.xml but I can't figure out the proper XML incantation to get this to work.

I can modify the annotation in the entity like this this:

@GenericGenerator(name = "MYGEN", strategy = "MyCustomGenerator")
@GeneratedValue(generator = "MYGEN")

But I need to somehow map this to orm.xml in order to override the original annotation. Looking at the orm.xml schema here it appears that I can't even specify a generation type besides "sequence" and "table".

I should mention that I am using JPA with Hibernate, if that matters.

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

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

发布评论

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

评论(1

把回忆走一遍 2024-09-02 10:31:39

你看过hibernate注释文档吗?
https://docs.jboss.org/ hibernate/stable/annotations/reference/en/html/xml-overriding.html

它很好地解释了如何覆盖 orm xml 中的注释配置,

例如,考虑这个实体:

@Entity
@Table(name = "API_USERS")
public class ApiUser {

    @Id
    @Column(name = "ID", unique = true, nullable = false, precision = 6, scale = 0)
    private Long id;
    ...
}

To override the ID field with a我使用的序列生成器:

<?xml version="1.0" encoding="UTF-8"?>

<entity-mappings 
  xmlns="http://java.sun.com/xml/ns/persistence/orm"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd"
  version="1.0">
    <entity class="com.muzicall.apiusers.entity.ApiUser" access="FIELD">
        <attributes>
            <id name="id">
                <column name="id"/>
                <generated-value generator="apiUserIdGen" strategy="SEQUENCE"/>
                <sequence-generator name="apiUserIdGen" sequence-name="api_users_seq" allocation-size="1"/>
            </id>
        </attributes>
    </entity>
</entity-mappings>

Did you look at the hibernate annotations documentation?
https://docs.jboss.org/hibernate/stable/annotations/reference/en/html/xml-overriding.html

It explains quite well how to override the annotation configurations in the orm xmls,

For exmaple, consider this entity:

@Entity
@Table(name = "API_USERS")
public class ApiUser {

    @Id
    @Column(name = "ID", unique = true, nullable = false, precision = 6, scale = 0)
    private Long id;
    ...
}

To override the ID field with a sequence generator I used:

<?xml version="1.0" encoding="UTF-8"?>

<entity-mappings 
  xmlns="http://java.sun.com/xml/ns/persistence/orm"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd"
  version="1.0">
    <entity class="com.muzicall.apiusers.entity.ApiUser" access="FIELD">
        <attributes>
            <id name="id">
                <column name="id"/>
                <generated-value generator="apiUserIdGen" strategy="SEQUENCE"/>
                <sequence-generator name="apiUserIdGen" sequence-name="api_users_seq" allocation-size="1"/>
            </id>
        </attributes>
    </entity>
</entity-mappings>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文