序列生成器在persistence.xml中 [英] Sequence Generator in persistence.xml

查看:114
本文介绍了序列生成器在persistence.xml中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

您必须在orm.xml中指定它。在persistence.xml中使用这个元素:

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

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

 <序列生成器名称=MY_SEQ
分配大小=1
序列-name =MY_SEQ
initial-value =1/>


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

<属性>

< id name =id>
< generated-value strategy =SEQUENCEgenerator =MY_SEQ/>

< / id>

< / attributes>
< / entity>

在这种情况下,id属性将从orm.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

解决方案

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.

这篇关于序列生成器在persistence.xml中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆