hibernate学习笔记1--------窥探hibernate

ssh的复习进入了数据层的学习hibernate.

哎!!!正所谓:雁过留声,人过留影。欢迎大家留言交流指正

一、引入:
模型不匹配(阻抗不匹配):关系模型与对象模型不匹配
java面向对象语言:
对象模型:其主要概念:继承、关联、多态等;
数据库的是关系模型:其主要概念:表、主键、外键等

解决办法:
1.使用jdbc手工转换。


2.使用ORM(object Relation Mapping对象关系映射)框架来解决,主流的ORM框架有Hibernate、TopLink、OJB

二、配置环境

三、小例子

domain

package cn.itcast.hibernate.domain;

import java.util.Date;

public class User {
	private int id;
	private String name;
	private Date birthday;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
}


映射文件文件作用:
是数据模型与对象模型对应起来

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.itcast.hibernate.domain">
	<class name="User" table="tb_User">
		<!-- 对象标示符,类型可以不写,hibernate自己识别 -->
		<id name="id">
			<!-- 指定主键生成方式。
        native根据方言判定生成主键的方式
         -->
			 <generator class="native"/> 
		</id>
		
		<property name="name"  />
		<property name="birthday"  />
	</class>


</hibernate-mapping>


hibernate.cfg.xml

<!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 >
		<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
		<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:ORCL</property>
		<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property><!-- 方言 -->
		<property name="hibernate.connection.username">websb</property>
		<property name="hibernate.connection.password">ddit</property>
		<property name="hibernate.hbm2ddl.auto">create</property><!-- 自动创建表 -->
		<mapping resource="cn/itcast/hibernate/domain/User.hbm.xml"/>
	</session-factory>
</hibernate-configuration>


简单粗暴的测试类:

package cn.itcast.hibernate;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import cn.itcast.hibernate.domain.User;



public class Base {
	public static void main(String []args){
		//对hibernate初始化----注册驱动
		Configuration cfg = new Configuration();
		cfg.configure();
		//工厂类
		SessionFactory sf = cfg.buildSessionFactory();
		
		Session s = sf.openSession();
		//打开事务
		Transaction tx=s.beginTransaction();
		
		User user = new User();
		user.setBirthday(new Date());
		user.setName("name");
		
		s.save(user);
		tx.commit();//提交事务
		s.close();
		System.out.println("end");
		
	}
}


小知识点总结:

(1)///表示的是连接的是本地的默认端口则可以省略
(2)配置sessionFactory中的property属性时,可以缺省hibernate这个开头
(3)hibernate-release-4.2.1.Final\project\etc下有个配置模版hibernate.properties.template
相关属性的值都有
(4)自动创建表属性值解释:
#hibernate.hbm2ddl.auto create-drop 在初始化时创建表,jdbc结束时删除表(一般应用与测试)
#hibernate.hbm2ddl.auto create 在初始化时创建表,jdbc结束时不删除表
#hibernate.hbm2ddl.auto update 拿映射文件和数据库中的表结构进行校验,如果不一致,则进行更新
#hibernate.hbm2ddl.auto valiadate  校验映射文件与表是否能对应起来,不一致报错(4.x的版本中没有此值,疑似删除了)
(5)对于mysql数据库,偶尔不开启事务,依然可以插入数据。请查看表结构(是什么引xing),是否支持事务。不支持事务的表,不能进行回滚。

运行时报的错误:
(1)org.hibernate.service.jndi.JndiException: Error parsing JNDI name [foo]
解决:
http://blog.csdn.net/walldr/article/details/8582581
扩展:
hiberbate的连接池配置:
http://www.cnblogs.com/lihuiyy/archive/2013/03/19/2969870.html
(2) org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ORA-00903: 表名无效

Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute statement
解决:
http://hi.baidu.com/hohai109/item/44f94ddda0fe18c9251f40ed

(3) Unsuccessful: drop table tb_1User cascade constraints
2013-5-18 23:31:34 org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: ORA-00942: 表或视图不存在

网上暂时没搜到解释,估计是在自动创建表之前,先drop一下,因为原来没有所以才报的吧~!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值