简体   繁体   English

@SequenceGenerator 的 initValue 和 allocationSize 被忽略,生成器没有分配给 @Id 字段(H2、HIbernate、Spring)

[英]@SequenceGenerator's initValue and allocationSize are ignored and generator not assigned to @Id field (H2, HIbernate, Spring)

I use JPA in a Spring application configured to use embedded H2 database.我在配置为使用嵌入式 H2 数据库的 Spring 应用程序中使用 JPA。

I have a User entity defined like this:我有一个像这样定义的用户实体:

@Entity
@SequenceGenerator(name = "myseq", sequenceName = "MY_SEQ", initialValue = 1000, allocationSize = 1)
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "myseq")
    private Long id;

    @Column(name = "USERNAME")
    private String userName;

    @Column(name = "PASSWORD_ENCODED")
    private String passwordEncoded;

    @ManyToMany
    @JoinTable(name = "USER_ROLES", joinColumns = @JoinColumn(name = "USER_ID", referencedColumnName = "ID"), inverseJoinColumns = @JoinColumn(name = "ROLE_ID", referencedColumnName = "ID"))
    private Set<Role> roles;
    }

    //getters
}

Context is defined like this:上下文定义如下:

@Configuration
@EnableWebMvc
@EnableWebSecurity
@EnableAutoConfiguration
@EnableJpaRepositories(basePackages = "my.package")
@EntityScan(basePackages = "my.package")
@ComponentScan(basePackages = "my.package" )
public class AuthenticationWebAppContext extends WebSecurityConfigurerAdapter {
}

I can see from the log generated that MY_SEQ is generated.从生成的日志中可以看出生成了MY_SEQ。 However, initialValue and allocationSize are completely ignored and the sequence is not assigned to the id field of USER但是,initialValue 和 allocationSize 被完全忽略,序列没有分配给 USER 的 id 字段

17:22:29.236 [main] DEBUG org.hibernate.SQL - create sequence my_seq start with 1 increment by 1
17:22:29.237 [main] DEBUG org.hibernate.SQL - create table role (id bigint generated by default as identity, name varchar(255), primary key (id))
17:22:29.248 [main] DEBUG org.hibernate.SQL - create table user (id bigint not null, password_encoded varchar(255), username varchar(255), primary key (id))

So, when a row insert is attempted by data.sql file, I got the following error:因此,当 data.sql 文件尝试行插入时,出现以下错误:

Caused by: org.h2.jdbc.JdbcSQLException: NULL not allowed for column "ID"; SQL statement:
INSERT INTO user (USERNAME, PASSWORD_ENCODED) VALUES ('user1', '<some_giberish>') [23502-194]

What I am missing?我缺少什么?

Your JPA set-up is correct but you have to keep in mind that the persistence provider will only take care of generating the id for you (additional query to the database for the next value of the sequence) when inserting through Hibernate or JPA API.您的 JPA 设置是正确的,但您必须记住,当通过 Hibernate 或 JPA API 插入时,持久性提供程序只会为您生成 id(对数据库的附加查询以获取序列的下一个值)。

This will not happen when you perform the insertions 'by hand' in the data.sql file.当您在data.sql文件中“手动”执行插入时,不会发生这种情况。 You would have to invoke the sequence manually there:您必须在那里手动调用序列:

INSERT INTO user (ID, USERNAME, PASSWORD_ENCODED)
VALUES (NEXTVAL('my_seq')'user1', '<some_giberish>')

Edit编辑

This property: spring.jpa.hibernate.use-new-id-generator-mappings or hibernate.id.new_generator_mappings=true (if your not using spring boot) would allow for initialValue feature support.此属性: spring.jpa.hibernate.use-new-id-generator-mappingshibernate.id.new_generator_mappings=true (如果您不使用 spring boot)将允许initialValue功能支持。

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

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