使用Spring Data JPA创建表时SQL语句中出现警告,插入表时出现错误

5m1hhzi4  于 2023-10-20  发布在  Spring
关注(0)|答案(1)|浏览(155)

我使用Spring Data JPA创建了一个表,所以这意味着我没有输入任何SQL代码。现在我得到了一个关于SQL语句中语法错误的警告,如下所示:

Syntax error in SQL statement "create table [*]user (id bigint not null, city varchar(255), fullname varchar(255), password varchar(255), phone_number varchar(255), state varchar(255), street varchar(255), username varchar(255), zip varchar(255), primary key (id))"; expected "identifier";

这是我的“用户”域的一部分,用于获取用户信息

@Entity
@Data
@NoArgsConstructor(access = AccessLevel.PRIVATE, force = true)
@RequiredArgsConstructor
public class User implements UserDetails{

    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    private final String username;
    private final String password;
    private final String fullname;
    private final String street;
    private final String city;
    private final String state;
    private final String zip;
    private final String phoneNumber;

其他方法无关紧要,因为它们只是覆盖UserDetails接口的布尔方法,只返回“true”
现在我得到了一个错误的异常Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException:。当我运行并导航到web上的/register页面时,我填写用户详细信息并提交,以便将其插入UserRepository:

import org.springframework.data.repository.CrudRepository;
import tacos1.User;

public interface UserRepository extends CrudRepository<User, Long> {

    User findByUsername(String username);
}

我在网上看到这个错误:

There was an unexpected error (type=Internal Server Error, status=500).
could not prepare statement; SQL [insert into user (city, fullname, password, phone_number, state, street, username, zip, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?)];

原因:

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "insert into [*]user (city, fullname, password, phone_number, state, street, username, zip, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?)"; expected "identifier";
9lowa7mx

9lowa7mx1#

如果您的应用程序使用H2数据库,“User”是一个保留关键字,可以在文档中看到。
一个快速解决方法是将实体名称重命名为“Users”,或者将NON_KEYWORDS=user添加到JDBC URL。

spring.datasource.url=jdbc:h2:mem:mydb;NON_KEYWORDS=user

相关问题