Spring Boot JPA未知列在字段列表中

时间:2018-05-28 12:56:47

标签: java spring spring-boot

在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;
    }

在Repository包中

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?

3 个答案:

答案 0 :(得分:3)

&#34;这意味着我应该将MySQL表中的列名更改为account_id&#34;

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

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

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

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

答案 1 :(得分:0)

对于Hibernate5和Spring boot 1.5或更高版本,如果即使使用 PhysicalNamingStrategyStandardImpl 作为命名策略后仍然仍然出现“未知列”错误,请尝试对字段的getter方法(而不是属性)使用@Column映射级别@column映射。

答案 2 :(得分:0)

使用@Column(name="accountid", nullable = false, unique = true) Mysql中的列不区分大小写。如果您在@Column中使用区分大小写的列名,则它将驼峰大小写转换为蛇形大小写。