我正在使用Spring Data JPA和Hibernate 處理Spring Boot 專案,但我發現嘗試使用JpaRepository介面的save()方法執行簡單的INSERT查詢時遇到了一些困難。
作為我使用的資料庫PostgreSQL,這里我有以下表格:
CREATE TABLE IF NOT EXISTS public."User"
(
id bigint NOT NULL,
first_name character varying(50) COLLATE pg_catalog."default" NOT NULL,
middle_name character varying(50) COLLATE pg_catalog."default",
surname character varying(50) COLLATE pg_catalog."default" NOT NULL,
sex "char" NOT NULL,
birthdate date NOT NULL,
tax_code character varying(255) COLLATE pg_catalog."default" NOT NULL,
e_mail character varying(255) COLLATE pg_catalog."default" NOT NULL,
contact_number character varying(50) COLLATE pg_catalog."default" NOT NULL,
created_at timestamp without time zone NOT NULL,
CONSTRAINT "User_pkey" PRIMARY KEY (id)
)
我已經映射到我的專案中的一個用戶物體類,這個:
package com.easydefi.users.entity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
@Entity
@Table(name = "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;
@Column(name = "sex")
private char sex;
@Column(name = "birthdate")
private Date birthdate;
@Column(name = "tex_code")
private String taxCode;
@Column(name = "e_mail")
private String eMail;
@Column(name = "contact_number")
private String contactNumber;
@Column(name = "created_at")
private Date createdAt;
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;
this.sex = sex;
this.birthdate = birthdate;
this.taxCode = taxCode;
this.eMail = eMail;
this.contactNumber = contactNumber;
this.createdAt = createdAt;
}
}
好的,我創建了這個擴展 JpaRepository介面的UserRepository介面:
public interface UserRepository extends JpaRepository<User, Integer> {
}
At the moment it is empty because I was trying to perform a first test of the save() method.
Finnally I created this simple test class containing a method to test the operation related to the insertion of a new record into the User table, this:
@SpringBootTest()
@ContextConfiguration(classes = GetUserWsApplication.class)
@TestMethodOrder(OrderAnnotation.class)
public class UserRepositoryTest {
@Autowired
private UserRepository 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 performing this method I obtain the following exception\error in my stacktrace:
2021-11-03 12:19:28.563 WARN 8094 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 42601
2021-11-03 12:19:28.567 ERROR 8094 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: syntax error at or near "user"
Position: 13
2021-11-03 12:19:32.058 INFO 8094 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2021-11-03 12:19:32.065 INFO 8094 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2021-11-03 12:19:32.080 INFO 8094 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
The problem is that it speack about a generic syntax error at or near "user" but I can not understand what is the problem.
What is the problem? How can I try to fix it? What am I missing?
uj5u.com熱心網友回復:
在 Postgres 中 user 是一個保留的 SQL 關鍵字。您可以將表的名稱更改為其他單詞,也可以使用“”技巧,但我不推薦這樣做。
uj5u.com熱心網友回復:
“user”是 PostgresSQL 中的保留關鍵字。將表的 User 名稱更改為其他名稱或使用雙引號將其轉義。
@Table(name = "\"User\"")
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/346492.html
標籤:java spring-boot hibernate spring-data-jpa
