본문으로 바로가기

 

javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: Unknown integral data type for ids : java.lang.String

 

#원인

 

@GeneratedValue(strategy = GenerationType.SEQUENCE,
    generator = "USER_SEQ_GENERATOR")

 

원래 Oracle에서 생성한 SEQUENCE로 PK값을 생성하려고 했는데

알고보니 ID 필드를 String으로 설정해놓아서 데이터타입 오류가 발생했다.

 

Oracle의 SEQUENCE는 폐기하고 직접 String으로 저장할 수 있는 방법을 찾아보았다.

 

#해결

 

@GeneratedValue(generator = "USER_GENERATOR")
@GenericGenerator(name = "USER_GENERATOR", strategy = "uuid")

 

@GeneratedValue 와 @GenericGenerator 조합을 사용하면

Hibernate에서 자동으로 임의의 uuid 값을 생성해준다.

 

 

** UUID란

A universally unique identifier (UUID) is a 128-bit label used for information in computer systems. The term globally unique identifier (GUID) is also used, often in software created by Microsoft.

 

UUID는 Universally Unique IDentifier 의 약자로, 컴퓨터 시스템의 정보에 사용되는 128bit 레이블이다.

Microsoft에서는 종종 GUID를 사용한다. (UUID랑 유사한 개념인 듯)

When generated according to the standard methods, UUIDs are, for practical purposes, unique. Their uniqueness does not depend on a central registration authority or coordination between the parties generating them, unlike most other numbering schemes. While the probability that a UUID will be duplicated is not zero, it is close enough to zero to be negligible.

 

UUID는 따로 중앙 등록 기관이 있는 것이 아니고, 만든 사람들끼리 겹치지 않게 조정(?) 하는것도 아니다.

물론, UUID가 중복될 확률이 완전히 0%는 아니지만, 매우매우 작아서 무시할만 하다.

 

설명을 읽어보니 중복될 확률이 거의 없어서 여러 데이터를 단일 데이터베이스로 결합했을 시에도 PK 중복문제가 생기지 않는다는 것 같다.

 


#참고자료

 

https://docs.jboss.org/hibernate/annotations/3.4/api/org/hibernate/annotations/GenericGenerator.html

 

GenericGenerator (Hibernate Annotations API Documentation)

 String strategy           Generator strategy either a predefined Hibernate strategy or a fully qualified class name.

docs.jboss.org

https://en.wikipedia.org/wiki/Universally_unique_identifier

 

Universally unique identifier - Wikipedia

From Wikipedia, the free encyclopedia Jump to navigation Jump to search 128-bit label used to identify information in computer systems A universally unique identifier (UUID) is a 128-bit label used for information in computer systems. The term globally uni

en.wikipedia.org

 

반응형