views:

342

answers:

1

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.

+1  A: 

Hi,

Did you look at the hibernate annotations documentation? http://www.redhat.com/docs/manuals/jboss/jboss-eap-4.3/doc/hibernate/Annotations_Reference_Guide/Overriding_metadata_through_XML.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>
Mike K.
thanks, that would probably work, but I am no longer working on that project.
Ken Liu
No problem, I simply had the same problem and took half a day to find the solution, so I figured I'll answer anyway so that someone else might find it more easily next time :)
Mike K.

related questions