2

i have a test driven,

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);
    }
}

and the applicatin.xml is

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

but it report exception

java.lang.IllegalArgumentException: Unknown entity: 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) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:241) at com.sun.proxy.$Proxy24.persist(Unknown Source) at com.chinalbs.framework.dao.impl.BaseDaoImpl.persist(BaseDaoImpl.java:108) at com.chinalbs.service.impl.ConductorServiceImpl.save(ConductorServiceImpl.java:40) at com.chinalbs.service.impl.ConductorServiceImpl.save(ConductorServiceImpl.java:23) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) at

the entity is

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;
    }
}

why the entity is unknown? please give me some suggestion,thanks for your any help and suggestion

persistence.xml is

 <?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>
9
  • Tidy up your config listing so that it is displayed correctly.. Commented Mar 12, 2015 at 9:13
  • @AlanHay thanks ,i have change the config,and i think the problem may be entity should configure in junit test in other ways? Commented Mar 12, 2015 at 9:23
  • 2
    Check the annotations '@Entity', '@Table' are from package javax.persistence and not org.hibernate. Check there is nothing in persistence.xml to prevent classes being recognised as entities. Commented Mar 12, 2015 at 9:28
  • @AlanHay '@Entity', '@Table' are from package javax.persistence,and the persistence.xml is added to my question Commented Mar 12, 2015 at 9:39
  • 1
    Try: <property name="packagesToScan" value="com.chinalbs.entity"/> Commented Mar 12, 2015 at 9:55

1 Answer 1

6

I believe the issue is with the following:

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

Looking at the API docs for LocalContainerEntityManagerFactoryBean it can be seen that the packagesToScan method takes one or more Strings as parameters rather than a List.

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

It would seem then that changing your config to either:

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

or

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

would address the problem.

If that still causes issues try adding the following within the <persistence-unit/ > element of persistence.xml.

    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.