我正在使用Spring Data JPA 開發Spring Boot 應用程式。作為資料庫,我使用 PostgreSQL,我發現將表的自動增量主鍵欄位映射到物體類的相關欄位時遇到了一些問題。
在我的資料庫中,我手動創建了這個表:
CREATE TABLE IF NOT EXISTS public."user"
(
id bigint NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1 ),
first_name character varying(50) COLLATE pg_catalog."default" NOT NULL,
middle_name character varying(50) COLLATE pg_catalog."default" NOT NULL,
surname character varying(50) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT user_pkey PRIMARY KEY (id)
)
請不要將id欄位(我的 PK)定義為具有GENERATED ALWAYS約束的bigint而不是串行(這是因為串行資料型別已被棄用,因為它不是 SQL 標準的一部分)。
然后我創建了這個物體類映射這個表:
package com.easydefi.users.entity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
@Entity
@Table(name = "portal_user")
@Data
public class User implements Serializable {
private static final long serialVersionUID = 5062673109048808267L;
@Id
@Column(name = "id")
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "middle_name")
private String middleName;
@Column(name = "surname")
private String surname;
public User(String firstName, String middleName, String surname, char sex, Date birthdate, String taxCode,
String eMail, String contactNumber, Date createdAt) {
super();
this.firstName = firstName;
this.middleName = middleName;
this.surname = surname;
}
}
Then I have this repository interface (at the moment it is empty because I am testing only the save() method directly provided by JpaRepository):
public interface UsersRepository extends JpaRepository<User, Integer> {
}
Finnally I created this simple unit test method in order to test the insert of a new record via the save() method of my repository:
@SpringBootTest()
@ContextConfiguration(classes = GetUserWsApplication.class)
@TestMethodOrder(OrderAnnotation.class)
public class UserRepositoryTest {
@Autowired
private UsersRepository userRepository;
@Test
@Order(1)
public void testInsertUser() {
User user = new User("Mario", null, "Rossi", 'M', new Date(), "XXX", "[email protected]", "329123456", new Date());
userRepository.save(user);
assertTrue(true);
}
}
the problem is that when the save() method is performed I am obtaining this exception:
Hibernate:
insert
into
portal_user
(first_name, middle_name, surname, id)
values
(?, ?, ?, ?)
2021-11-04 12:17:39.576 WARN 11436 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 428C9
2021-11-04 12:17:39.578 ERROR 11436 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: cannot insert a non-DEFAULT value into column "id"
Detail: Column "id" is an identity column defined as GENERATED ALWAYS.
Hint: Use OVERRIDING SYSTEM VALUE to override.
So I try to modify the mapping of the id field into my entity class, in this way:
@Id
@Column(name = "id")
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
But running my test method now the save() method execution give me this other error:
Hibernate:
select
nextval ('hibernate_sequence')
2021-11-04 12:20:21.133 WARN 11639 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 42P01
2021-11-04 12:20:21.136 ERROR 11639 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: relation "hibernate_sequence" does not exist
Position: 17
What is the problem? What am I missing? How can I try to fix this issue?
uj5u.com熱心網友回復:
改變
GeneratedValue(strategy=GenerationType.AUTO)
到
@GeneratedValue(strategy=GenerationType.IDENTITY)
由于GenerationType.IDENTITY創建primary key與auto increment和創建hibernate_sequence資料庫中的表自動
uj5u.com熱心網友回復:
錯誤肯定在這一行。
id bigint NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1 ),
如果您想快速修復,可以將其更改為下面的一個。
SERIAL在 Postgres 中作業得很好。當然,它可能不會在其他資料庫上運行,但是 IT 專案多久進行一次資料庫遷移?絕不。
id serial PRIMARY KEY NOT NULL,
這能解決您的問題嗎?或者您出于某種原因需要在沒有 SERIAL 的情況下解決它?
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/347721.html
標籤:java spring-boot hibernate spring-data-jpa hibernate-mapping
上一篇:在棋盤上移動N個國王
