gpt4 book ai didi

java - 名称的冲突持久性单元定义

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:03:43 26 4
gpt4 key购买 nike

运行我的 jar 时出现这个异常。

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

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

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

有趣的是,当我在 Idea 中运行它时 - 效果很好。这仅在尝试运行此应用程序的 jar 文件时发生。

最佳答案

尝试在 entityManagerFactory 中指定持久性 XML 文件的确切位置 bean :

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

关于java - 名称的冲突持久性单元定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24935192/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com