본문 바로가기

백엔드 개발/JPA

[하이버네이트 유저 가이드 파헤치기] BasicTypeRegistry & Explicit BasicTypes - 2.3.4, 2.3.5

반응형

2.3.4. BasicTypeRegistry

우리는 이전에 하이버네이트 타입은 자바(JAVA) 타입도 SQL 타입도 아니지만, 하이버네이트 타입은 둘(자바 타입, SQL 타입) 모두를 이해하며 둘 사이의 변환을 수행한다고 언급했었다.

그러나 이전 챕터의 Example 3. 에서 어떻게 하이버네이트는 org.hibernate.type.StringType과  java.lang.String 속성이 연결되며, org.hibernate.type.IntegerType은 어떻게 java.lang.Integer 속성으로 연결되는지 알았을까?

 

정답은 org.hibernate.type.BasicTypeRegistry라고 불리는 하이버네이트 내부의 서비스에 있다. 

org.hibernate.type.BasicTypeRegistry는 필수적으로 org.hibernate.type.BasicType 인스턴스의 이름이 키로 지정된 맵 데이터 구조를 유지한다.

 

우리는 다음 섹션(Explicit BasicTypes)에서 명시적으로 어떤 베이직타입을  특정 속성에 사용할지 하이버네이트에게 알릴 수 있다는 것을 살펴볼 것이다. 그러나 우선, 암시적 해결이 작동하는 방식과 애플리케이션이 암시적으로 해결을 조정할 수 있는 방법을 살펴볼 것이다. 

 

원문
We said before that a Hibernate type is not a Java type, nor an SQL type, but that it understands both and performs the marshalling between them. But looking at the basic type mappings from the previous examples, how did Hibernate know to use its org.hibernate.type.StringType for mapping for java.lang.String attributes, or its org.hibernate.type.IntegerType for mapping java.lang.Integer attributes?

The answer lies in a service inside Hibernate called the org.hibernate.type.BasicTypeRegistry, which essentially maintains a map of org.hibernate.type.BasicType (an org.hibernate.type.Type specialization) instances keyed by a name.

We will see later, in the Explicit BasicTypes section, that we can explicitly tell Hibernate which BasicType to use for a particular attribute. But first, let’s explore how implicit resolution works and how applications can adjust the implicit resolution.

 

이전 챕터의 Example 3. 에서 Product의 sku와 같이 String 속성을 취하는 필드가있다고 했을때, 이 속성들은 명시적으로 타입 매핑을 하지 않았기 때문에, 하이버네이트는 java.lang.String을 위해 등록된 매핑을 찾기위해 BasicTypeRegistry를 탐색한다.

이것은 우리가 봤던 이 장 시작 부분에 있는 테이블"BasicTypeRegistry key(s)" 열을 참조해라.

 

BasicTypeRegistry의 내부 기준선으로, 하이버네이트는 Java 타입에 대한 JDBC의 추천 매핑을 따른다.

JDBC는 StringType을 처리하는 정확한 매핑으로 String을 VARCHAR로 매핑하는 것을 추천한다.

그래서 이 것은 BasicTypeRegistry 내부에서 String 타입들에 대한 기준 매핑이 된다.

 

애플리케이션은 부트스트랩하는 동안 MetadataBuilder#applyBasicType 메소드나 MetadataBuilder#applyTypes  메소드를 사용하여 확장(새로운 BasicType 등록 추가)하거나 재정의(존재하는 BasicType 등록을 대체)할 수 있다. 더 자세한 내용은 Custom BasicTypes 섹션을 참고하라.

원문
As an example, take a String attribute such as we saw before with Product#sku. Since there was no explicit type mapping, Hibernate looks to the BasicTypeRegistry to find the registered mapping for java.lang.String. This goes back to the "BasicTypeRegistry key(s)" column we saw in the tables at the start of this chapter.

As a baseline within BasicTypeRegistry, Hibernate follows the recommended mappings of JDBC for Java types. JDBC recommends mapping Strings to VARCHAR, which is the exact mapping that StringType handles. So that is the baseline mapping within BasicTypeRegistry for Strings.

Applications can also extend (add new BasicType registrations) or override (replace an existing BasicType registration) using one of the MetadataBuilder#applyBasicType methods or the MetadataBuilder#applyTypes method during bootstrap. For more details, see Custom BasicTypes section.

 

반응형

2.3.5. Explicit BasicTypes

간혹 당신은 특정한 속성을 다르게 처리하길 원할 수 있다.

하이버네이트는 종종 암시적으로 당신이 원하지 않는 (그리고 당신이 BasicTypeRegistry를 조정하는 것을 원하지 않는 어떤 이유로 인해 ) BasicType를 선정할 것이다.

 

이런 경우에 당신은 아래 예제 6처럼 org.hibernate.annotations.Type 어노테이션으로 사용할 BasicType을 명시해야한다.

 

Example 6. Using @org.hibernate.annotations.Type

@Entity(name = "Product")
public class Product {

	@Id
	private Integer id;

	private String sku;

	@org.hibernate.annotations.Type( type = "nstring" )
	private String name;

	@org.hibernate.annotations.Type( type = "materialized_nclob" )
	private String description;
}

 

원문
Sometimes you want a particular attribute to be handled differently. Occasionally Hibernate will implicitly pick a BasicType that you do not want (and for some reason you do not want to adjust the BasicTypeRegistry).

In these cases, you must explicitly tell Hibernate the BasicType to use, via the org.hibernate.annotations.Type annotation.

 

 

이것은 하이버네이트에게 String 타입이 nationalized 데이터로 저장되어야함을 알려준다.

이것은 단지 설명을 위한 예시이다. nationalized 데이터를 나타내는 더 나은 방법은  Mapping Nationalized Character Data 를 참조하라.

 

추가적으로, description 속성 역시 LOB 데이터로 처리되어야한다.

역시나 설명을 위한 예시이니 LOB 데이터를 나타내는 더 나은 방법은  Mapping LOBs 를 참조하라.

 

org.hibernate.annotations.Type#type 속성은 아래 중 어떤 것으로도 이름을 지정할 수 있다.

  • org.hibernate.type.Type을 구현한 모든 검증된 이름
  • BasicTypeRegistry에 등록된 모든 키 
  • 모든 알려진 타입 정의 이름

 

원문
This tells Hibernate to store the Strings as nationalized data. This is just for illustration purposes; for better ways to indicate nationalized character data see Mapping Nationalized Character Data section.

Additionally, the description is to be handled as a LOB. Again, for better ways to indicate LOBs see Mapping LOBs section.

The org.hibernate.annotations.Type#type attribute can name any of the following:
  - Fully qualified name of any org.hibernate.type.Type implementation
  - Any key registered with BasicTypeRegistry
  - The name of any known type definitions
반응형