Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conflicting persistence unit definitions for name

Got this exception when running my jar.

java.lang.IllegalStateException: Conflicting persistence unit definitions 
for name 'hibernateUnit': file:/D:/Assignment.jar, file:/D:/Assignment.jar

My persistence.xml

<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="hibernateUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>EventImpl</class>           
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format.sql" value="true"/>
            <property name="hibernate.connection.driver_class" value="${db.driver.name}"/>
            <property name="hibernate.connection.url" value="${db.url.value}"/>
            <property name="hibernate.connection.username" value="${db.user.name}"/>
            <property name="hibernate.connection.password" value="${db.user.password}"/>
        </properties>
    </persistence-unit>
</persistence>

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:jpa="http://www.springframework.org/schema/data/jpa"
       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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
    <context:annotation-config/>
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <context:property-placeholder location="db.properties"/>       
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${db.driver.name}"/>
        <property name="url" value="${db.url.value}"/>
        <property name="username" value="${db.user.name}"/>
        <property name="password" value="${db.user.password}"/>
    </bean>       
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="persistenceUnitName" value="hibernateUnit"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <property name="loadTimeWeaver">
            <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
        </property>
    </bean>
    <bean id="entityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>      
</beans>

Its interesting that when I run it in Idea - works great. This only happens when trying to run jar file of this application.

like image 311
user2620644 Avatar asked Nov 10 '22 04:11

user2620644


1 Answers

Try to specify the exact location of your persistence XML file in the entityManagerFactory bean:

<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>

like image 137
Florian Lopes Avatar answered Nov 14 '22 21:11

Florian Lopes