Spring JPA配置问题

时间:2015-11-19 15:18:51

标签: spring hibernate jpa

我已经完成了关于Spring JPA配置的几乎所有帖子。基本上我想在我的项目中实现JPA分页,为此我需要Spring JPA支持。但是当我在项目中包含spring-data-jpa.jar时,我得到以下异常。

Caused by: java.lang.IllegalArgumentException: JBAS011470: Persistence unitName was not specified and there are 2 persistence unit definitions in application deployment deployment "CPCardApp.ear".  Either change the application deployment to have only one persistence unit definition or specify the unitName for each reference to a persistence unit.
    at org.jboss.as.jpa.container.PersistenceUnitSearch.ambiguousPUError(PersistenceUnitSearch.java:172)
    at org.jboss.as.jpa.container.PersistenceUnitSearch.findWithinDeployment(PersistenceUnitSearch.java:152)
    at org.jboss.as.jpa.container.PersistenceUnitSearch.findWithinApplication(PersistenceUnitSearch.java:86)
    at org.jboss.as.jpa.container.PersistenceUnitSearch.findPersistenceUnitSupplier(PersistenceUnitSearch.java:76)
    at org.jboss.as.jpa.container.PersistenceUnitSearch.resolvePersistenceUnitSupplier(PersistenceUnitSearch.java:63)
    at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.getPersistenceUnit(JPAAnnotationParseProcessor.java:357)
    at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.getBindingSource(JPAAnnotationParseProcessor.java:288)
    at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.processMethod(JPAAnnotationParseProcessor.java:201)
    at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.processPersistenceAnnotations(JPAAnnotationParseProcessor.java:138)
    at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.deploy(JPAAnnotationParseProcessor.java:95)
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:116) [jboss-as-server-7.1.3.Final-redhat-4.jar:7.1.3.Final-redhat-4]
    ... 5 more

persistence.xml (注意:我的xml中有2个持久性单元,Spring不支持)

<?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="data01">
        <jta-data-source>java:/MySqlDataDS</jta-data-source>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.cache.use_second_level_cache"
                value="false" />
            <property name="hibernate.id.new_generator_mappings" value="false" />
        </properties>
    </persistence-unit>
    <persistence-unit name="cpaudit">
        <jta-data-source>java:/MySqlAuditDS</jta-data-source>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.cache.use_second_level_cache"
                value="false" />
            <property name="hibernate.id.new_generator_mappings" value="false" />
        </properties>
    </persistence-unit>
</persistence>

1 个答案:

答案 0 :(得分:0)

您可以拥有2个持久性单位。您的配置存在问题(使用JBoss + Spring + JPA(Hibernate)+ JTA的组合)。正确的配置取决于您的JBoss和Spring版本。 无论您需要一个xml文件来定义具有两个实体管理器的JPA配置。

在application-jpa-config.xml中尝试这样的事情:

include