gpt4 book ai didi

spring - java.lang.IllegalArgumentException : Unknown entity 异常

转载 作者:行者123 更新时间:2023-12-02 08:29:16 24 4
gpt4 key购买 nike

我有一个测试驱动,

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

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

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>

最佳答案

我认为问题出在以下方面:

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

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

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-unit/ > 中添加以下内容persistence.xml 的元素。

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

关于spring - java.lang.IllegalArgumentException : Unknown entity 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29004334/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com