Hibernate基础之一:Hibernate4.1.6第一个程序
来源:互联网 发布:淘宝宝贝编辑助手 编辑:程序博客网 时间:2024/05/28 04:52
Hibernate4和3在细节上有挺多不一样的地方,今儿就吃了苦头,花了一两个小时解决两个环境问题引起的Exception。比如Could not parse mapping document from resource这个异常是我照着英文文档他写的类才改过来的。
先贴出源码吧,有几个异常网上没有正确的解决方案,用 Hibernate4.1.x出同样问题的先粘走试试,
第二部分说下异常的解决。
一:源码:
1.Oracle11建表
一:建表DROP TABLE student;CREATE TABLE student ( studentid NUMBER(9), name VARCHAR2(50) NOT NULL, age NUMBER(9) NOT NULL, CONSTRAINT student_studentid_pk PRIMARY KEY(studentid) );
2.建工程,倒库
2.1导入\hibernate-release-4.1.6.Final\lib下所有的库
2.2导入\slf4j-1.6.1\slf4j-nop-1.6.1.jar(单独下载的,不是hibernate里的api)
2.3导入Oracle\Administrator\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar
3.java类和测试类
package com.rt.hibernate.demo;public class Student {private int studentid;private String name;private int age;//setter&getterpublic int getStudentid() {return studentid;}public void setStudentid(int studentid) {this.studentid = studentid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
package com.rt.hibernate.demo;//import javax.transaction.Transaction;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;public class StudentTest {public static void main(String[] args){Student sd = new Student();sd.setStudentid(1100);sd.setName("zhangsan");sd.setAge(23);//准备工作SessionFactory sessionFactory;sessionFactory = new Configuration().configure().buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); //保存对象 session.save(sd); //收尾工作 transaction.commit(); session.close(); sessionFactory.close(); }}
4.hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:ORCL</property> <property name="connection.username">scott</property> <property name="connection.password">890307</property> <!-- JDBC connection pool (use the built-in) --> <!-- <property name="connection.pool_size">1</property> --> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- Enable Hibernate's automatic session context management --> <!-- <property name="current_session_context_class">thread</property> --> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <!-- <property name="hbm2ddl.auto">update</property> --> <mapping resource="com/rt/hibernate/demo/Student.hbm.xml"/><!-- 对应java类的配置文件 --> </session-factory></hibernate-configuration>
5.Student.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.rt.hibernate.demo"> <class name="Student"> <id name="studentid"> </id> <property name="name"/> <property name="age"/> </class> </hibernate-mapping>
二:今晚处理的Hibernate异常
1.Connection cannot be null when 'hibernate.dialect' not set
说是dialect设置没有,确认配过以下信息(对应Oracle类型查一下)的,
<!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
换用我测试类里session的设置和save()方法试试。
//准备工作SessionFactory sessionFactory;sessionFactory = new Configuration().configure().buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); //保存对象session.save(sd);//收尾工作transaction.commit(); session.close(); sessionFactory.close();我用别的方法也能消掉这个异常,可是会出下边第二个异常
2.Could not parse mapping document from resource *.hbm.xml
这个最头疼。
网上查到的资料都是说xml文件格式的,我换了n中格式可是问题依旧
知道Hibernate4和3在sessionFactory不同,就换了n多种方法都不行
然后看具体错误信息,提示是:processHbmXml,我就又仔仔细细检查了n遍
最后查到一个英文的示例,照着改了一下成了现在用的session.save()的设置和保存方法(解决异常1的方法),才把异常消掉……
3.无法将 NULL 插入
这个怪自己粗细大意,考来的建表脚本有NOT NULL,没消干净
- Hibernate基础之一:Hibernate4.1.6第一个程序
- 【hibernate4.3】构建第一个hibernate程序
- 第一个hibernate程序链接mysql hibernate4.0
- 【hibernate4.3】annotation 建立第一个程序
- Hibernate4入门到精通一——————第一个Hibernate程序
- 第一个Hibernate程序
- 第一个hibernate程序
- hibernate第一个程序
- 第一个hibernate程序
- 第一个hibernate程序
- 第一个Hibernate程序
- 第一个Hibernate程序
- 第一个Hibernate程序
- Hibernate第一个程序
- hibernate 第一个程序
- 第一个hibernate程序
- 第一个Hibernate程序
- Hibernate第一个程序
- 在MFC中添加用户自定义消息【转载】
- 解决MyEclipse中使用Alt+/没有提示的问题
- 用快排实现寻找数组中的最小k个数
- 线程的创建和退出
- 堆栈的使用
- Hibernate基础之一:Hibernate4.1.6第一个程序
- hdu 1599 find the mincost route
- hdu 1160 FatMouse's Speed
- android 基站定位
- zoj3656_Bit Magic(可以用2-SAT,我这里没用这个)
- 检查一个数组是否出现重复数字的算法(时间复杂度O(n的平方))
- MYSQL--事务处理
- 关于cdmaCellLocation的用法
- Makefile中的patsubst和foreach