首页 > 解决方案 > 字段列表中的 Spring Boot JPA 未知列

问题描述

在 MySQL 我有创建表的脚本

create table account (
AccountId int not null auto_increment,
Login varchar(31),
Password varchar(31),
primary key (AccountId)
);

在java类中我有这个表的模型

@Entity
@Table(name = "account")
public class Account {

    @Column(name = "AccountId", nullable = false, unique = true)
    private Integer AccountId;
    private String  Login;
    private String Password;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getAccountId() {
        return AccountId;
    }

在存储库包中

public interface AccountRepository extends JpaRepository<Account, Integer> {
    Account findAccountByLoginAndPassword(String login, String password);
}

在客户端站点中,我尝试发送请求登录名和密码,但在服务器站点中出现错误

2018-05-28 14:46:15.464  INFO 20300 --- [nio-8080-exec-2] o.h.h.i.QueryTranslatorFactoryInitiator  : HHH000397: Using ASTQueryTranslatorFactory
Hibernate: select account0_.account_id as account_1_0_, account0_.login as login2_0_, account0_.password as password3_0_ from account account0_ where account0_.login=? and account0_.password=?

`ERROR 20300 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : Unknown column 'account0_.account_id' in 'field list`

' 这意味着我应该将 MySQL 表中的列名更改为 account_id?

标签: javaspringspring-boot

解决方案


“这意味着我应该将 MySQL 表中的列名更改为 account_id”

是的,由于命名约定,这是一个好主意,但您也可以配置适当的命名策略:

spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy

或者您可以尝试以小写形式打印列名。(如果您的 MySQL 是基于 Windows 的)

@Column(name="accountid", nullable = false, unique = true)

推荐阅读