Java/JPA with Error

[해결 방법] org.hibernate.MappingException

HJ0216 2023. 8. 5. 18:06

👉 기본 환경

- Language: Java

- DB: H2 Database

- IDE: IntelliJ

 

 

⌨️ 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
@Entity
@SequenceGenerator(
       name = "MEMBER_SEQ_GENERATOR",
       sequenceName = "MEMBER_SEQ",
       allocationSize = 3
)
public class Member {
 
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "MEMBER_SEQ_GENERATOR")
    private Long id;
}
 
 
 

 

 

🖨️오류

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
Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: hello] Unable to build Hibernate SessionFactory
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1326)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1252)
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:56)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    at jpa_basic.Main.main(Main.java:13)
Caused by:      [entity-name=jpa_basic.Member]
    at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:124)
    at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:344)
    at org.hibernate.internal.SessionFactoryImpl.lambda$new$1(SessionFactoryImpl.java:286)
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
    at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:177)
    at java.base/java.util.HashMap$ValueSpliterator.forEachRemaining(HashMap.java:1692)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
    at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:285)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:468)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1249)
    ... 4 more
Caused by: org.hibernate.MappingException: The increment size of the [MEMBER_SEQ] sequence is set to [3] in the entity mapping while the associated database sequence increment size is [5].
    at org.hibernate.id.enhanced.SequenceStyleGenerator.configure(SequenceStyleGenerator.java:261)
    at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:118)
 
 
 

 

 

📡 원인

SequenceGenerator에서 allocationSize를 5로 설정한 후, 3으로 수정

 

엔티티 매핑에서 MEMBER_SEQ 시퀀스의 증가 크기를 3으로 설정했지만,

실제 데이터베이스의 MEMBER_SEQ 시퀀스는 증가 크기가 5로 설정되어 있음

 

JPA DDL 생성 기능에서 value를 create로 설정하더라도 table처럼 Sequence 객체도 drop 후 새롭게 생성되지 않음

 

 

📰 해결 방법

1
2
DROP SEQUENCE IF EXISTS sequence_name;
 
 
 

DB에서 직접 drop 후 애플리케이션을 새롭게 실행해야 함