내가 필요하다 persistence.xml의 요소?
매우 간단한 persistance.xml 파일이 있습니다.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="eventractor" transaction-type="RESOURCE_LOCAL">
<class>pl.michalmech.eventractor.domain.User</class>
<class>pl.michalmech.eventractor.domain.Address</class>
<class>pl.michalmech.eventractor.domain.City</class>
<class>pl.michalmech.eventractor.domain.Country</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
그리고 작동합니다.
모든 <class>요소를 제거하면 응용 프로그램에 모든 클래스가 표시되지 않습니다 @Entity.
@Entity클래스 를 스캔하는 자동 발현이 ?
persistence.xml에는 jar-file사용할 수 있습니다. 에서 자바 EE 5 튜토리얼 :
<persistence> <persistence-unit name="OrderManagement"> <description>This unit manages orders and customers. It does not rely on any vendor-specific features and can therefore be deployed to any persistence provider. </description> <jta-data-source>jdbc/MyOrderDB</jta-data-source> <jar-file>MyOrderApp.jar</jar-file> <class>com.widgets.Order</class> <class>com.widgets.Customer</class> </persistence-unit> </persistence>
이 파일은 OrderManagementJTA 인식 데이터 소스를 사용하는 라는 지속성 단위를 정의합니다 jdbc/MyOrderDB. jar-file및 class엔티티 클래스 임베드 클래스 및 매핑 수퍼 : 요소 영속성 관리 클래스를 지정한다. jar-file그동안 클래스 영속성 관리 포함하는 패키지 퍼시스턴스 유닛에게 표시 소자 지정 JAR 파일 class소자 명시 클래스 이름은 영속성 관리.
의 경우 최대 절전 모드 장를 살펴보십시오 . 자세한 내용은 설정 및 구성 도 참조하십시오.
편집 : 실제로 사양을 준수하지 않아도 괜찮다면 Hibernate는 Java SE에서 자동 감지를 지원합니다. 이렇게 비용 hibernate.archive.autodetection속성을 추가합니다 .
<persistence-unit name="eventractor" transaction-type="RESOURCE_LOCAL">
<!-- This is required to be spec compliant, Hibernate however supports
auto-detection even in JSE.
<class>pl.michalmech.eventractor.domain.User</class>
<class>pl.michalmech.eventractor.domain.Address</class>
<class>pl.michalmech.eventractor.domain.City</class>
<class>pl.michalmech.eventractor.domain.Country</class>
-->
<properties>
<!-- Scan for annotated classes and Hibernate mapping XML files -->
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
에서 자바 SE 환경, 사양에 의해 모든 클래스를 지정해야 당신이했던 것처럼 :
모든 명명 된 관리 지속성 클래스 목록은 이식성을 보장하기 위해 Java SE 환경에서 지정되어야합니다.
과
지속성 단위의 루트에 포함 된 어노테이션이있는 지속성 클래스가 지속성 단위에 포함되지 않은 경우 제외-비 목록 클래스 요소를 포함하지 않습니다. exclude-unlisted-classes 요소는 Java SE 환경에서 사용하기위한 것이 아닙니다.
(JSR-000220 6.2.1.6)
에서 자바 EE 환경, 당신은 사용하지 않는 작업을 수행 할 수 있습니다.
비공식적으로 <exclude-unlisted-classes>false</exclude-unlisted-classes>persistence.xml에서 설정 을 시도 할 수 있습니다 . 이 매개 변수의 주소은 falseEE 및 trueSE입니다. EclipseLink 와 Toplink 모두 내가 말할 수있는 한 담당자가 지원합니다. 그러나 SE에서 작동하는 것들에 따라 언급 된 것들.
다음을 시도 할 수 있습니다 (SE 환경에서 작동 할 수 있음).
<persistence-unit name="eventractor" transaction-type="RESOURCE_LOCAL">
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
persistence.xml에 Class 요소가 필요하고 있습니까?
아니요, 반드시 그런 것은 아닙니다. 다음은 Eclipse에서 수행하는 방법입니다 (Kepler 테스트 됨).
프로젝트를 마우스 오른쪽 버튼으로 클릭하고 속성을 클릭 한 다음 JPA를 선택 하고 지속성 클래스 관리 틱에서 자동으로 어노테이션이있는 클래스 발견을 선택하십시오 .
Spring에서 JPA를 실행하는 경우 3.1 버전부터 packagesToScan속성을 설정 LocalContainerEntityManagerFactoryBean하고 persistence.xml을 완전히 제거 할 수 있습니다 .
JPA 2+의 경우이 트릭을 수행합니다.
<jar-file></jar-file>
주석이 달린 @Entity 클래스에 대해 전쟁의 모든 항아리를 스캔하십시오.
Hibernate는 <exclude-unlisted-classes>false</exclude-unlisted-classes>SE에서 지원하지 않습니다 (다른 포스터 가 TopLink 및 EclipseLink에서 작동했습니다).
IntelliJ의 Import Database Schema 마법사와 같이 persistence.xml에 클래스 목록을 자동 생성하는 도구가 있습니다. persistence.xml에 프로젝트의 초기 클래스가 있으면 프로젝트가 진행됨에 따라 손으로 단일 클래스를 추가 / 제거하는 것이 간단해야합니다.
jar-file컴파일 된 클래스가있는 폴더에 대한 요소 경로를 제공 할 수 있습니다 . 예를 들어 몇 가지 통합 테스트에 persistence.xml을 준비 할 때 다음과 같은 내용을 추가했습니다.
<jar-file>file:../target/classes</jar-file>
내가하고있는 것과 비슷한 일을하고 있는지 확실하지 않지만 Maven을 사용하여 별도의 구성 요소에서 JAXB를 사용하여 XSD에서 소스 Java로드를 생성합니다. 이 아티팩트를 "기본 모델"이라고 가정 해 보겠습니다.
Java 소스를 포함하는이 아티팩트를 가져 와서 "기본 모델"아티팩트 jar의 모든 클래스에 대해 최대 절전 모드를 실행하고 각각을 명시 적으로 지정하지 않으려 고했습니다. 내 최대 절전 모드 구성 요소에 대한 종속성으로 "base-model"을 추가했지만 문제는 persistence.xml의 태그가 절대 경로를 지정할 수만 있다는 것입니다.
내가 얻은 방법은 내 "기본 모델"jar 종속성을 내 대상 디렉토리에 명시 적으로 복사하고 버전을 제거하는 것입니다. 따라서 "base-model"아티팩트를 빌드하면 "base-model-1.0-SNAPSHOT.jar"이 생성되지만 복사 리소스 단계에서는 "base-model.jar"로 복사합니다.
따라서 최대 절전 구성 요소에 대한 pom에서 :
<!-- We want to copy across all our artifacts containing java code
generated from our scheams. We copy them across and strip the version
so that our persistence.xml can reference them directly in the tag
<jar-file>target/dependency/${artifactId}.jar</jar-file> -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>process-resources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
</execution>
</executions>
<configuration>
<includeArtifactIds>base-model</includeArtifactIds>
<stripVersion>true</stripVersion>
</configuration>
</plugin>
그런 다음 다음 단계에서 "process-classes"에서 hibernate 플러그인을 호출합니다.
<!-- Generate the schema DDL -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>generate-ddl</id>
<phase>process-classes</phase>
<goals>
<goal>hbm2ddl</goal>
</goals>
</execution>
</executions>
<configuration>
<components>
<component>
<name>hbm2java</name>
<implementation>annotationconfiguration</implementation>
<outputDirectory>/src/main/java</outputDirectory>
</component>
</components>
<componentProperties>
<persistenceunit>mysql</persistenceunit>
<implementation>jpaconfiguration</implementation>
<create>true</create>
<export>false</export>
<drop>true</drop>
<outputfilename>mysql-schema.sql</outputfilename>
</componentProperties>
</configuration>
</plugin>
마지막으로 persistence.xml에서 jar의 위치를 명시 적으로 설정할 수 있습니다.
<jar-file>target/dependency/base-model.jar</jar-file>
속성을 추가합니다.
<property name="hibernate.archive.autodetection" value="class, hbm"/>
해결책은 아니지만 Spring을 사용하는 사람들을위한 힌트입니다.
나는 org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean설정과 함께 사용하려고 persistenceXmlLocation했지만 이것으로 <class>요소 를 제공 해야했습니다 ( persistenceXmlLocation방금을 가리 키 더라도 META-INF/persistence.xml).
사용 하지 않을 때는 persistenceXmlLocation이러한 <class>요소를 생략 할 수 있습니다.
이 솔루션이 사양에 해당하는지 확실하지 않지만 다른 사람들과 공유 할 수 있다고 생각합니다.
종속성 트리
my-entities.jar
엔티티 클래스 만 포함합니다. 아니 META-INF/persistence.xml.
my-services.jar
에 따라 다릅니다 my-entities. EJB 만 포함합니다.
my-resources.jar
에 따라 다릅니다 my-services. 리소스 클래스 및 META-INF/persistence.xml.
문제
<jar-file/>요소를my-resources임시 종속성의 버전 후위 아티팩트 이름으로 지정하려면 어떻게해야 합니까?<jar-file/>요소의 값과 실제 일시적 종속성의 값을 어떻게 동기화 할 수 있습니까?
해결책
직접 (중복?) 종속성 및 리소스 필터링
속성과 종속성을 my-resources/pom.xml.
<properties>
<my-entities.version>x.y.z-SNAPSHOT</my-entities.version>
</properties>
<dependencies>
<dependency>
<!-- this is actually a transitive dependency -->
<groupId>...</groupId>
<artifactId>my-entities</artifactId>
<version>${my-entities.version}</version>
<scope>compile</scope> <!-- other values won't work -->
</dependency>
<dependency>
<groupId>...</groupId>
<artifactId>my-services</artifactId>
<version>some.very.sepecific</version>
<scope>compile</scope>
</dependency>
<dependencies>
이제 persistence.xml필터링 할 준비를하십시오
<?xml version="1.0" encoding="UTF-8"?>
<persistence ...>
<persistence-unit name="myPU" transaction-type="JTA">
...
<jar-file>lib/my-entities-${my-entities.version}.jar</jar-file>
...
</persistence-unit>
</persistence>
Maven Enforcer 플러그인
으로 dependencyConvergence규칙, 우리는 것을 말씀 드릴 수 있습니다. my-entities'버전은 직접 및 전이 모두 동일합니다.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>enforce</id>
<configuration>
<rules>
<dependencyConvergence/>
</rules>
</configuration>
<goals>
<goal>enforce</goal>
</goals>
</execution>
</executions>
</plugin>
모든 경우에 반드시 그런 것은 아닙니다.
Jboss 7.0.8 및 Eclipselink 2.7.0을 사용하고 있습니다. 제 경우에는 persistence.xml에 동일한 항목을 추가하지 않고 추가하지 않고로드하기 위해 Jboss Standalone XML에 다음 시스템 속성을 추가했습니다.
<property name="eclipselink.archive.factory" value="org.jipijapa.eclipselink.JBossArchiveFactoryImpl"/>
참고 URL : https://stackoverflow.com/questions/1780341/do-i-need-class-elements-in-persistence-xml
'IT' 카테고리의 다른 글
| Visual Studio에서 Eclipse의 Ctrl + 클릭? (0) | 2020.08.10 |
|---|---|
| 자바 펼쳐 AES 암호화 (0) | 2020.08.10 |
| Python에서 다른 선 스타일로 주 및 부 격자 선을 만드는 방법 (0) | 2020.08.10 |
| 모든 약속이 해결 될 때까지 기다리십시오. (0) | 2020.08.10 |
| C ++ : 어떤 정규식 라이브러리를 사용합니까? (0) | 2020.08.10 |