jpa2与maven 3中的spring 3集成,在测试中找不到Entity

时间:2012-07-08 02:15:29

标签: testing jpa-2.0

我的pom.xml是:

<modelVersion>4.0.0</modelVersion>
<groupId>org.aio</groupId>
<artifactId>aio-core</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<name>aio</name>

<properties>
    <org.springframework.version>3.1.1.RELEASE</org.springframework.version>
    <org.springframework.security.version>3.0.2.RELEASE</org.springframework.security.version>
    <aspectj.version>1.6.9.M2</aspectj.version>
    <org.slf4j.version>1.6.1</org.slf4j.version>
    <hibernate.version>4.1.4.FINAL</hibernate.version>
    <hibernate.jpa.version>1.0.1.Final</hibernate.jpa.version>
    <commons-dbcp.version>1.4</commons-dbcp.version>
    <jackson.mapper.version>1.8.2</jackson.mapper.version>
    <freemarker.version>2.3.18</freemarker.version> 
</properties>
<build>
    <plugins>
        <!-- Configuration which allows JUnit tests to be placed in the same folder 
            as java files -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <testClassesDirectory>${project.build.directory}/test-classes/</testClassesDirectory>
            </configuration>
        </plugin>
    </plugins>
    <!-- <filters> -->
    <!-- filter files DONT end up in WAR file. -->
    <!-- <filter>src/main/recources/jdbc.properties</filter> -->
    <!-- </filters> -->
    <!-- Configuration which allows configuration files (such as xml files) 
        to be placed in the same folder as java files -->
    <resources>
        <resource>
            <directory>${basedir}/src/main/resources/</directory>
        </resource>
    </resources>
</build>


<dependencies>
    <!-- ********************************************************************** 
        ** SPRING DEPENDENCIES ** ********************************************************************** -->
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework.version}</version>
        <exclusions>
            <!-- Exclude Commons Logging in favor of SLF4j -->
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
        <type>jar</type>
        <optional>false</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework.version}</version>
        <type>jar</type>
        <optional>false</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${org.springframework.version}</version>
        <type>jar</type>
        <optional>false</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${org.springframework.version}</version>
        <type>jar</type>
        <optional>false</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${org.springframework.version}</version>
        <type>jar</type>
        <optional>false</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${org.springframework.version}</version>
        <type>jar</type>
        <optional>false</optional>
    </dependency>


    <!-- Security -->
    <!--  
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>${org.springframework.security.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>${org.springframework.security.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-acl</artifactId>
        <version>${org.springframework.security.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>${org.springframework.security.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
        <version>${org.springframework.security.version}</version>
    </dependency>
    -->


    <!-- URL Rewrite -->
    <dependency>
        <groupId>org.tuckey</groupId>
        <artifactId>urlrewritefilter</artifactId>
        <version>3.1.0</version>
        <scope>runtime</scope>
    </dependency>

    <!-- HIBERNATE DEPENDENCIES -->

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.jboss.spec.javax.transaction</groupId>
                <artifactId>jboss-transaction-api_1.1_spec</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <version>${hibernate.jpa.version}</version>
    </dependency> 
    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>


    <!-- ********************************************************************** 
        ** OTHER DEPENDENCIES ** ********************************************************************** -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-jcl</artifactId>
        <version>${org.slf4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${org.slf4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${aspectj.version}</version>
        <type>jar</type>
        <optional>false</optional>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
    </dependency>



    <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <type>jar</type>
        <scope>provided</scope>
        <optional>false</optional>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <type>jar</type>
        <scope>provided</scope>
        <optional>false</optional>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <type>jar</type>
        <scope>provided</scope>
        <optional>false</optional>
    </dependency>

    <!-- File upload -->
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.2.1</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>${commons-dbcp.version}</version>
    </dependency>

    <!-- ********************************************************************** 
        ** TEST DEPENDENCIES ** ********************************************************************** -->

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>1.8.0.10</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc14</artifactId>
        <version>10.2.0.4.0</version>
        <optional>false</optional>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.17</version>
        <optional>false</optional>
    </dependency>
    <!-- Jackson JSON Mapper -->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>${jackson.mapper.version}</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>${jackson.mapper.version}</version>
    </dependency>
    <!-- Jackson JSON Mapper end -->

    <dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
        <version>${freemarker.version}</version>
    </dependency>
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2.2</version>
        <scope>runtime</scope>
    </dependency>
</dependencies>



<repositories>
    <repository>
        <id>repository.springframework.milestone</id>
        <name>Spring Framework Maven Milestone Repository</name>
        <url>http://maven.springframework.org/milestone</url>
    </repository>
    <repository>
        <id>spring-releases</id>
        <name>Spring Maven RELEASE Repository</name>
        <url>http://maven.springframework.org/release</url>
    </repository>
    <repository>
        <id>repository.jboss.org-public</id>
        <name>JBoss.org Maven repository</name>
        <url>https://repository.jboss.org/nexus/content/groups/public</url>
    </repository>
    <repository>
        <id>EclipseLink Repo</id>
        <url>http://www.eclipse.org/downloads/download.php?r=1&amp;nf=1&amp;file=/rt/eclipselink/maven.repo</url>
    </repository>
</repositories>

和spring配置文件app-context.xml:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:jdbc.properties</value>
        </list>
    </property>
</bean>

<context:annotation-config/>


<aop:aspectj-autoproxy />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="defaultAutoCommit" value="false" />
</bean>

 <!-- JPA EntityManagerFactoryBean for EntityManager-->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceXmlLocation" value="classpath:persistence.xml" />
    <property name="persistenceUnitName" value="persistenceUnit" />
    <property name="jpaPropertyMap">
        <props>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
        </props>
    </property>
</bean>


<!-- Transaction manager for JPA -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory">
        <ref bean="entityManagerFactory" />
    </property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<context:component-scan base-package="org.aio.*">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

当我的应用程序在Web服务器上运行时,它工作正常,但是当我运行junit测试时,它会发生:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Article is not mapped [select count(*) from Article]

但是当我将Article.class复制到测试类路径'target / test-classes / org / aio / modules /'时,它可以再次工作,所以我很好奇hibernate如何在类路径中自动扫描带注释的实体,请帮助我

源代码: https://code.google.com/p/jpa2-spring3-aio/source/checkout

1 个答案:

答案 0 :(得分:1)

啊,经过一系列的测试,我发现如果把persistence.xml放到文件夹'META-INF /'中,它会找到带注释的类,但如果我按春天加载它

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    ...
    <property name="persistenceXmlLocation" value="classpath:persistence.xml" />
    ....</bean>

所以,在junit测试中,JPA扫描实体路径配置在spring和标准方式之间必须完全不同。