persistence.xml中的Sequence Generator

Ash*_*wal 3 java hibernate jpa

在JPA中,通常我们在实体bean中指定序列生成器.我们可以在persistence.xml中指定它吗?如果是,请分享所需的步骤

All*_*lan 8

您必须在orm.xml中指定它.在persistence.xml中使用以下元素:

 <mapping-file>META-INF/orm.xml</mapping-file>
Run Code Online (Sandbox Code Playgroud)

然后在你的orm.xml中(如果在其中指定不同的属性,orm.xml将覆盖注释)

  <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>
Run Code Online (Sandbox Code Playgroud)

在这种情况下,将从orm.xml设置id属性.您用于其他属性的任何其他注释仍然有效.