Do I need elements in persistence.xml?

后端 未结 11 1674
滥情空心
滥情空心 2020-11-22 16:12

I have very simple persistance.xml file:




        
相关标签:
11条回答
  • 2020-11-22 16:51

    Not necessarily in all cases.

    I m using Jboss 7.0.8 and Eclipselink 2.7.0. In my case to load entities without adding the same in persistence.xml, I added the following system property in Jboss Standalone XML:

    <property name="eclipselink.archive.factory" value="org.jipijapa.eclipselink.JBossArchiveFactoryImpl"/>

    0 讨论(0)
  • 2020-11-22 16:54

    I'm not sure this solution is under the spec but I think I can share for others.

    dependency tree

    my-entities.jar

    Contains entity classes only. No META-INF/persistence.xml.

    my-services.jar

    Depends on my-entities. Contains EJBs only.

    my-resources.jar

    Depends on my-services. Contains resource classes and META-INF/persistence.xml.

    problems

    • How can we specify <jar-file/> element in my-resources as the version-postfixed artifact name of a transient dependency?
    • How can we sync the <jar-file/> element's value and the actual transient dependency's one?

    solution

    direct (redundant?) dependency and resource filtering

    I put a property and a dependency in 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>
    

    Now get the persistence.xml ready for being filtered

    <?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 Plugin

    With the dependencyConvergence rule, we can assure that the my-entities' version is same in both direct and transitive.

    <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>
    
    0 讨论(0)
  • 2020-11-22 16:55

    Not sure if you're doing something similar to what I am doing, but Im generating a load of source java from an XSD using JAXB in a seperate component using Maven. Lets say this artifact is called "base-model"

    I wanted to import this artifact containing the java source and run hibernate over all classes in my "base-model" artifact jar and not specify each explicitly. Im adding "base-model" as a dependency for my hibernate component but the trouble is the tag in persistence.xml only allows you to specify absolute paths.

    The way I got round it is to copy my "base-model" jar dependency explictly to my target dir and also strip the version of it. So whereas if I build my "base-model" artifact it generate "base-model-1.0-SNAPSHOT.jar", the copy-resources step copies it as "base-model.jar".

    So in your pom for the hibernate component:

                <!-- 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>
    

    Then I call the hibernate plugin in the next phase "process-classes":

                <!-- 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>
    

    and finally in my persistence.xml I can explicitly set the location of the jar thus:

    <jar-file>target/dependency/base-model.jar</jar-file>
    

    and add the property:

    <property name="hibernate.archive.autodetection" value="class, hbm"/>
    
    0 讨论(0)
  • 2020-11-22 16:59

    In Java SE environment, by specification you have to specify all classes as you have done:

    A list of all named managed persistence classes must be specified in Java SE environments to insure portability

    and

    If it is not intended that the annotated persistence classes contained in the root of the persistence unit be included in the persistence unit, the exclude-unlisted-classes element should be used. The exclude-unlisted-classes element is not intended for use in Java SE environments.

    (JSR-000220 6.2.1.6)

    In Java EE environments, you do not have to do this as the provider scans for annotations for you.

    Unofficially, you can try to set <exclude-unlisted-classes>false</exclude-unlisted-classes> in your persistence.xml. This parameter defaults to false in EE and truein SE. Both EclipseLink and Toplink supports this as far I can tell. But you should not rely on it working in SE, according to spec, as stated above.

    You can TRY the following (may or may not work in SE-environments):

    <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>
    
    0 讨论(0)
  • 2020-11-22 17:01

    For those running JPA in Spring, from version 3.1 onwards, you can set packagesToScan property under LocalContainerEntityManagerFactoryBean and get rid of persistence.xml altogether.

    Here's the low-down

    0 讨论(0)
提交回复
热议问题