Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot JPA unknown column in field list

In MySQL I have script that create table

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

In java class i have model to this table

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

In Repository package

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

In client site i try send in request login and password but i have error in server site

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`

' That mean I should change my column name in MySQL table to account_id?

like image 832
Walter White Avatar asked May 28 '18 12:05

Walter White


2 Answers

Use @Column(name="accountid", nullable = false, unique = true) Columns in Mysql are case insensitive. If you use case sensitive column name in @Column then it will convert camel case to snake case.

like image 29
Mahesh Bhuva Avatar answered Nov 05 '22 20:11

Mahesh Bhuva


"That means I should change my column name in MySQL table to account_id"

Yep, this is a good idea because of naming convention, but also you can configure proper naming strategy:

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

Or you can try to print your column name in lower case. (If your MySQL is Windows based)

@Column(name="accountid", nullable = false, unique = true)
like image 69
Vasily Komarov Avatar answered Nov 05 '22 20:11

Vasily Komarov