Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequence Generator in persistence.xml

In JPA, generally we specify the sequence generator in the entity bean. Can we specify this in the persistence.xml? if yes, pls share the steps neeeded

like image 660
Ashish K Agarwal Avatar asked Jan 20 '23 00:01

Ashish K Agarwal


1 Answers

You have to specify it in the orm.xml. In the persistence.xml use this element:

 <mapping-file>META-INF/orm.xml</mapping-file>

Then in your orm.xml (orm.xml will override annotations if you specify different attributes in it)

  <sequence-generator name="MY_SEQ"
    allocation-size="1"
    sequence-name="MY_SEQ"
    initial-value="1" />


 <entity class="my.entities.Entity" name="Entity">
        <table name="Entity"/>

        <attributes>

            <id name="id">
                    <generated-value strategy="SEQUENCE" generator="MY_SEQ"/>

            </id>

        </attributes>
    </entity>

In this case, the id property will be set from the orm.xml. Any other annotations you are using for other properties will still work.

like image 56
Allan Avatar answered Jan 24 '23 19:01

Allan