项目用的是jpa+hibernate+spring做的,然后我写了个单元测试,运行报错
java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: LsmpOssSetting is not mapped [SELECT t FROM LsmpOssSetting t ]以下是相关代码
package com.newcosoft.lsmp.oss.mode.po;import com.newcosoft.lsmp.common.BaseEntity;import java.sql.Timestamp;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;/**
 * LsmpOssSetting generated by hbm2java
 */
@Entity
@Table(name = "LSMP_OSS_SETTING", uniqueConstraints = @UniqueConstraint(columnNames = "ID"))
public class LsmpOssSetting extends BaseEntity implements java.io.Serializable { /**
 * 
 */
private static final long serialVersionUID = 1L;
private long id;
private String lastSettingContent;
private int syncFlag;
private String lsmpSettingName;
private String lsmpSettingDescription;
private LsmpOssSettingApp lsmpSettingApp;
private String lsmpSettingContent;
private int status;
private Timestamp createTime;
private Timestamp lastChangeTime;
private Integer ext01;
private String ext02; public LsmpOssSetting() {
} public LsmpOssSetting(long id) {
this.id = id;
} @Id
@Column(name = "ID", unique = true, nullable = false, precision = 7, scale = 0)
@SequenceGenerator(name = "LSMP_OSS_SETTING_ID_GENERATOR", sequenceName = "SEQ_OSS_SETTING")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "LSMP_OSS_SETTING_ID_GENERATOR")
public long getId() {
return this.id;
} public void setId(long id) {
this.id = id;
} @Column(name = "LAST_SETTING_CONTENT")
public String getLastSettingContent() {
return lastSettingContent;
} public void setLastSettingContent(String lastSettingContent) {
this.lastSettingContent = lastSettingContent;
} @Column(name = "SYNC_FLAG")
public int getSyncFlag() {
return syncFlag;
} public void setSyncFlag(int syncFlag) {
this.syncFlag = syncFlag;
} @Column(name = "LSMP_SETTING_NAME")
public String getLsmpSettingName() {
return this.lsmpSettingName;
} public void setLsmpSettingName(String lsmpSettingName) {
this.lsmpSettingName = lsmpSettingName;
} @Column(name = "LSMP_SETTING_DESCRIPTION")
public String getLsmpSettingDescription() {
return this.lsmpSettingDescription;
} public void setLsmpSettingDescription(String lsmpSettingDescription) {
this.lsmpSettingDescription = lsmpSettingDescription;
} @ManyToOne
@JoinColumn(name = "LSMP_SETTING_APP_ID", referencedColumnName = "ID")
public LsmpOssSettingApp getLsmpSettingApp() {
return this.lsmpSettingApp;
} public void setLsmpSettingApp(LsmpOssSettingApp lsmpSettingApp) {
this.lsmpSettingApp = lsmpSettingApp;
} @Column(name = "LSMP_SETTING_CONTENT")
public String getLsmpSettingContent() {
return this.lsmpSettingContent;
} public void setLsmpSettingContent(String lsmpSettingContent) {
this.lsmpSettingContent = lsmpSettingContent;
} @Column(name = "STATUS", precision = 2, scale = 0)
public int getStatus() {
return this.status;
} public void setStatus(int status) {
this.status = status;
} @Column(name = "CREATE_TIME")
public Timestamp getCreateTime() {
return this.createTime;
} public void setCreateTime(Timestamp createTime) {
this.createTime = createTime;
} @Column(name = "LAST_CHANGE_TIME")
public Timestamp getLastChangeTime() {
return this.lastChangeTime;
} public void setLastChangeTime(Timestamp lastChangeTime) {
this.lastChangeTime = lastChangeTime;
} @Column(name = "EXT_01", precision = 7, scale = 0)
public Integer getExt01() {
return this.ext01;
} public void setExt01(Integer ext01) {
this.ext01 = ext01;
} @Column(name = "EXT_02", length = 100)
public String getExt02() {
return this.ext02;
} public void setExt02(String ext02) {
this.ext02 = ext02;
}}applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://www.springframework.org/schema/tx
                    http://www.springframework.org/schema/tx/spring-tx.xsd
                    http://www.springframework.org/schema/aop
                    http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.newcosoft.lsmp.*.dao.*" />
<context:component-scan base-package="com.newcosoft.lsmp.*.business.*" /> <aop:aspectj-autoproxy /> <bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> <bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.newcosoft.lsmp.oss.mode.po"/>
<property name="persistenceXmlLocation" value="src/main/resource/persistence.xml" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false" />
<property name="database" value="ORACLE" />
<property name="databasePlatform" value=" org.hibernate.dialect.Oracle10gDialect" />
</bean>
</property>
<property name="persistenceUnitName" value="lsmp-oss" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
</props>
</property>
</bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean> <tx:annotation-driven transaction-manager="transactionManager" /> <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
<property name="jdbcUrl" value="jdbc:oracle:thin:@10.1.1.20:1521:orcl" />
<property name="username" value="bcgdev_fc" />
<property name="password" value="123456" />
<property name="idleConnectionTestPeriod" value="1" />
<property name="maxConnectionsPerPartition" value="8" />
<property name="minConnectionsPerPartition" value="1" />
<property name="idleMaxAge" value="100" />
<property name="partitionCount" value="1" />
<property name="acquireIncrement" value="5" />
<property name="statementsCacheSize" value="4" />
<property name="releaseHelperThreads" value="240" />
</bean>

<bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>

<aop:config>
<aop:pointcut id="productServiceMethods"
expression="execution(* 
com.newcosoft.lsmp.*.business..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.newcosoft.lsmp.*.business..*.*(..))" />
</aop:config> <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="change*" propagation="REQUIRED" />
<tx:method name="someOtherBusinessMethod" propagation="REQUIRES_NEW" />
<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
</beans>  persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
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_1_0.xsd">
<persistence-unit name="lsmp-oss" transaction-type="RESOURCE_LOCAL"> </persistence-unit>
</persistence>最后是测试用例
package com.newcosoft.lsmp.oss.dao.env;import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;import org.junit.Test;
import org.springframework.context.support.FileSystemXmlApplicationContext;public class SettingEnvDaoTest {
     @Test
public void getSetting(){
     FileSystemXmlApplicationContext context=new FileSystemXmlApplicationContext("E://pcgworkspace/oss-2.6.0/lsmp-oss-web/src/main/resource/applicationContext.xml");
     EntityManagerFactory emf=context.getBean("entityManagerFactory", EntityManagerFactory.class);
String hql = "SELECT t FROM LsmpOssSetting t ";
EntityManager em=emf.createEntityManager();
Query query=em.createQuery(hql);
System.out.println(query.getResultList().size());
}
}

解决方案 »

  1.   

    求大神解答,实在是找不到哪里错了,中文 英文网站都搜了个遍,基本上都是在说select后面跟的entity书写有误,这个例子里没有写错。可是还是报not mapped 
      

  2.   

     <persistence-unit name="lsmp-oss" transaction-type="RESOURCE_LOCAL">
     <class>com.newcosoft.lsmp.oss.mode.po.LsmpOssSetting</class>
        </persistence-unit>加上这一句看看。
      

  3.   


    谢谢大神,加上这句是可以的,不过我在applicationContext.xml定义了这么一句扫描
    <property name="packagesToScan" value="com.newcosoft.lsmp.oss.mode.po"/>  不是有了这句扫描就不需要在persistence.xml列出entity了吗?
      

  4.   

    <property name="packagesToScan" value="com.newcosoft.lsmp.oss.mode.po"/> 这个应该是
    <property name="packagesToScan">
    <list>
    <value>com.newcosoft.lsmp.oss.mode.po.**.*</value>
    </list>
    </property>
      

  5.   


    试了下,不对,entity是放在po这个包里的
      

  6.   


    试了下,不对,entity是放在po这个包里的好像测试的时候,就要像我第一种那种写法才行。
      

  7.   


    试了下,不对,entity是放在po这个包里的好像测试的时候,就要像我第一种那种写法才行。 
    感谢你,现在知道问题出在packagesToScan,我继续在网上找解决办法吧
      

  8.   

    问题找到了
         <property name="packagesToScan" value="com.newcosoft.lsmp.oss.mode.po"/>         <property name="persistenceXmlLocation" value="src/main/resource/persistence.xml" /> 
    问题出在persistence.xml上,我把它和spring配置文件放到了一起,因为persistence.xml文件的影响,导致packagesToScan这个属性无法正常工作,无法再去扫描entity,所以会报is not mapped