简体   繁体   中英

Declaring a persistent-unit without class in JPA

Well, my doubt is very simple but strange for me, because in everywhere i hear: "You must declare all persistent classes in persistent-unit".

I decide don't declare any class in my persistent-unit, just put the default configuration, and my application works fine even now. So, why declare my classes there ?

NOTE: I don't know if this problem is related with the scneario above, but when i try to load a lazy attribute all fields in this object are NULL and have in attribute "handler" something like: JavaLazyInitializer.

EDIT 1:

This is my persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd ">


<persistence-unit name="odontonewPU">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
 <properties>
   <property name="hibernate.show_sql" value="true" />
  <property name="hibernate.format_sql" value="false" />
  <property name="hibernate.hbm2ddl.auto" value="update" />  
 </properties>
</persistence-unit>

</persistence>

This is my applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <!-- Seta anotaçoes para serem usadas pelo Spring -->
    <context:annotation-config />

    <!-- Define o pacote onde o Spring vai procurar por beans anotados -->
    <context:component-scan
        base-package="br.com.odontonew" />

    <!-- define que as transaçoes irao ser anotadas -->
    <tx:annotation-driven proxy-target-class="true" /> 

    <!-- Configuracao do Banco de Dados -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost/odontonew" />
        <property name="username" value="root" />
        <property name="password" value="123456" />
    </bean>

    <!-- Configuracao do Hibernate -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="odontonewPU" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
                <property name="showSql" value="true" />
            </bean>
        </property>
    </bean>

    <!-- Configuracao do gerente de transacoes do Spring -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

</beans>

You do not have to declare your persistent classes in your persistence.xml, at least not with JPA 2.0. I am not sure about JPA 1.

The provider scans the classes in the persistence root and evaluates the annotations ( @Entity , @MappedSuperclass etc) as well as the contents of persistence.xml , so in the end you get a union of both declarations.

From the Pro JPA 2 book:

A managed class will be included if it is among the following:

  • local classes : annotated classes in the deployment unit in which the persistence.xml was packaged.

  • classes in mapping files : classes having mapping entries in an XML mapping file

  • explicitly listed classes : classes that are listed as class elements in the persistence.xml

  • additional jars of managed classes : annotated classes in a named jar listed in a jar-file element of the persistence.xml file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM