简体   繁体   English

持久化JPA对象时发生异常

[英]exception while persist the JPA object

I have made an entity class and add the annotation @id and @GeneratedValue on ID field. 我做了一个实体类,并在ID字段上添加了注释@id@GeneratedValue While persisting the object I was wondering it will automatically set the value in ID field but when commit the data into db I got sequence exception. 在持久化对象时,我想知道它将自动在ID字段中设置值,但是将数据提交到db时却出现了序列异常。 Then I had set the ID and persist it will commit successfully. 然后,我已经设置了ID,并坚持将其成功提交。

I have ran my code again and try to persist the data with the same values and I got exception related to the duplication.. 我再次运行我的代码,尝试使用相同的值保存数据,但出现了与复制有关的异常。

I have searched and change my @GeneratedValue annotation with the following: 我已经搜索并使用以下内容更改了@GeneratedValue批注:

@SequenceGenerator(name= "seq",sequenceName="seq")
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="seq")

and the data is persist successfully without setting the ID value manually. 并且无需手动设置ID值即可成功持久保存数据。

So my question is what is the diffrenece between @GeneratedValue and @SequenceGenerator ain't these two annotation used for the same purpose? 所以我的问题是@GeneratedValue@SequenceGenerator之间的@SequenceGenerator是不是这两个注释用于同一目的? and how can we use the strategy in the attribute? 以及如何在属性中使用策略?

Please guide me.Help will be appreciated Thanks 请指导我。将不胜感激谢谢

I found this may help you. 我发现可能对您有帮助。 From the Javadoc for SequenceGenerator 从Javadoc for SequenceGenerator

Defines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation. 定义一个主键生成器,当为GeneratedValue批注指定了生成器元素时,可以按名称引用。 A sequence generator may be specified on the entity class or on the primary key field or property. 可以在实体类或主键字段或属性上指定序列生成器。 The scope of the generator name is global to the persistence unit (across all generator types). 生成器名称的范围对于持久性单元是全局的(跨所有生成器类型)。

I will try to cover your 2 questions in one shot. 我将一口气解决您的两个问题。

@SequenceGenerator is used for point to the sequence you created in the DB with the ' sequenceName ' property and then you give it a name with the ' name ' property. @SequenceGenerator用于指向您在数据库中使用' sequenceName '属性创建的sequenceName ,然后使用' name '属性为其命名。 Example: 例:

 @SequenceGenerator( name="myIdSeq", sequenceName="MY_ID_SEQ", allocationSize=1, initialValue=1 )
 @GeneratedValue( strategy=GenerationType.SEQUENCE, generator="myIdSeq" )

One note this name can be shared across your persistence unit. 请注意,此name可以在您的持久性单元之间共享。




@GeneratedValue defines what strategy you want the ID to be generated, typically we have stategies like @GeneratedValue定义了您希望生成ID的策略,通常我们会采用以下策略

1. IDENTITY(AUTO) which will auto increment and is popular in MySQL 1. IDENTITY(AUTO)会自动递增,在MySQL中很流行

2. SEQUENCE which points to a sequence Gernerator and is the one you use above. 2. SEQUENCE指向一个序列Gernerator,它是您在上面使用的序列。 Note the generator value should match the name value in the @ SequenceGenerator 请注意, generator值应与@ SequenceGenerator中的name值匹配

3. TABLE which uses a dedicated table to stores the sequence name with column for running numbers and is suitable for enterprise level app. 3. TABLE ,该使用专用表存储序列名称和带有运行编号的列,适用于企业级应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM