java.lang.IllegalArgumentException:未知实体

时间:2015-03-12 07:47:13

标签: spring hibernate jpa

我有一个测试驱动,

package com.chinalbs.service;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
@Component
@TransactionConfiguration(defaultRollback = true)
@Transactional
public class TestConductor {

    @Resource(name = "conductorServiceImpl")
    private ConductorService cService;

    @Resource(name = "enterpriseToConductorServiceImpl")
    private EnterpriseToConductorService eToConductorService;

    @Test
    public void testSave() {
        Conductor willedSaved = getConductor();
        cService.save(willedSaved);
        Conductor saved = cService.find(willedSaved.getId());
        Assert.assertEquals(willedSaved, saved);
        cService.delete(saved);
        Conductor notExisted = cService.find(saved.getId());
        Assert.assertNotNull(notExisted);
    }
}

和applicatin.xml是

<bean id="own" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${jdbc.driver}" />
        <property name="jdbcUrl" value="${jdbc.url.own}" />
        <property name="user" value="${jdbc.username.own}" />
        <property name="password" value="${jdbc.password.own}" />
        <property name="initialPoolSize" value="${connection_pools.initial_pool_size}" />
        <property name="minPoolSize" value="${connection_pools.min_pool_size}" />
        <property name="maxPoolSize" value="${connection_pools.max_pool_size}" />
        <property name="maxIdleTime" value="${connection_pools.max_idle_time}" />
        <property name="acquireIncrement" value="${connection_pools.acquire_increment}" />
        <property name="checkoutTimeout" value="${connection_pools.checkout_timeout}" />
    </bean>

<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="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="false" />
            <property name="generateDdl" value="true" />
        </bean>
    </property>
    <property name="packagesToScan">
        <list>
            <value>com.chinalbs.entity</value>
        </list>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
            <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
            <prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
            <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
            <prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
            <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
            <prop key="hibernate.connection.isolation">2</prop>
            <prop key="javax.persistence.validation.mode">none</prop>
        </props>
    </property>
</bean>

<bean id="dataSource" class="com.chinalbs.framework.datasource.RoutingDataSource">
       <property name="defaultTargetDataSource" ref="own"/>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource">
            <ref local="dataSource" />
        </property>
    </bean>

但它报告异常

  

java.lang.IllegalArgumentException:未知实体:   com.chinalbs.entity.Conductor at   org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:842)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:606)at   org.springframework.orm.jpa.SharedEntityManagerCreator $ SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:241)     在com.sun.proxy。$ Proxy24.persist(未知来源)at   com.chinalbs.framework.dao.impl.BaseDaoImpl.persist(BaseDaoImpl.java:108)     在   com.chinalbs.service.impl.ConductorServiceImpl.save(ConductorServiceImpl.java:40)     在   com.chinalbs.service.impl.ConductorServiceImpl.save(ConductorServiceImpl.java:23)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:606)at   org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)     在   org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)     在

     

实体是

package com.chinalbs.entity;
@Entity
@Table(name="rd_conductor")
@SequenceGenerator(name="sequenceGenerator",sequenceName = "rd_conductor_sequence")
public class Conductor implements Serializable{



    /**
     * 
     */
    private static final long serialVersionUID = -8389224625777077734L;

    /**
     * 
     */

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO,generator = "sequenceGenerator")
    private long id;

    @Column(name="sn")
    private String sn="";

    @Column(name="name")
    private String name;

    @Column(name="create_time")
    private Date createTime;

    public String getSn() {
        return sn;
    }

    public void setSn(String sn) {
        this.sn = sn;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}

为什么实体未知?请给我一些建议,谢谢你的任何帮助和建议

persistence.xml是

 <?xml version="1.0" encoding="UTF-8"?>
    <persistence 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" version="2.0">

        <persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL"></persistence-unit>

    </persistence>

1 个答案:

答案 0 :(得分:6)

我认为问题在于:

<property name="packagesToScan">
    <list>
        <value>com.chinalbs.entity</value>
    </list>
</property>

查看LocalContainerEntityManagerFactoryBean的API文档,可以看出packagesToScan方法将一个或多个字符串作为参数而不是List。

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean.html#setPackagesToScan-java.lang.String...-

然后将配置更改为:

<property name="packagesToScan" value="com.chinalbs.entity">

<property name="packagesToScan">
    <array>
        <value>com.chinalbs.entity</value>
    </array>
</property>

会解决问题。

如果仍然导致问题,请尝试在persistence.xml的<persistence-unit/ >元素中添加以下内容。

    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>