Database&Jpa

Mysql GenerationType.AUTO 에러

향각산 2023. 11. 9. 20:53

MySQL에 테이블을 만들고 auto increment를 설정하고 jpa를 연결하려고 했는데

다음과 같은 오류가 발생함.

나는 시퀀스를 쓴적도 없고 mysql은 시퀀스가 없다고 들었는데 대체 저 hibernate쿼리는 무엇일까 찾아봄.

 

2023-11-08T13:32:38.164+09:00  INFO 19496 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
Hibernate: 
    select
        next_val as id_val 
    from
        users_seq for update
2023-11-08T13:32:40.045+09:00 ERROR 19496 --- [nio-8080-exec-1] o.hibernate.id.enhanced.TableStructure   : could not read a hi value

java.sql.SQLSyntaxErrorException: Table 'test.users_seq' doesn't exist
  at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:121) ~[mysql-connector-j-8.2.0.jar:8.2.0]
	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-j-8.2.0.jar:8.2.0]
	at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:912) ~[mysql-connector-j-8.2.0.jar:8.2.0]
	at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:968) ~[mysql-connector-j-8.2.0.jar:8.2.0]

 

@Entity(name = "uesrs")
@Getter
public class Users {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
}

 

해당 코드가 h2로 연결해서 테스트할땐 문제가 없었으나, mysql로 변경하니까 문제가 발생했다.

GenerationType.AUTO를 설정하면 hibernate가 각각 연결된 DB 기본설정에 맞춰서 전략을 찾는다고 돼있었으나 로그는 그렇지 않다.

 

 

구글링해본 결과 hibernate 6.0 이상부터는 AUTO로 입력하는 경우 기본적으로 시퀀스 방식으로 지정하는걸로 보인다.

http://(https://discourse.hibernate.org/t/about-generationtype-auto-sequence-in-mysql/7717)

 

그렇다면 방법은 다음처럼 전략을 변경해줘야 함.

@Entity(name = "uesrs")
@Getter
public class Users {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
}

'Database&Jpa' 카테고리의 다른 글

[DB] EntityManager Multirow Insert  (0) 2024.07.30
[DB] WITH AS 구문 사용하기  (0) 2024.07.25
Mongodb 다운로드  (0) 2022.08.02
expected "identifier"; SQL statement:  (0) 2021.12.28
[MariaDB] 한글 깨짐  (0) 2020.09.11
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함