我尝试在Spring项目的项目中将hibernate从4升级到5 4.2.升级完成后,当我调用更新方法时,我在堆栈跟踪中发现以下错误.
10:53:32,185 ERROR TableStructure:149 - could not read a hi value
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.hibernate_sequence' doesn't exist
Run Code Online (Sandbox Code Playgroud)
我用注释更改了自动递增的Id
@GeneratedValue(strategy=GenerationType.AUTO)
Run Code Online (Sandbox Code Playgroud)
仍然是错误.
我有以下类描述片段:
...
@Column(name = "invalidate_token_date")
@Temporal(TemporalType.TIMESTAMP)
private LocalDateTime invalidateTokenDate;
....
Run Code Online (Sandbox Code Playgroud)
此代码不起作用,因为@Temporal不支持LocalDateTime.我看到的建议如何使用LocalDateTime从乔达时间,但我使用Java 8.
请给我一些建议.
PS
这是我目前的JPA依赖:
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud) 迁移到Hibernate 5.2.7后,我似乎得到了id字段的错误值.
我的代码:
@Id @SearchableId
@GeneratedValue(strategy=GenerationType.AUTO, generator="hms_seq_gen")
@SequenceGenerator(name="hms_seq_gen", sequenceName="patregn_seq")
protected Integer ID;
Run Code Online (Sandbox Code Playgroud)
Hibernate触发此查询:
select nextval ('patregn_seq')
它给出了5367.表中id字段的最后一个值是5358.
我明白了
ERROR: duplicate key value violates unique constraint "patientregistration_pkey"
[java] Detail: Key (id)=(5318) already exists.
我相信这个问题类似于这和这个,但我不得不问,因为给出的解决方案有没有为我工作:
我补充道
<property value="true" name="hibernate.id.new_generator_mappings"/>
到我的persistence.xml,但无济于事.任何帮助将不胜感激.
private static final String SEQUENCE = "my_seq";
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQUENCE)
@SequenceGenerator(name = SEQUENCE, sequenceName = SEQUENCE)
private Long titId;
Run Code Online (Sandbox Code Playgroud)
这将创建以下架构:
CREATE SEQUENCE my_seq
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1000
CACHE 1;
Run Code Online (Sandbox Code Playgroud)
观察:当我将current value序列设置为 1 时,第一个@Id自动生成的是50。当我将值设置为 时1000,第一个 id 为50000。
因此,不知何故,序列的当前值总是乘以50。为什么?我怎样才能防止这种情况并只使用序列中的nexval?