JPA 엔티티 매핑 확장
문제
기존 JPA 매핑을 확장하는 쉬운 방법을 찾고 있습니다. 아이디어는 다음과 같습니다.
EJB3+JPA 모듈이있는 EAR 프로젝트가 있으며 Classa 주석이 있고 테이블 class_a에 매핑되었습니다. 그러나 다른 모듈 (다른 EJB 모듈)이 classa에 더 많은 속성을 추가하는 classB를 갖기를 원합니다 (확장?).
그래도 한 가지 방법은 추가 필드를 클래스_A 테이블에 추가하고 해당 데이터를 검색하기 위해 비 HQL 쿼리를 실행하는 것입니다. 손으로 많은 일을해야하기 때문에 좋지 않습니다 : 유형 매핑, 열 매핑 등.
간단한 점검을 수행했지만 두 번째 모듈에서 Classa를 확장 할 수없는 것 같습니다 (Classb 별)은 다른 EntityManagerFactories를 사용하고 첫 번째 모듈의 일부 클래스는 두 번째 모듈에서 볼 수없고 그 반대도 마찬가지입니다.
나는 이미 보았다u003Cjar-file> Inside Inside Insistence.xml 태그. 그런 것이 필요하지만, 그것을 사용하려면 첫 번째 모듈에 해당 파일이 나열되어 있어야하며 존재해야합니다 (찾을 수없는 경우 건너 뛰지 않음). 확장 모듈 (두 번째 모듈)에 넣을 수 있고 확장 가능한 모듈 (첫 번째)이 아닌 모듈에 넣을 수 있습니까?
런타임에 JPA 매핑을 확장하는 방법이 있다면 좋을 것입니다. 그런 방법이 있습니까? 내 문제에 대한 또 다른 해결책이 있습니까?
해결책
구현 된 솔루션은 다음과 같습니다. 항아리 하나와 두 개의 EJB 모듈이 있습니다.
항아리는 기본입니다. 확장에 대한 기본 엔티티 및 로컬 인터페이스를 보유합니다.
@Entity public class BaseEntity { public long id; @Id @GeneratedValue public long getId() {... ... other variables, getters, setters ... } @Local public interface EntitiyManagerWithExtendedEntitiesInterface { public EntityManager getEntityManager; }
첫 번째 EJB 모듈은 기본 엔티티를 확장하고 EJB를 추가하여 엔티티 관리자를 얻는 모듈입니다. 이 모듈에는 또한 포함됩니다
persistence.xml
~와 함께<jar-file>../path_to_first_jar_file.jar</jar-file>
선.@Entity ... discriminator annotations public class ExtEntity extends BaseEntity { ... additional fields here } @Stateless public class EntitiyManagerWithExtendedEntitiesBean implements EntitiyManagerWithExtendedEntitiesInterface { @PersitenceContext EntityManager em; public EntityManager getEntityManager() { return em; } }
두 번째 EJB 모듈에는 컴파일을 위해 항아리 만 필요하지만 첫 번째 EJB가 실행 해야하는 EJB가 있습니다 (구현할 EJB가 필요합니다.
EntitiyManagerWithExtendedEntitiesInterface
상호 작용).@Stateless public class getSomeEntity { @EJB EntitiyManagerWithExtendedEntitiesInterface ext; EntityManager em; @PostConstruct public void injectEntityManager() { em = ext.getEntityManager(); } public void ejbInterfaceMethod() { ... method that uses em variable (EntityManager) } }
이런 식으로 응용 프로그램 서버는 모듈 간의 종속성을 관리해야하며 1st EJB 모듈을 쉽게 교체하여 다른 확장 엔티티 세트를 포함시킬 수 있습니다.