2013年9月16日星期一

Find great God, being given org.hibernate.hql.ast.QuerySyntaxException

Project using jpa + hibernate + spring to do, and then I wrote a unit test run error
java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: LsmpOssSetting is not mapped [SELECT t FROM LsmpOssSetting t]

The following is the relevant code
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>


Finally, the test case
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());
}
}

------ Solution ------------------------------------- -------

com.newcosoft.lsmp.oss.mode.po.LsmpOssSetting


add this one to see.
------ For reference only -------------------------------------- -
Great God seeking answers, can not find where it is wrong, Chinese English sites are found over and over, basically saying select the entity followed by writing a mistake, this case is not wrong . But still reported not mapped
------ For reference only ------------------------------- --------


Thank god , add this is possible, but I'm such a definition in applicationContext.xml scan
<property name="packagesToScan" value="com.newcosoft.lsmp.oss.mode.po"/> 
not had phrase scans do not need to list the entity in the persistence.xml yet ?
------ For reference only -------------------------------------- -
this should be


com.newcosoft.lsmp.oss.mode.po. **. *


------ For reference only ----------------------------- ----------


tried, right , entity is on the po this package
------ For reference only ------------------- --------------------

  
tried, right , entity is placed on the bag po   like test, should be like the first one that I have written down.
------ For reference only -------------------------------------- -

    
tried, right , entity is placed on the bag po        like test, should be like the first one that I have written down.  
Thank you, now know what the problem packagesToScan, I continue to find a solution online right
------ For reference only ---------------- -----------------------
problems found
     <property name="packagesToScan" value="com.newcosoft.lsmp.oss.mode.po"/>         <property name="persistenceXmlLocation" value="src/main/resource/persistence.xml" /> 

problem lies in the persistence.xml , I take it and put together the spring configuration file , because the persistence.xml file , resulting in packagesToScan This property does not work , you can not go scanning entity, it will be reported is not mapped

没有评论:

发表评论