개발/SPRING & JPA

Jpa entity primitive vs wrapper

Leedo1982 2021. 3. 4. 12:19

jpa 를 사용하면서 entity 에 변수타입을 primitive 로 해야할지, wrapper 로 해야할지 고민을 가지고 있었다.

그러던 중 하나의 근거를 발견했다.

 

 

4.1.2. Provide an identifier property (optional)
Cat has a property called id. This property maps to the primary key column of a database table. The property might have been called anything, and its type might have been any primitive type, any primitive "wrapper" type, java.lang.String or java.util.Date. If your legacy database table has composite keys, you can use a user-defined class with properties of these types (see the section on composite identifiers later in the chapter.)

The identifier property is strictly optional. You can leave them off and let Hibernate keep track of object identifiers internally. We do not recommend this, however.

In fact, some functionality is available only to classes that declare an identifier property:

Transitive reattachment for detached objects (cascade update or cascade merge) - see Section 10.11, “Transitive persistence”

Session.saveOrUpdate()

Session.merge()

We recommend that you declare consistently-named identifier properties on persistent classes and that you use a nullable (i.e., non-primitive) type.

Chapter 4. Persistent Classes

 

Chapter 4. Persistent Classes

Persistent classes are classes in an application that implement the entities of the business problem (e.g. Customer and Order in an E-commerce application). Not all instances of a persistent class are considered to be in the persistent state. For example,

docs.jboss.org

 

 

결론 : nullable 가 가능한 타입을 제안한다.