Do I need elements in persistence.xml?

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

I have very simple persistance.xml file:




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

    It's not a solution but a hint for those using Spring:

    I tried to use org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean with setting persistenceXmlLocation but with this I had to provide the <class> elements (even if the persistenceXmlLocation just pointed to META-INF/persistence.xml).

    When not using persistenceXmlLocation I could omit these <class> elements.

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

    You can provide for jar-file element path to a folder with compiled classes. For example I added something like that when I prepared persistence.xml to some integration tests:

     <jar-file>file:../target/classes</jar-file>
    
    0 讨论(0)
  • 2020-11-22 16:42

    for JPA 2+ this does the trick

     <jar-file></jar-file>
    

    scan all jars in war for annotated @Entity classes

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

    Do I need Class elements in persistence.xml?

    No, you don't necessarily. Here is how you do it in Eclipse (Kepler tested):

    Right click on the project, click Properties, select JPA, in the Persistence class management tick Discover annotated classes automatically.

    enter image description here

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

    The persistence.xml has a jar-file that you can use. From the Java EE 5 tutorial:

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

    This file defines a persistence unit named OrderManagement, which uses a JTA-aware data source jdbc/MyOrderDB. The jar-file and class elements specify managed persistence classes: entity classes, embeddable classes, and mapped superclasses. The jar-file element specifies JAR files that are visible to the packaged persistence unit that contain managed persistence classes, while the class element explicitly names managed persistence classes.

    In the case of Hibernate, have a look at the Chapter2. Setup and configuration too for more details.

    EDIT: Actually, If you don't mind not being spec compliant, Hibernate supports auto-detection even in Java SE. To do so, add the hibernate.archive.autodetection property:

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

    Hibernate doesn't support <exclude-unlisted-classes>false</exclude-unlisted-classes> under SE, (another poster mentioned this works with TopLink and EclipseLink).

    There are tools that will auto-generate the list of classes to persistence.xml e.g. the Import Database Schema wizard in IntelliJ. Once you've got your project's initial classes in persistence.xml it should be simple to add/remove single classes by hand as your project progresses.

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