我正在运行MariaDB 10.3.7
提供序列的最新服务器。
我将我的序列创建为:
drop sequence if exists user_account_id_seq;
create sequence user_account_id_seq;
drop table if exists user_account;
create table user_account (
-- id bigint(20) unsigned not null auto_increment,
id bigint(20) unsigned not null default (next value for user_account_id_seq),
version int(10) unsigned not null,
firstname varchar(255) not null,
lastname varchar(255) not null,
password varchar(100),
password_salt varchar(50),
readable_password varchar(50),
email varchar(50) not null,
confirmed_email bit(1) not null check (confirmed_email in (0, 1)),
work_phone varchar(20),
unique key email (email),
primary key (id)
);
drop sequence if exists user_role_id_seq;
create sequence user_role_id_seq;
drop table if exists user_role;
create table user_role (
-- id bigint(20) unsigned not null auto_increment,
id bigint(20) unsigned not null default (next value for user_role_id_seq),
version int(10) unsigned not null,
user_account_id bigint(20) unsigned not null,
role varchar(50) not null,
unique key user_role_u1 (user_account_id, role),
key user_account_id (user_account_id),
constraint user_role_fk1 foreign key (user_account_id) references user_account (id),
primary key (id)
);
看起来像:
MariaDB [useraccounttest]> show create sequence user_account_id_seq;
+---------------------+--------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------------------+--------------------------------------------------------------------------------------------------------------------------------------------+
| user_account_id_seq | CREATE SEQUENCE `user_account_id_seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB |
+---------------------+--------------------------------------------------------------------------------------------------------------------------------------------+
我想知道为什么我的序列会生成一个负数:
19:16:42.294 [main] DEBUG org.hibernate.event.internal.AbstractSaveEventListener - Generated identifier: -29, using strategy: org.hibernate.id.enhanced.SequenceStyleGenerator
我正在使用方言org.hibernate.dialect.MariaDB103Dialect
。
这是配置:
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_generator")
@Column(name = "id", updatable = false, nullable = false)
private Long id;
...
}
@Entity
@Table(name = "user_account")
@SequenceGenerator(name = "id_generator", sequenceName = "user_account_id_seq")
public class User extends AbstractEntity {
...
}
这是我的属性:
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MariaDB103Dialect
spring.datasource.driver-class-name=net.sf.log4jdbc.DriverSpy
spring.datasource.url=jdbc:log4jdbc:mysql://localhost:3306/useraccounttest?serverTimezone=UTC
spring.datasource.username=useraccount
spring.datasource.password=mypassword
spring.jpa.show-sql=true
控制台日志块:
19:16:42.294 [main] DEBUG org.springframework.orm.jpa.JpaTransactionManager - Participating in existing transaction
19:16:42.294 [main] DEBUG org.hibernate.event.internal.AbstractSaveEventListener - Generated identifier: -29, using strategy: org.hibernate.id.enhanced.SequenceStyleGenerator
19:16:42.295 [main] DEBUG org.hibernate.event.internal.AbstractFlushingEventListener - Processing flush-time cascades
19:16:42.295 [main] DEBUG org.hibernate.event.internal.AbstractFlushingEventListener - Dirty checking collections
19:16:42.295 [main] DEBUG org.hibernate.engine.internal.Collections - Collection found: [com.thalasoft.userdata.jpa.domain.User.userRoles#-29], was: [<unreferenced>] (initialized)
19:16:42.295 [main] DEBUG org.hibernate.event.internal.AbstractFlushingEventListener - Flushed: 1 insertions, 0 updates, 0 deletions to 1 objects
19:16:42.295 [main] DEBUG org.hibernate.event.internal.AbstractFlushingEventListener - Flushed: 1 (re)creations, 0 updates, 0 removals to 1 collections
19:16:42.295 [main] DEBUG org.hibernate.internal.util.EntityPrinter - Listing entities:
19:16:42.296 [main] DEBUG org.hibernate.internal.util.EntityPrinter - com.thalasoft.userdata.jpa.domain.User{userRoles=[], firstname=Stephane, password=toto, readablePassword=null, confirmedEmail=false, workPhone=null, id=-29, version=0, passwordSalt=, email=component[value]{value=stephane@thalasoft.com}, lastname=Eybert}
19:16:42.296 [main] DEBUG org.hibernate.SQL - insert into user_account (version, confirmed_email, email, firstname, lastname, password, password_salt, readable_password, work_phone, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into user_account (version, confirmed_email, email, firstname, lastname, password, password_salt, readable_password, work_phone, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
19:16:42.297 [main] DEBUG jdbc.audit - 1. PreparedStatement.new PreparedStatement returned com.zaxxer.hikari.pool.ProxyConnection.prepareStatement(ProxyConnection.java:318)
19:16:42.302 [main] DEBUG jdbc.audit - 1. Connection.prepareStatement(insert into user_account (version, confirmed_email, email, firstname, lastname, password, password_salt, readable_password, work_phone, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)) returned net.sf.log4jdbc.PreparedStatementSpy@44bc2449 com.zaxxer.hikari.pool.ProxyConnection.prepareStatement(ProxyConnection.java:318)
19:16:42.302 [main] DEBUG jdbc.audit - 1. PreparedStatement.setInt(1, 0) returned com.zaxxer.hikari.pool.HikariProxyPreparedStatement.setInt(HikariProxyPreparedStatement.java:-1)
19:16:42.303 [main] DEBUG jdbc.audit - 1. PreparedStatement.setBoolean(2, false) returned com.zaxxer.hikari.pool.HikariProxyPreparedStatement.setBoolean(HikariProxyPreparedStatement.java:-1)
19:16:42.303 [main] DEBUG jdbc.audit - 1. PreparedStatement.setString(3, "stephane@thalasoft.com") returned com.zaxxer.hikari.pool.HikariProxyPreparedStatement.setString(HikariProxyPreparedStatement.java:-1)
19:16:42.303 [main] DEBUG jdbc.audit - 1. PreparedStatement.setString(4, "Stephane") returned com.zaxxer.hikari.pool.HikariProxyPreparedStatement.setString(HikariProxyPreparedStatement.java:-1)
19:16:42.304 [main] DEBUG jdbc.audit - 1. PreparedStatement.setString(5, "Eybert") returned com.zaxxer.hikari.pool.HikariProxyPreparedStatement.setString(HikariProxyPreparedStatement.java:-1)
19:16:42.304 [main] DEBUG jdbc.audit - 1. PreparedStatement.setString(6, "toto") returned com.zaxxer.hikari.pool.HikariProxyPreparedStatement.setString(HikariProxyPreparedStatement.java:-1)
19:16:42.304 [main] DEBUG jdbc.audit - 1. PreparedStatement.setString(7, "") returned com.zaxxer.hikari.pool.HikariProxyPreparedStatement.setString(HikariProxyPreparedStatement.java:-1)
19:16:42.304 [main] DEBUG jdbc.audit - 1. PreparedStatement.setNull(8, 12) returned com.zaxxer.hikari.pool.HikariProxyPreparedStatement.setNull(HikariProxyPreparedStatement.java:-1)
19:16:42.305 [main] DEBUG jdbc.audit - 1. PreparedStatement.setNull(9, 12) returned com.zaxxer.hikari.pool.HikariProxyPreparedStatement.setNull(HikariProxyPreparedStatement.java:-1)
19:16:42.305 [main] DEBUG jdbc.audit - 1. PreparedStatement.setLong(10, -29) returned com.zaxxer.hikari.pool.HikariProxyPreparedStatement.setLong(HikariProxyPreparedStatement.java:-1)
19:16:42.305 [main] DEBUG jdbc.sqlonly - com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61)
1. insert into user_account (version, confirmed_email, email, firstname, lastname, password, password_salt,
readable_password, work_phone, id) values (0, 0, 'stephane@thalasoft.com', 'Stephane', 'Eybert',
'toto', '', NULL, NULL, -29)
19:16:42.307 [main] ERROR jdbc.audit - 1. PreparedStatement.executeUpdate() insert into user_account (version, confirmed_email, email, firstname, lastname, password, password_salt,
readable_password, work_phone, id) values (0, 0, 'stephane@thalasoft.com', 'Stephane', 'Eybert',
'toto', '', NULL, NULL, -29)
com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Out of range value for column 'id' at row 1