programing

POJO를 확장하여 JPA 엔티티를 구축 할 수 있습니까?

firstcheck 2021. 1. 15. 08:16
반응형

POJO를 확장하여 JPA 엔티티를 구축 할 수 있습니까?


다음 POJO가 있다고 가정 해 보겠습니다.

public class MyThing {
 private int myNumber;
 private String myData;
//assume getter/setter methods
}

이제이 POJO를 JPA 엔티티로 확장 할 수 있습니까?

@Entity
@Table(name = "my_thing")
public class MyThingEntity extends MyThing implements Serializable {
 @Column(name = "my_number")
 //?????????
 @Column(name = "my_data")
 //????????
}

POJO를 JPA 엔티티와 별도로 유지하고 싶습니다. POJO는 다른 프로젝트에 있으며 지속성 레이어없이 자주 사용됩니다. 내 프로젝트는 POJO에서 엔티티로의 매핑 오버 헤드없이이를 데이터베이스에 유지하려고합니다.

JPA 엔터티가 POJO라는 것을 이해하지만이를 사용하려면 javax.persistence를 구현하는 라이브러리를 포함해야하며 동일한 기본 개체를 사용하는 다른 프로젝트는 지속성 레이어를 사용하지 않습니다.

이것이 가능한가? 이것이 좋은 생각입니까?


JPA 사양 상태

엔티티 는 엔티티 클래스뿐만 아니라 엔티티가 아닌 클래스를 확장 할 수 있으며, 비 엔티티 클래스는 엔티티 클래스를 확장 할 수 있습니다.

@ javax.persistence.MappedSuperclass 주석을 사용하면 이러한 종류의 매핑을 정의 할 수 있습니다.

@MappedSuperclass
public class MyThing implements Serializable {
    private int myNumber;
    private String myData;

    // getter's and setter's
}

@Entity
@Table(name="MY_THING")
public class MyThingEntity extends MyThing {


}

JPA 사양에서 말했듯이

MappedSuperclass 주석은 매핑 정보가 상속되는 엔티티에 적용되는 클래스 지정 합니다 .

MappedSuperclass 주석으로 지정된 클래스 는 매핑 된 수퍼 클래스 자체에 대한 테이블이 없기 때문에 매핑이 하위 클래스에만 적용된다는 점을 제외하고 엔티티와 동일한 방식으로 매핑 할 수 있습니다.

MyThing에서 정의한 일부 속성을 재정의해야하는 경우 @AttributeOverride (단일 속성을 재정의하려는 경우) 또는 @AttributeOverrides (두 개 이상의 속성을 재정의하려는 경우)를 사용합니다.

@Entity
@Table(name="MY_THING")
@AttributeOverride(name="myData", column=@Column(name="MY_DATA"))
public class MyThingEntity extends MyThing {


}

@Entity
@Table(name="MY_OTHER_THING")
@AttributeOverrides({
    @AttributeOverride(name="myData1", column=@Column(name="MY_DATA_1")),
    @AttributeOverride(name="myData2", column=@Column(name="MY_DATA_2"))
})
public class MyOtherThingEntity extends MyThing {

}

기본 클래스를 변경하지 않으려면 xml을 사용하여 @MappedSuperClass로 정의 할 수 있습니다.

Be aware: by default, the persistence provider will look in the META-INF directory for a file named orm.xml

<?xml version="1.0" encoding="UTF-8"?>

<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd" version="1.0">
    <mapped-superclass class="MyThing">

    </mapped-superclass>
</entity-mappings>

Nothing else. If you want to override a property, use @AttributeOverride as shown above


It is possible:

  • you can map it with XML - make an orm.xml (conforming to the orm schema), and map the columns of your POJO, without even extending it. It will be JPA-enabled in one environment, and a POJO in the other one
  • override just the getter methods and annotate them - (I'm not sure if this will work)

That said, I don't think it is necessary to do this. Just annotate your POJO, and add the compile-time dependency to your projects. Then each project will decide whether it will use them as JPA entities or not.

ReferenceURL : https://stackoverflow.com/questions/2516329/is-it-possible-to-build-a-jpa-entity-by-extending-a-pojo

반응형