在学习spring的时候,要整合hibernate,本来是看起来挺简单的,但是遇到的远要比想到了多,而且多很多,
期间几天一个bug实在难调,几度放弃,但终究柳暗花明,抑制不住喜悦就想着分享一下成果吧。

1、实体类 User:

 1 import java.util.Date;
 2
 3 import javax.persistence.Column;
 4 import javax.persistence.Entity;
 5 import javax.persistence.GeneratedValue;
 6 import javax.persistence.GenerationType;
 7 import javax.persistence.Id;
 8 import javax.persistence.Temporal;
 9 import javax.persistence.TemporalType;
10
11 @Entity
12 public class User {
13
14     @Id
15     @Column(name="user_id")
16     @GeneratedValue(strategy=GenerationType.IDENTITY)
17     private Integer id;
18     @Column(name="user_name")
19     private String name;
20     private String pass;
21     @Temporal(TemporalType.DATE)
22     private Date birth;
23
24
25
26     public User() {
27
28     }
29
30
31     public User( String name, String pass, Date birth) {
32         super();
33         this.name = name;
34         this.pass = pass;
35         this.birth = birth;
36     }
37
38     public Integer getId() {
39         return id;
40     }
41
42
43     public void setId(Integer id) {
44         this.id = id;
45     }
46
47
48     public String getName() {
49         return name;
50     }
51     public void setName(String name) {
52         this.name = name;
53     }
54     public String getPass() {
55         return pass;
56     }
57     public void setPass(String pass) {
58         this.pass = pass;
59     }
60
61
62     public Date getBirth() {
63         return birth;
64     }
65
66
67     public void setBirth(Date birth) {
68         this.birth = birth;
69     }
70
71
72 }

2、hibernate配置文件hibernate.cfg.xml:

 1 <?xml version='1.0' encoding='utf-8'?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5
 6 <hibernate-configuration>
 7
 8     <session-factory>
 9         <!-- hibernate 所需的配置信息 -->
10         <property name="show_sql">true</property>
11         <property name="dialect">org.hibernate.dialect.MySQLDialect</property><!--方言-->
12         <property name="format_sql">true</property>
13         <property name="hbm2ddl.auto">update</property>
14
15         <!-- hibernate 指定映射类 -->
16         <mapping class="com.csu.domain.User"/>
17
18     </session-factory>
19
20 </hibernate-configuration>

3、c3p0数据库连接池所需要的数据库配置信息,放置在jdbc.properties文件中,这样也方便日后修改只需要改动属性文件,
而不需要改动配置文件:

user=root
password=7890
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/spring
maxPoolSize=200
minPoolSize=2
initialPoolSize=2

4、spring的配置文件beans.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2
 3 <!-- 整个Spring 文件的根元素就是beans -->
 4 <beans xmlns="http://www.springframework.org/schema/beans"
 5 xmlns:p="http://www.springframework.org/schema/p"
 6 xmlns:util="http://www.springframework.org/schema/util"
 7 xmlns:context="http://www.springframework.org/schema/context"
 8     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 9     xsi:schemaLocation="http://www.springframework.org/schema/beans
10         http://www.springframework.org/schema/beans/spring-beans.xsd
11
12         http://www.springframework.org/schema/util
13         http://www.springframework.org/schema/util/spring-util.xsd
14
15         http://www.springframework.org/schema/context
16         http://www.springframework.org/schema/context/spring-context.xsd
17         ">
18
19        <!-- *****************配置数据源*********************** -->
20
21        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
22
23            <property name="locations">
24                <!-- 列出需要读取的属性文件 -->
25                <list>
26                    <value>classpath:jdbc.properties</value>
27                </list>
28            </property>
29
30        </bean>
31
32        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
33            p:user="${user}"
34            p:password="${password}"
35            p:driverClass="${driverClass}"
36            p:jdbcUrl="${jdbcUrl}"
37            p:maxPoolSize="${maxPoolSize}"
38            p:minPoolSize="${minPoolSize}"
39            p:initialPoolSize="${initialPoolSize}"
40        />
41        <!-- *****************配置数据源*********************** -->
42
43     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
44     p:dataSource-ref="dataSource"
45     p:configLocation="classpath:hibernate.cfg.xml"
46     />
47     <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"
48     p:sessionFactory-ref="sessionFactory"
49     />
50
51 </beans>

5、测试主类:

 1 import java.util.Date;
 2
 3 import org.hibernate.FlushMode;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 import org.springframework.orm.hibernate3.HibernateTemplate;
 7
 8 import com.csu.domain.User;
 9
10 public class SpHiTest {
11
12     public static void main(String[] args) {
13         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
14
15         //直接利用数据库的HibernateTemplate类进行数据库操作,可以极大方便了操作流程
16         HibernateTemplate ht = (HibernateTemplate)ctx.getBean("hibernateTemplate");
17
18         ht.save(new User("chen","123",new Date()));
19
20     }
21
22
23
24 }

6、期间碰到的问题是一直出现这个问题:

Exception in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.at org.springframework.orm.hibernate4.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1128)at org.springframework.orm.hibernate4.HibernateTemplate$20.doInHibernate(HibernateTemplate.java:737)at org.springframework.orm.hibernate4.HibernateTemplate.doExecute(HibernateTemplate.java:341)at org.springframework.orm.hibernate4.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:309)at org.springframework.orm.hibernate4.HibernateTemplate.persist(HibernateTemplate.java:734)at com.csu.test.SpHiTest.main(SpHiTest.java:26)

反正无论怎么调都无法正确,最终还是将hibernate4版本换成了hibernate3才得以解决。最后的结果看一下:


转载于:https://www.cnblogs.com/chentao-cus/p/4837839.html

整合Spring与Hibernate相关推荐

  1. java project整合spring和hibernate

    web工程整合ssh已经很多例子了,本例目前为java project,所以要整合spring和hibernate. spring版本是3.0.6 hibernate版本是3.3.0 project架 ...

  2. spring和hibernate整合的几种方式详细介绍

    Spring与Hibernate整合 Spring与Hibernate整合关键点: 1) Hibernate的SessionFactory对象交给Spring创建: 2) hibernate事务交给s ...

  3. 总结xml配置spring-aop声明式事务配置与hibernate报错:** isno active spring和hibernate整合,原因会话工厂去路(到spring不仅仅是bean)错误

    spring事务管理太厉害了!!可以不再自管事务开发了! spring aop声明式事务配置 问题: 困扰我近十多天的的spring事务管理终于解决了, 再也不用自己管理事务了 嗯,可以删该死的hib ...

  4. java ee ssh三大框架知识点_详解JAVAEE——SSH三大框架整合(spring+struts2+hibernate)...

    详解JAVAEE--SSH三大框架整合(spring+struts2+hibernate) 发布时间:2020-09-17 13:34:05 来源:脚本之家 阅读:64 作者:kent鹏 一.整合原理 ...

  5. Ehcache 整合Spring 使用页面、对象缓存

    2019独角兽企业重金招聘Python工程师标准>>> che 整合Spring 使用页面.对象缓存 Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且 ...

  6. ehcache 缓存java对象_Ehcache 整合Spring 使用页面、对象缓存

    Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...

  7. Ehcache学习总结(3)--Ehcache 整合Spring 使用页面、对象缓存

    Ehcache 整合Spring 使用页面.对象缓存 Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式 ...

  8. spring入门(11)-spring与hibernate整合完成增删改查的操作(封装HibernateTemplate模版类对象)

    今天是spring的最后一节课,这节课老师讲了spring与hibernate整合完成增删改查的操作,这是很重要的一节课,这也是第一次真正的实现spring结合Hibernate和数据库连接上,下面是 ...

  9. SSH整合(Struts2+hibernate+spring)

    1.创建表 create table t_user(id int primary key auto_increment,username varchar(50),password varchar(32 ...

最新文章

  1. DOS 批处理命令学习1
  2. js日期比较大小_node.js 内存泄漏的秘密
  3. 前端学习(578):chrome devtools
  4. 按钮3D分层悬停效果
  5. python 并发编程 多线程 目录
  6. TCP/IP 详解 卷一 协议 (第二版)翻译问题(第一周)2018
  7. java学习之单件模式
  8. POJ 1947 Rebuilding Roads
  9. 阿里云添加DNS API 的accesskey
  10. What is road rage?
  11. ScreenShot(Reproduced)
  12. 报表工具之ireport
  13. 线性回归分析中的哑变量
  14. SRM 453.5(Div1)
  15. MySQL树形结构设计
  16. Ubuntu 14.04 下安装Skype
  17. 实木地板被机器人弄成坑_木地板砸出小坑怎么办
  18. 2017CBBA中国健身设备器材展览会会刊(参展商名录)
  19. 瑞星杀毒软件的安装与设置
  20. 大数据挖掘建模案例分析:利用BP神经网络算法进行用户行为分析(一)

热门文章

  1. 【GDOI 2011 DAY2 T3】零什么的最讨厌了 (快速求阶乘、中国剩余定理)
  2. return to dl_resolve无需leak内存实现利用
  3. 算法----字符串拷贝
  4. 转帖Masonry介绍与使用实践(快速上手Autolayout)
  5. html5做一个相册_HTML5最新版本介绍
  6. 邀请合作如何表达_如何邀请大咖嘉宾来商群分享
  7. (220)FPGA内部组成LUT介绍
  8. java jar metainf_java – 从生成的jar文件中排除META-INF / maven文件夹
  9. matlab吧结果存数组,求助:如何将带有符号变量的运算结果储存到数组中
  10. python tkinter button_[转载]Python Tkinter之Button(转载)