pee*_*eer 1 java hibernate spring-boot crud-repository
我有一个带有 h2 数据库的 Spring Boot 项目。
我有一个实体类,从中生成架构:
@NoArgsConstructor
@Entity
@Table(name = "NAMES")
public class Name {
@Id
@GeneratedValue
public Long id;
@Column(nullable = false)
public String name;
public Name(String name) {
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个data.sql文件:
insert into names (id, name) values (1, 'Alex');
insert into names (id, name) values (2, 'Bob');
Run Code Online (Sandbox Code Playgroud)
我的 application.properties 是:
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.defer-datasource-initialization=true
spring.jpa.show-sql=true
Run Code Online (Sandbox Code Playgroud)
应用程序启动得很好,我可以通过 localhost:8080/h2-console 确认数据已加载到数据库中。但我无法将新数据保存到表中
//public interface NameRepository extends CrudRepository<Name,Long> {}
@RestController
@Service
public class MyController {
@Autowired
private final NameRepository nameRepository;
@PostMapping("/triggerError")
public ResponseEntity<Void> trigger() {
Name newName = new Name("Chris");
nameRepository.save(newName);
return ResponseEntity.ok().build();
}
}
Run Code Online (Sandbox Code Playgroud)
错误信息是:
could not execute statement; SQL [n/a]; constraint [\"PRIMARY KEY ON PUBLIC.NAMES(ID) ( /* key:1 */ CAST(1 AS BIGINT), 'Alex')\"; SQL statement:
insert into names (name, id) values (?, ?) [23505-210]];
Run Code Online (Sandbox Code Playgroud)
我认为这意味着 spring 想要在 id=1 处插入新名称,而没有意识到 id 1 和 2 已经在使用中。我想正确的参数@GeneratedValue可以解决这个问题,但我不明白它们的含义以及选择哪一个。
@GeneratedValue(strategy = GenerationType.AUTO)是默认值,见上文。
@GeneratedValue(strategy = GenerationType.TABLE)相同的错误
@GeneratedValue(strategy = GenerationType.SEQUENCE)相同的错误
@GeneratedValue(strategy = GenerationType.IDENTITY)不同的错误:
...
Caused by: org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: NULL not allowed for column \"ID\"; SQL statement:\ninsert into names (id, name) values (null, ?) [23502-210]
...
could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
Run Code Online (Sandbox Code Playgroud)
所以显然这不是注释,而是其他东西。
我放弃了,这是我的 MRE:https ://github.com/timo-a/duckpond-spring-backend/tree/debug/ saving
@Shankar Ghimire 部分正确。这既是一个 Hibernate bug,也是一个 data.sql 问题。将您的 Hibernate 版本更新到 5.6.5:
implementation('org.springframework.boot:spring-boot-starter-data-jpa'){
exclude group: 'org.hibernate', module: 'hibernate-core'
}
implementation 'org.hibernate:hibernate-core:5.6.5.Final'
Run Code Online (Sandbox Code Playgroud)
由于您使用的是 h2 auto_increment,因此您应该删除 data.sql 中的 id
implementation('org.springframework.boot:spring-boot-starter-data-jpa'){
exclude group: 'org.hibernate', module: 'hibernate-core'
}
implementation 'org.hibernate:hibernate-core:5.6.5.Final'
Run Code Online (Sandbox Code Playgroud)
该序列不会递增,否则会导致唯一索引或主键冲突。
顺便说一句,您的存储库使用 Integer 作为您的 PK,而您的实体使用 Long。由于问题被标记为 JPA,因此使用:
public interface NameRepository extends JpaRepository<Name,Long> {}
Run Code Online (Sandbox Code Playgroud)
您将数据插入名为“scores”的表中,但我认为这是一个拼写错误。
| 归档时间: |
|
| 查看次数: |
3469 次 |
| 最近记录: |