Mr *_*gan 9 java spring hibernate spring-boot
当我尝试运行 Spring Boot 和 Hibernate 应用程序时,我发现它因为以下原因而崩溃:
org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing sequence [hibernate_sequence]
但我不明白为什么这是因为我没有使用 Hibernate 序列。我在 Apache Derby 中的表如下:
CREATE TABLE TEAM (
TEAM_ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
NAME VARCHAR(50) NOT NULL,
CONSTRAINT PK_TEAM PRIMARY KEY(Team_Id)
);
CREATE TABLE PLAYER (
PLAYER_ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
NAME VARCHAR(50) NOT NULL,
NUM INTEGER NOT NULL,
POSITION VARCHAR(50) NOT NULL,
TEAM_ID INTEGER,
CONSTRAINT PK_PLAYER PRIMARY KEY(PLAYER_ID),
CONSTRAINT FK_PLAYER FOREIGN KEY(TEAM_ID) REFERENCES TEAM(TEAM_ID)
);
Run Code Online (Sandbox Code Playgroud)
我的应用程序application.properties文件是:
# Hibernate table generation.
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.DerbyTenSevenDialect
spring.jpa.show-sql=true
# Apache Derby settings
spring.datasource.driverClassName=org.apache.derby.jdbc.ClientDriver
spring.datasource.url=jdbc:derby://localhost:1527/Library
spring.datasource.username=username
spring.datasource.password=password`
Run Code Online (Sandbox Code Playgroud)
所涉及的两个 Java 类是:
@Entity
@Table(name = "TEAM")
public class Team {
@Id
@Column(name = "TEAM_ID", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer teamId;
@Column(name = "NAME")
private String name;
@OneToMany(cascade = CascadeType.ALL,
fetch = FetchType.EAGER,
mappedBy = "team")
private List<Player> players;
Run Code Online (Sandbox Code Playgroud)
和:
@Entity
@Table(name = "PLAYER")
public class Player {
@Id
@Column(name = "PLAYER_ID", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer playerId;
@Column(name = "NAME")
private String name;
@Column(name = "NUM")
private int num;
@Column(name = "POSITION")
private String position;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "TEAM_ID", nullable = true)
private Team team;
Run Code Online (Sandbox Code Playgroud)
谁能告诉我哪里错了?
Maven 依赖项是:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.14.2.0</version>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
Zha*_*ang 11
遇到这个问题,下面是我的搜索结果:
GenerationType.AUTO如果您在 java bean 中使用,则默认情况下 hibernate 将使用hibernate_sequence该序列。
因此,一种选择是通过以下方式在数据库中创建此序列:
create sequence <schema>.hibernate_sequence
或者您可以@GeneratedValue(strategy = GenerationType.IDENTITY)在 java bean 源代码中使用,它不需要这样的序列。
引用 Java 持久性/身份:
身份排序使用数据库中特殊的 IDENTITY 列,允许数据库在插入行时自动为对象分配 id。许多数据库都支持标识列,例如 MySQL、DB2、SQL Server、Sybase 和 Postgres。Oracle不支持IDENTITY列,但可以通过使用序列对象和触发器来模拟它们。
进一步阅读:
休眠中的 GenerationType.AUTO 与 GenerationType.IDENTITY
| 归档时间: |
|
| 查看次数: |
14514 次 |
| 最近记录: |