我在表中有一個帶有復合主鍵的舊資料庫project。(BaseEntity 包含 lastModifiedDate 和 lastModifiedBy 的公共屬性)
@Entity
@IdClass(ProjectPk.class)
public class Project extends BaseEntity {
@Id
@GeneratedValue(strategy=GenerationType.TABLE, generator="nextProjectId")
@TableGenerator(
name="nextProjectId",
table="projectId",
pkColumnName = "proj_Id",
pkColumnValue="proj_id"
)
private Long projId;
@Id
private int version;
//other properties, getters and setters omitted for clarity
}
PK課
public class ProjectPk implements java.io.Serializable {
private int projId;
private int version;
//both constructoirs, equals, hashcode, getters and setters omitted for clarity
}
- 我有 flyway 遷移檔案來模擬生產資料庫。
drop table if exists project;
CREATE TABLE project
(
proj_id bigint,
version int,
-- other columns omitted for clarity
PRIMARY KEY (`proj_id`, `version`)
) ENGINE=InnoDB;
drop table if exists project_id;
CREATE TABLE project_id
(
proj_id bigint
) ENGINE=InnoDB;
- flyway 按照遷移檔案中的順序創建表
Table: project_id
Columns:
proj_id bigint
...
Table: project
Columns:
proj_id bigint PK
version int PK
...
在 Maven 構建期間,我收到驗證錯誤
Schema-validation: wrong column type encountered in column [proj_id] in table [project_id]; found [bigint (Types#BIGINT)], but expecting [varchar(255) (Types#VARCHAR)]
我做錯了什么讓休眠期望[varchar(255) (Types#VARCHAR)]?
這是帶有 MySql 資料庫的 SpringBoot 專案 2.6.6
uj5u.com熱心網友回復:
我發現您的代碼存在以下問題:
Project.projId(Long 型別) 和ProjectPk.projId(int 型別)之間的型別不匹配。- 您為表使用了錯誤的表結構
project_id。
您可以在下面看到一個作業示例。
假設您有以下表格:
CREATE TABLE test_project
(
proj_id bigint,
version int,
title VARCHAR(50),
PRIMARY KEY (proj_id, version)
);
create table table_identifier (
table_name varchar(255) not null,
product_id bigint,
primary key (table_name)
);
insert into table_identifier values ('test_project', 20);
和以下映射:
@Entity
@Table(name = "test_project")
@IdClass(ProjectPk.class)
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "nextProjectId")
@TableGenerator(
name="nextProjectId",
table="table_identifier",
pkColumnName = "table_name",
valueColumnName="product_id",
allocationSize = 5
)
@Column(name = "proj_id")
private Long projId;
@Id
private int version;
// other fields, getters, setters ...
}
您將能夠像下面這樣持久化物體:
Project project = new Project();
project.setVersion(1);
// ...
entityManager.persist(project);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/525131.html
上一篇:找到預系結的JDBC連接!如果被告知管理DataSourc,JpaTransactionManager不支持在DataSourceTransactionManager中運行
下一篇:具有嵌套關聯的介面投影
