Home > Net >  H2 SQLGrammarException: could not prepare statement
H2 SQLGrammarException: could not prepare statement

Time:03-04

I have a spring boot application with an h2 database that is addressed by a UserRepository. I am trying to use Spring security to authenticate using jdbc(Hibernate here)

@Entity
@Table(name="USER")

public class User {
    
    @Id
    @Column(name="USER_ID")
    private long id;
    
    @Column(name="USERNAME, nullable = false, unique = true")
    private String username;
    
    @Column(name="PASSWORD")
    private String password;
    
    /*setter/getter follows */
}

Repository:-

public interface UserRepository extends JpaRepository<User, Long>{
    User findByUsername(String username);
}

SQL:

CREATE TABLE USER(
    USER_ID BIGINT AUTO_INCREMENT PRIMARY KEY,
    USERNAME VARCHAR(128) NOT NULL UNIQUE,
    PASSWORD VARCHAR(256) NOT NULL
);

INSERT INTO USER (USERNAME,PASSWORD) VALUES('jdoe','foobar');

But when enter correct username and password, it is not accepting it.

Exception:

SQL Error: 42001, SQLState: 42001
2022-03-04 13:43:18.571 ERROR 17924 --- [nio-8080-exec-7] o.h.engine.jdbc.spi.SqlExceptionHelper   : Syntax error in SQL statement "SELECT USER0_.USER_ID AS USER_ID1_0_, USER0_.PASSWORD AS PASSWORD2_0_, USER0_.USERNAME, NULLABLE = FALSE, UNIQUE[*] = TRUE AS USERNAME3_0_ FROM USER USER0_ WHERE USER0_.USERNAME, NULLABLE = FALSE, UNIQUE = TRUE=? "; expected "*, NOT, EXISTS, INTERSECTS, SELECT, FROM, WITH"; SQL statement:
select user0_.user_id as user_id1_0_, user0_.password as password2_0_, user0_.username, nullable = false, unique = true as username3_0_ from user user0_ where user0_.username, nullable = false, unique = true=? [42001-196]

CodePudding user response:

Nice mistake:

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

->

@Column(name="USERNAME", nullable = false, unique = true)
  • Related