“No Persistence Unit Found” error

后端 未结 4 1257
不知归路
不知归路 2021-02-05 09:14

I am getting the following error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name \'org.springframework.transaction.interc         


        
相关标签:
4条回答
  • 2021-02-05 09:35

    Well I usually place the persistence.xml in a resources folder /src/main/resouces/META-INF/persistence.xml because I do remember problems in the build not copying the resources to the output-directory.

    I just had a look at our configuration and it looks a little different:

    <!--
        - Drives transactions using local JPA APIs
    -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    
    <tx:annotation-driven mode="aspectj" proxy-target-class="true" />
    
    <!--
        Custom PersistenceUnitManager, that reads all persistence.xml files in the
        classpath and merges them to one single virtual persistence.xml.
    
        In order to operate properly, all persistence.xml have to define the same
        persistence unit name.
    -->
    <bean id="persistenceUnitManager" class="de.cware.cweb.jpa.MergingPersistenceUnitManager">
        <property name="persistenceXmlLocations">
            <list>
                <value>classpath*:META-INF/persistence.xml</value>
            </list>
        </property>
        <property name="defaultDataSource" ref="dataSource"/>
    </bean>
    
    <bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    
    <!--
        Creates a EntityManagerFactory for use with the Hibernate JPA provider and a simple
        in-memory data source populated with test data
    -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="loadTimeWeaver">
            <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
        </property>
        <property name="jpaDialect">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="false"/>
                <property name="generateDdl" value="true"/>
            </bean>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">${cweb.database.dialect}</prop>
                <prop key="hibernate.hbm2ddl.auto">${cweb.database.generate}</prop>
                <prop key="hibernate.search.default.indexBase">${cwb.home}/indexes</prop>
                <prop key="hibernate.ejb.naming_strategy">de.cware.cweb.jpa.QuotingNamingStrategy</prop>
            </props>
        </property>
        <property name="dataSource" ref="dataSource"/>
        <property name="persistenceUnitManager" ref="persistenceUnitManager"/>
    </bean>
    <bean id="dataSource" class="org.apache.commons.dbcp.ManagedBasicDataSource">
        <property name="driverClassName" value="${cweb.database.driver}"/>
        <property name="url" value="${cweb.database.url}"/>
        <property name="username" value="${cweb.database.user}"/>
        <property name="password" value="${cweb.database.password}"/>
        <property name="initialSize" value="${cweb.database.initial-pool-size}"/>
        <property name="maxIdle" value="${cweb.database.min-pool-size}"/>
        <property name="maxActive" value="${cweb.database.max-pool-size}"/>
        <property name="validationQuery" value="#{'${cweb.database.validationQuery}}'.length()==0?'':'${cweb.database.validationQuery}'}"/>
        <property name="testOnBorrow" value="#{'${cweb.database.validationQuery}}'.length()==0?false:true}"/>
        <property name="testWhileIdle" value="#{'${cweb.database.validationQuery}}'.length()==0?false:true}"/>
        <property name="timeBetweenEvictionRunsMillis" value="#{'${cweb.database.validationQuery}}'.length()==0?-1:10000}"/>
        <property name="minEvictableIdleTimeMillis" value="#{'${cweb.database.validationQuery}}'.length()==0?-1:60000}"/>
        <property name="defaultTransactionIsolation" value="#{ T(java.sql.Connection).TRANSACTION_READ_COMMITTED}"/>
    </bean>
    

    Hope this helps :-)

    Chris

    0 讨论(0)
  • 2021-02-05 09:37
    • keep your persistence.xml in WEB-INF/classes/META-INF/persistence.xml

    • remove hibernate conflicting libraries....

    • remove hibernate - 3.2.0 jar if hibernate core is there

    0 讨论(0)
  • 2021-02-05 09:38

    This error can happen if you have a hibernate mapped class with relations with another non-mapped class, a solution is to check the relation item as @transient (hibernate will ignore it, use this just to check if this is the error, and later map the related class). Example:

    @Entity
    public class City {
    
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    
    private String name;
    
    @Transient
    private State state;
    
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public State getState() {
        return state;
    }
    
    public void setState(State state) {
        this.state = state;
    }
    
    }
    


    And this is the non-mapped class which may give the error:

    public class State {
    
    private String name;
    
    }
    
    0 讨论(0)
  • 2021-02-05 09:39

    place the persistence.xml in a resources folder /src/main/resouces/META-INF/persistence.xml for me it's work this solution

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