想参考Springside实现Hibernate的零配置文件,写了一个简单的测试用例,查询一张表(数据库表名为TB_USER,对应的实体类名为User),但是出错如下:
org.hibernate.hql.ast.QuerySyntaxException: User is not mapped [from User]
错误提示是说我的“from User”语句中“User”未建立映射。在网上查过,类似的问题一般是因为类名的大小写错误或者配置文件错误,导致类名和数据库表名没有建立映射关系,但我似乎没有这类问题。好几天了找不到原因,不知道哪里的配置有毛病,以下是相关代码
测试用例如下:
[code="java"]
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.test.junit38.SpringTransactionalTestCase;
import accountant.entity.User;
public class DaoTest extends SpringTransactionalTestCase {
@Autowired
org.springside.modules.orm.hibernate.HibernateDao dao ;
//列出所有User表记录
public void testUserList() {
//试过把完整包名带上,写成“from accountant.entity.User”,还是一样的错误
dao.find("from User", null);
}
}
[/code]
User类代码如下,其中@Table(name="TB_USER")指明对应的数据库表是“TB_USER”,这张表在库中确定存在,并有两条记录
[code="java"]
package accountant.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Entity
@Table(name="TB_USER")
public class User {
String name;
String id;
String sex;
String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
[/code]
applicationContext.xml中的数据库相关配置如下,其中
“”是指定自动扫描实体类的包,User类也就在“accountant.entity”这个包下
[code="java"]
<!-- 数据源配置,使用应用内的DBCP数据库连接池。-->
<!--这里数据库的连接应该是没问题的,因为出错时提示的是Hql执行错误,说明数据库连接已经成功建立了,不过是执行的语句有问题 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- Connection Pooling DBCP -->
<property name="initialSize" value="5" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="30" />
<property name="maxWait" value="1000" />
<property name="poolPreparedStatements" value="true" />
<property name="defaultAutoCommit" value="false" />
</bean>
<!-- Hibernate配置-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="namingStrategy">
<bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9iDialect</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider
</prop>
</props>
</property>
<property name="packagesToScan" value="accountant.entity.*" />
</bean>
<!-- Dao类 -->
<bean id="hibernateDao" class="org.springside.modules.orm.hibernate.HibernateDao">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
[/code]
相关的代码就是以上三处,不知道哪里有遗漏或是错误,请指点一二,非常感谢!
[b]问题补充:[/b]
[code="java"][/code]
先谢谢二位,我的spring和hibernate的jar版本如下:
[img]/upload/attachment/124016/5a67b24b-27e0-348a-ab6f-5d7003972509.gif[/img]
[img]/upload/attachment/124018/751e247e-4c6c-341f-9859-4a52863af90d.gif[/img]
这个小例子是在springside的mini-web范例上直接改的,应该环境方面不会有大问题吧
to layer555:
springside的mini-web的applicationContext.xml是这样的:
[code="java"]
<?xml version="1.0" encoding="UTF-8"?>
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-lazy-init="true">
<description>Spring公共配置文件 </description>
<!-- 定义受环境影响易变的变量 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<!-- 标准配置 -->
<value>classpath*:/application.properties</value>
<!-- 本地开发环境配置 -->
<value>classpath*:/application.local.properties</value>
<!-- 服务器生产环境配置 -->
<!-- <value>file:/var/myapp/application.server.properties</value> -->
</list>
</property>
</bean>
<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
<context:component-scan base-package="org.springside.examples.miniweb" />
<!-- 数据源配置,使用应用内的DBCP数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- Connection Pooling DBCP -->
<property name="initialSize" value="5" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="30" />
<property name="maxWait" value="1000" />
<property name="poolPreparedStatements" value="true" />
<property name="defaultAutoCommit" value="false" />
</bean>
<!-- 数据源配置,使用应用服务器的数据库连接池 -->
<!--<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/ExampleDB" />-->
<!-- Hibernate配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="namingStrategy">
<bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider
</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.ehcache_config_file}</prop>
</props>
</property>
<property name="packagesToScan" value="org.springside.examples.miniweb.entity.*" />
</bean>
<!-- 事务管理器配置,单数据源事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 事务管理器配置,多数据源JTA事务-->
<!--
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager or
WebLogicJtaTransactionManager" />
-->
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
[/code]
这里面也没有configurationClass,也能正常,不知道在什么地方设置或默认了
to xuzhfa123:
改了你说的两处,确实可以跑通了,非常感谢。但搞不明白的是,在上面贴的mini-web的配置文件中是这么写的:
[code="java"]
[/code]
这个例子是可以正常运行的,为什么我就必须改成
[code="java"]
[/code]
才可以呢?