关于Spring整合Hibernate,映射实体类is not mapped

亚森保罗 2014-08-01 01:55:54
用了两种方式:
1、
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>vo/Customer.hbm.xml</value>
</list>
</property>
</bean>

不可行,报错没有找到映射
2、
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:config/hibernate.cfg.xml">
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
</bean>

hibernate.cfg.xml中: <mapping resource="vo/Customer.hbm.xml" />
可行,可以执行成功

搞不懂为什么第一种方式不行,求解
...全文
969 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
JE_GE 2017-03-02
  • 打赏
  • 举报
回复
http://blog.csdn.net/zhutulang/article/details/26968159
wangyitaowide 2017-03-02
  • 打赏
  • 举报
回复
楼主有答案没?
亚森保罗 2014-08-08
  • 打赏
  • 举报
回复
EvanJim 2014-08-04
  • 打赏
  • 举报
回复
应该是路径的问题
Coder_D 2014-08-04
  • 打赏
  • 举报
回复
是MyEclipse生成的文件吗?我记得有一个版本它生成映射文件的DTD末尾多了一个空格,所以会出问题!
亚森保罗 2014-08-04
  • 打赏
  • 举报
回复
多谢大家提出的可行性分析!如果需要我提供其他配置文件,请随时回帖!多谢!
亚森保罗 2014-08-04
  • 打赏
  • 举报
回复
引用 10 楼 applewings2014 的回复:

<property name="mappingLocations">
			<list>
				<value>classpath:/你的路径/你的路径/*.hbm.xml</value>
			</list>
</property>
试试通配符方式,匹配你某个文件夹下面的所有以 hbm.xml结尾的文件
错误还是一样,报没有映射Customer类
严重: Servlet.service() for servlet [default] in context with path [/MyWork] threw exception [org.hibernate.hql.ast.QuerySyntaxException: Customer is not mapped [from Customer where custname='zxc']] with root cause
org.hibernate.hql.ast.QuerySyntaxException: Customer is not mapped [from Customer where custname='zxc']
	at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:181)
	at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
	at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:93)
	at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:277)
	at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056)
	at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
	at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
亚森保罗 2014-08-04
  • 打赏
  • 举报
回复
引用 9 楼 gudu1289 的回复:
加上classpath:如下: <property name="mappingLocations"> <list> <value>classpath:vo/Customer.hbm.xml</value> <value>classpath:vo/Customer.hbm.xml</value> </list> </property>
也试过了↓,还是报同样错误
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource"/>
		</property>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQLDialect
            </value>
            </property>
        <property name="mappingLocations">
        <list>
            <value>classpath:vo/Customer.hbm.xml</value>
        </list>
    	</property>
	</bean>
亚森保罗 2014-08-04
  • 打赏
  • 举报
回复
引用 15 楼 u010808697 的回复:
应该是路径的问题

目录层次结构很简单的,应该不是路径问题。。。
亚森保罗 2014-08-04
  • 打赏
  • 举报
回复
引用 14 楼 u012171905 的回复:
是MyEclipse生成的文件吗?我记得有一个版本它生成映射文件的DTD末尾多了一个空格,所以会出问题!
不是用MyEclipse生成的框架,是我手动敲的。。。
applewings2014 2014-08-02
  • 打赏
  • 举报
回复

<property name="mappingLocations">
			<list>
				<value>classpath:/你的路径/你的路径/*.hbm.xml</value>
			</list>
</property>
试试通配符方式,匹配你某个文件夹下面的所有以 hbm.xml结尾的文件
qiubai00 2014-08-02
  • 打赏
  • 举报
回复
加上classpath:如下: <property name="mappingLocations"> <list> <value>classpath:vo/Customer.hbm.xml</value> <value>classpath:vo/Customer.hbm.xml</value> </list> </property>
亚森保罗 2014-08-01
  • 打赏
  • 举报
回复
真心求解,虽然用第二种方式可行, 但是一直身怀一颗打破砂锅问到底的心,况且刚刚接触SSH,对我以后的学习很有用,希望大家发表各自见解,多谢!
亚森保罗 2014-08-01
  • 打赏
  • 举报
回复
引用 5 楼 wlwlwlwl015 的回复:
路径写全,也许是路径不对,没加载到hbm配置文件。
vo已经是src下最外层目录了
亚森保罗 2014-08-01
  • 打赏
  • 举报
回复
引用 3 楼 wlwlwlwl015 的回复:
我以前的项目这样写是好的,你参考一下:

<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref local="dataSource" />
		</property>
		<property name="mappingResources">
			<list>
				<value>com/ql/entity/User.hbm.xml</value>
				<value>com/ql/entity/Student.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<value>
				hibernate.show_sql=true
				hibernate.dialect=org.hibernate.dialect.MySQLDialect
				hibernate.cache.use_second_level_cache=true
				hibernate.cache.provider_class=org.hibernate.cache.OSCacheProvider
				hibernate.generate_statistics=true
				hibernate.connection.release_mode=auto
				hibernate.autoReconnect=true
			</value>
		</property>
	</bean>
多谢回答,但是还是报同样的错 以下是我修改后的↓
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource"/>
		</property>
				<property name="mappingResources">
			<list>
				<value>vo/Customer.hbm.xml</value></list>
		</property>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQLDialect
            </value>
            </property>
	</bean>
小灯光环 2014-08-01
  • 打赏
  • 举报
回复
路径写全,也许是路径不对,没加载到hbm配置文件。
亚森保罗 2014-08-01
  • 打赏
  • 举报
回复
引用 1 楼 wlwlwlwl015 的回复:
你把mappingResources那一段放到hibernateProperties上面试试。
试过了还是不行 严重: Servlet.service() for servlet [default] in context with path [/MyWork] threw exception [org.hibernate.hql.ast.QuerySyntaxException: Customer is not mapped [from Customer where custname='asd']] with root cause org.hibernate.hql.ast.QuerySyntaxException: Customer is not mapped [from Customer where custname='asd']
小灯光环 2014-08-01
  • 打赏
  • 举报
回复
我以前的项目这样写是好的,你参考一下:

<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref local="dataSource" />
		</property>
		<property name="mappingResources">
			<list>
				<value>com/ql/entity/User.hbm.xml</value>
				<value>com/ql/entity/Student.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<value>
				hibernate.show_sql=true
				hibernate.dialect=org.hibernate.dialect.MySQLDialect
				hibernate.cache.use_second_level_cache=true
				hibernate.cache.provider_class=org.hibernate.cache.OSCacheProvider
				hibernate.generate_statistics=true
				hibernate.connection.release_mode=auto
				hibernate.autoReconnect=true
			</value>
		</property>
	</bean>
亚森保罗 2014-08-01
  • 打赏
  • 举报
回复
试过了还是不行 严重: Servlet.service() for servlet [default] in context with path [/MyWork] threw exception [org.hibernate.hql.ast.QuerySyntaxException: Customer is not mapped [from Customer where custname='asd']] with root cause org.hibernate.hql.ast.QuerySyntaxException: Customer is not mapped [from Customer where custname='asd']
小灯光环 2014-08-01
  • 打赏
  • 举报
回复
你把mappingResources那一段放到hibernateProperties上面试试。
BOS 技术整理-02 Oracle 数据库安装配置使用 直接安装 打开服务 并尝试连接数据库 安装远程连接工具 PLSQLDeveloper 安装配置使用 2.在 PLSQL Developer 启动,配置 oracle instanceclient 目录 4、 默认 PLSQL Developer 连接 Oracle 使用客户端字符集 utf-8 , 基于工具使用 SQL 插入中文有乱码问题,设置客户端字符集 gb2312 配置 环境变量 NLS_LANG = SIMPLIFIED CHINESE_CHINA.ZHS16GBK 弹窗了,就表示成功了! 工具补充: Navicat Premium 安装和使用 直接安装 ok jquery easyui 总合 (直接看API文档 后期加上如何使用) Orcale 创建用户 先进入 Orcale 命令行 针对 BOS 系统建立新的用户 创建 bos/bos 账户, 授予角色 connect、resource -- Create the user create user BOS identified by "" default tablespace SYSTEM temporary tablespace TEMP profile DEFAULT password expire; -- Grant/Revoke role privileges grant connect to BOS; grant resource to BOS; -- Grant/Revoke system privileges grant unlimited tablespace to BOS; Spring 环境搭建 IOC 导入 context 依赖 beans、core、expression AOP 导入 测试集成 Web 集成 配置监听器 整合持久层框架 hibernate、jpa web.xml 配置 spring 监听器 src/main/resource 下 建立 applicationContext.xml rc/main/resource 提供 config.properties 外部属性文件、log4j.properties 日志配置文件 配置文件如下: applicationContext.xml  struts2 注解 类上 @ParentPackage 包继承谁? extends=”struts-default” @Namespace 包命名空间 namespace=”/” @Actions struts2 的 Action 配置,用于配置多个 Action 方法上 @Action 配置访问路径,和方法绑定 @Result 结果集配置  struts2 和 spring 整合 struts2 的 Action 使用 spring 注解,被 spring 管理 @Controller spring 表现层 bean @Scope 配置 Action 为多例 Spring Data JPA 代替了 Hibernate "CURD " 但是和Hibernate一样 实现DAO(数据)层的功能 "增删改查" Hibernate 实体类 生成表结构 ---- 领域(Domain)驱动建模 这次使用的是驱动建模,传统的设计模式是: 先设计表,再创建实体 JPA 注解使用 @Entity 标明是实体类 @Table 映射表名称 @Id 主键标识 @GeneratedValue 自动生成主键的值 @Column 设置映射数据表中列信息 @Temporal 控制数据表保存日期哪个部分(日期、时间、日期时间) @OneToMany(mappedBy 相当于 inverse ) @ManyToOne 结合 @JoinColumn 添加一列外键 @ManyToMany 一方写 mappedBy 放弃外键维护、另一方 @JoinTable 维护中间表 收派标准修添加能实现 html 请求 后台处理 服务器端Service和DAO编写整合springdatajpa 业务层接口 修改 Action 代码调用 Service 业务层实现类 在applicationContext添加一条 用来整合 Spring Data jpa 注意: 这里的扫描包,必须指定到类的上级包 比如 一个类 com.itStone.bao.dao.test.java 那么它的上级包 就是 com.itStone.bao.dao 根级包是 : 就是 com 只需要编写 DAO 接口,继承 JpaRepository即可 不需要对 DAO 接口做任何实现, 实际上 spring 会调用 SimpleJpaRepository 实现 struts 2 注解

67,511

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧