开发者

Hibernate: can I override an identifier generator using XML with a custom generator?

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 i开发者_如何学编程n 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.


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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜