13

我已经创建了 spring data JPA 示例项目。为此,我使用了 eclips。有两个项目,一个是只有带注释的实体类的领域对象项目。其他项目是实际的 spring data jpa 项目,它对项目一具有依赖性。实际上它通过 maven 依赖获取域项目实体类。

项目 1:人力资源管理域

实体类:

package com.hrm.ws.data.domain;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;



@Entity(name = "Employee")
@Table(name = "employee")
@Inheritance(strategy = InheritanceType.JOINED)
public class Employee {

    protected long id;

    protected String firstName;

    protected String lastName;

    /**
     * Gets the value of the id property.
     * 
     */
    @Id
    @Column(name = "id", scale = 0)
    @GeneratedValue(strategy = GenerationType.AUTO)
    public long getId() {
        return id;
    }

项目 2:hrm-ws-service

持久性上下文.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"

       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/data/jpa
                           http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">


        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="${jdbc.driverClassName}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>

        <jpa:repositories base-package="com.hrm.ws.data.repository" />


        <bean id="employeeDao" class="com.hrm.ws.data.repository.EmployeeRepositoryImpl"/>      


        <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">

              <property name="entityManagerFactory" ref="entityManagerFactory" />
              <property name="jpaDialect">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
              </property>

        </bean>

        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

              <property name="dataSource" ref="dataSource" />

              <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                  <property name="generateDdl" value="${jdbc.generateDdl}" />
                  <property name="showSql" value="${jdbc.showSql}"/>
                </bean>
              </property>
              <property name="persistenceUnitName" value="jpa.sample" />

        </bean>

</beans>

所以我的包的设置如下:

src/main/java - 包含 spring 存储库

src/main/resources - 包含应用程序上下文

src/test/java - 包含单元测试

以及项目 1(hrm-domain)的依赖项

我的问题是当我运行这个项目时,我收到如下错误:

... 38 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Not an entity: class com.hrm.ws.data.domain.Employee
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1429)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:844)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:786)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474)
    ... 40 more


**Caused by: java.lang.IllegalArgumentException: Not an entity: class** com.hrm.ws.data.domain.Employee
    at org.hibernate.ejb.metamodel.MetamodelImpl.entity(MetamodelImpl.java:160)
    at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:52)
    at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getMetadata(JpaEntityInformationSupport.java:61)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:145)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:83)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:66)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:146)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:120)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:39)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
    ... 48 more

我想知道这个问题,因为当我将域对象(Employee.java)放在我的 spring 项目中时,它自己而不是单独的 jar 它工作正常。这个问题只发生在它作为一个单独的 jar 文件中。如果有人对这种情况有经验,请给我帮助。

4

3 回答 3

7

我也有同样的问题。我的是我没有packagesToScan在 bean 中提供属性entityManagerFactory,所以 Spring 无法加载实体。查看emfspring 配置 xml 中的 bean 声明。以下是 EMF 声明,希望它可以帮助您摆脱错误:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="packagesToScan" value="com.hrm.ws.data.domain" />
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
</bean>
于 2012-07-09T16:39:19.533 回答
1

您没有在 com.hrm.ws.data.domain 包中扫描带有 @Entity 注释的 bean,因此 spring 容器不知道您的实体,因此会产生异常。将以下行添加到您的 persistence-context.xml 配置文件中:

<context:annotation-config />
<context:component-scan base-package="com.hrm.ws.data.domain" />

您还需要按如下方式更改根 xml 元素,以便识别上下文元素:

<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/data/jpa
                           http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

而且,如果您还没有,请将 spring-context 依赖项添加到您的 pom 文件中。

于 2012-06-04T04:14:00.230 回答
0

使用以下选项之一:-

  1. context:component-scan base-package="Your Base Package Name" in spring.xml 文件

  2. 在 entityManagerFactory 上设置 packagesToScan 属性

  3. 使用 persistence.xml 并在那里列出所有实体。

于 2015-01-23T17:13:06.010 回答