Enzo
Enzo

Reputation: 55

OpenJPA - How to make work strategy=Sequence against Oracle and strategy=Identity against MySql

I deal with an application which supports both Oracle and MySql and makes of openJPA.

Issue

For a given entity there is column which is populated based on some generatedValue. For some issue on db locks, I have to turn it from tableGenerator to sequenceGenerator. This worked fine against Oracle:

@Override
@Basic
@Column(name = "ORDER_NUMBER", length = GUID_LENGTH, nullable = false, unique = true)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ORD_SEQ")
@SequenceGenerator(sequenceName = "ORDERNUMBER", allocationSize = 1, name = "ORD_SEQ")
public String getOrderNumber() {
    return orderNumber;
}

Of course it does not work against mySql as it does not support sequence object.

Then I went though the article here: https://vladmihalcea.com/how-to-replace-the-table-identifier-generator-with-either-sequence-or-identity-in-a-portable-way/ which suggested to load orm file only when the build is done against mySql and override the property there such as:

<entity class="com.ecommerceAppName.extensions.domain.cartorder.impl.ExtCartOrderImpl" metadata-complete="false" access="PROPERTY">
    <attributes>
        <id name="orderNumber">
            <generated-value strategy="IDENTITY"/>
        </id>
    </attributes>
</entity>

I tried to build with that but the startup fall down in some errors, complaining about missing mappings.

I see from this question: Override JPA only Table Generator of Entity with orm.xml (Hibernate) that I cannot override a specific property of JPA entity but instead I must report all the mappings. Is that valid also for openJPA?

Given the same code, is there any chance I could use:

Upvotes: 1

Views: 89

Answers (1)

Chris
Chris

Reputation: 21145

Your first link is using hibernate as the JPA provider and overriding annotations with an ORM.xml, showing even Hibernate can do it - the question you linked is specific to hibernate mappings, so nothing to do with standard JPA/orm.xml handling.

OpenJPA and any JPA provider should also override the annotations with what is in the orm.xml. The only time the orm.xml forces annotations to be ignored is if it is marked as xml-mapping-metadata-complete

Upvotes: 0

Related Questions