我正在使用内存中的H2数据库对spring应用程序进行一些测试。数据库有一个“消息”表,其模式为:
CREATE TABLE `message` (
`id` bigint(20) NOT NULL,
`body` CLOB,
`channels` varchar(500) NOT NULL,
`end_date` datetime DEFAULT NULL,
`start` datetime DEFAULT NULL,
`status` varchar(120) DEFAULT NULL,
`title` varchar(120) DEFAULT NULL,
`approved` bit(1) DEFAULT NULL,
`approver_name` varchar(150) DEFAULT NULL,
`approver_sub` varchar(120) DEFAULT NULL,
`create_date` datetime DEFAULT NULL,
`creator_name` varchar(150) DEFAULT NULL,
`creator_sub` varchar(120) DEFAULT NULL,
`modify_date` datetime DEFAULT NULL,
`affiliations` varchar(500) NOT NULL,
`html` bit(1) NOT NULL,
`reject_reason` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
)
一个实体类:
@Entity
public class Message {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", length = 100, nullable = false, unique = true)
private long id;
@Column(name = "title", length = 120, nullable = true)
private String title;
@Lob
@Column(name = "body", nullable = true)
private String body;
...
}
在我的代码中,我试图在消息表中插入一个新条目。我有一个服务方法,比如:
public Message saveMessage(MessageInput messageInput, LocalDateTime start, LocalDateTime end, String status, String requestor) {
Message message = new Message();
message.setTitle(messageInput.getMessage_title());
message.setBody(messageInput.getMessage_body());
messageRepository.save(message);
}
其中messageRepository是一个org.springframework.data.repository.CrudRepository
我打开了日志记录,当我运行我的代码时,我得到以下日志消息:
Hibernate: insert into message (id, affiliations, approved, approver_name, approver_sub, body, channels, create_date, creator_name, creator_sub, end_date, html, modify_date, reject_reason, start, status, title) values (default, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2023-08-31 12:30:03 [main] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [[Employee, Staff]]
2023-08-31 12:30:03 [main] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [2] as [BOOLEAN] - [false]
2023-08-31 12:30:03 [main] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [3] as [VARCHAR] - [null]
2023-08-31 12:30:03 [main] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [4] as [VARCHAR] - [null]
2023-08-31 12:30:03 [main] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [5] as [CLOB] - [the test body]
2023-08-31 12:30:03 [main] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [6] as [VARCHAR] - [[57823ff6-e262-4201-8766-84cd17dfe116]]
2023-08-31 12:30:03 [main] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [7] as [TIMESTAMP] - [2023-08-31T12:30:03.304774]
2023-08-31 12:30:03 [main] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [8] as [VARCHAR] - [ptb014]
2023-08-31 12:30:03 [main] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [9] as [VARCHAR] - [ptb014]
2023-08-31 12:30:03 [main] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [10] as [TIMESTAMP] - [2023-09-07T12:30:03.268680]
2023-08-31 12:30:03 [main] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [11] as [BOOLEAN] - [false]
2023-08-31 12:30:03 [main] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [12] as [TIMESTAMP] - [2023-08-31T12:30:03.304793]
2023-08-31 12:30:03 [main] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [13] as [VARCHAR] - [null]
2023-08-31 12:30:03 [main] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [14] as [TIMESTAMP] - [2023-08-31T12:30:03.268680]
2023-08-31 12:30:03 [main] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [15] as [VARCHAR] - [MODERATOR_APPROVED]
2023-08-31 12:30:03 [main] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [16] as [VARCHAR] - [A Title]
2023-08-31 12:30:03 [main] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - NULL not allowed for column "ID"; SQL statement:
insert into message (id, affiliations, approved, approver_name, approver_sub, body, channels, create_date, creator_name, creator_sub, end_date, html, modify_date, reject_reason, start, status, title) values (default, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [23502-220]
查看日志,似乎插入包含一个id参数,但由于数据库期望生成该值,因此它被传递为NULL。我做了一些研究,我看到的大多数建议都建议将GenerationType设置为IDENTITY,或者将spring.jpa.hibernate. ddl-auto配置更改为“update”。我两种都试过了,结果还是一样。我需要做些什么才能让身份生成发挥作用?
1条答案
按热度按时间ifmq2ha21#
您需要将
id
列声明为标识列:在H2的MySQL或MariaDB兼容模式中,您可以使用非标准
而不是.