hibernate学习(一)

一.进行hibernate配置

1.可以用myeclipe核心包自动添加hibernate3.3,也可以用官网下的包。

2.进行hibernate.cfg.xml中的配置,(注意最好不要把其改动,虽然别的也行,但是改了,在configuration那里要改)可以查看文档,主要进行的数据库连接

 

<!-- SQL dialect -->

<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

需要注意,用什么数据库,必须要相关方言,可查文档

 

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

这个生成对话时,Session session = sf.getCurrentSession();需要用到,没有会报错。

但用Session session = sf.openSession();可以屏蔽,不会报错。current_session_context_class

除了thread外,还有几个属性,不常用,不研究

 

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

这个是显示hibernate自动生成的sql

 

<property name="format_sql">true</property>

这个是显示规范的hibernate语句

 

<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>

 自动建表功能,有create,update,creat-drop,validate具体可查文档,create每次建新表,update没表就建新的,有的话在原有基础上自增,create-drop,session对话关闭,其表删除,validate貌似不匹配时用

 

<mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/>

单独是hibernate核心包时用,它指向的是**.hbm.xml,后缀最好不要乱改,否则容易出错,而且注意是/不是..

 

 

<mapping class="com.hibernate.Student" />

用到hibernate的annotation,这个比hbm.xml大大简化

 

3.进行student类的书写

注意:annotation和非annotation不一样,后者与数据库表关联在**.hbm.xml中,前者加标签,后面讲

一般包名用的是import javax.persistence.*;

String Date date;包名一般是 java.util.Date

 

4.进行studentTest测试

首先要记得,写main函数,我就忘了。。。。

然后主要配置

选择导入的包习惯为 org.hibernate.*,其他我也不好说对不对,貌似也可以

 

 Configuration cfg = new AnnotationConfiguration();

//这里要注意,如果用annotation这里就这么写,非annotation就写成Configuration cfg = new Configuration();
 SessionFactory sf =  cfg.configure().buildSessionFactory();

//只有有了SessionFactory才有session对话,倘若写的的不是hibernate.cfg.xml这里 cfg.configure("**.xml").buildSessionFactory();


 Session session = sf.openSession();//得到一个新的session对话

 // Session session = sf.getCurrentSession();也可以用这个,但这个是在线程中取对话,只要不commit(),取得的对话一致

Session session1 = sf.getCurrentSession();  session == session1,且commit后会话会自动关闭。

前者每写一个,就生成新的对话

 session.beginTransaction();
 session.save(s);
 session.getTransaction().commit();
 session.close();
 sf.close();

//记得关闭

 

问题1:

貌似是:添加myeclipe自带的hibernate3会出现,自己添加官网的,不会出现 

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).

log4j:WARN Please initialize the log4j system properly.

 解决办法:在src写一个log4j.properties,可以网上找,或在包里搜

 

问题2:

 Could not parse mapping document from resource com/hibernate/MyXml.xml

 Could not parse mapping document from invalid mapping
原因xml里没有这个头文件,后者不全,让其无法解析

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

 

问题3:

当你hibernate中,同时有,你运行非annotation时,就会出错

<mapping resource="com/hibernate/MyXml.xml"/>
<mapping class="com.hibernate.Student" />

An AnnotationConfiguration instance is required to use <mapping class="com.hibernate.Student"/>

 

办法:屏蔽掉mapping class,或者在Configuration cfg = new AnnotationConfiguration();写成这样,也可以避免

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值