1 新建项目
2 学习建立user-library-hibernate,并加入相应的jar包
a项目右键-build path-configure build path-add library
b选择user-library,在其中新建library,命命为hibernate
c 在该library中加入hibernate所需要的jar包
hibernate
/hibernate3.jar
/lib/required目录下的所有包 6个
Sl4j-nop jar
3 引入mysql的JDBC驱动包
4 在MYSQL中建数据库和相应的表student(id,name,age)
5 建立hibernate配置文件hibernate.cfg.xml
参考文档中COPY,修改对应的数据库连接,
6 建立student类
7 建立映射文件Student.hbm.xml 参考相应文档
8 将映射文件加到hibernate-cfg.xml中
搭建日志环境并配置显示DDL语句
slf的实现:slf4j nodep ,log4j ,jdk logging api ,apache common-log.
slf4j.nop.jar是slf-api.jar其相应的接口实现
把slf的接口对到log4j的实现,在user library中hibernate,把slf的实现slf4j-nop-
把slf的接口转换成log4j的接口.最后添加一个log4j的配置文件log4j.properties
利用HIBERNATE导出数据库建表
//读取配置文件hibernate.cfg.xml
Configuration cfg=new AnnotationConfiguration().configure();(有注解时使用AnnotationConfiguration),configure()可以手动指定配置文件名称.
Configuration cfg=new Configuration(),会默认读取hibernate.properties文件
//创建schemaExport对象
import org.hibernate.tool.hbm2ddl.SchemaExport;
SchemaExport export=new SchemaExport(cfg);
//创建数据库表
export.create(true, true);
对象映射(采用Annotation注解方式)
1 建表
Create table teacher(id int primary key,name varchar(20),title varchar(20));
2 创建teacher类,并进行注解@
import javax.persistence.Entity;
import javax.persistence.Id;
在bean中加注解
@ Entity,@Id(加在getId()上面)
3 在hibernate中加入annotation相应的jar包
hibernate-annotations.jar
/lib目录下加入相应的包ejb3-persistence.jar, hibernate-commons-annotations.jar
注意:annotation文档中并没有提到hibernate-annotations.jar包
4 参考annotation文档建立对应的注解
5 在hibernate.cfg.xml中建立映射
<mapping class=”com.xx.xx”/>
6 示例
//AnnotationConfiguration;
Configuration cfg=new AnnotationConfiguration();
SessionFactory sf=cfg.configure().buildSessionFactory();
Session session=sf.openSession();
session.beginTransaction();
session.save(t);//
session.getTransaction().commit();
session.close();
sf.close();
对象映射(采用配置文件方式)
1 在相应的类中建立对应的配置文件.
例如Student类对应的配置文件Student.hbm.xml
<hibernate-mapping package="com.model">
<class name="Student" table=" Student" >
<id name="id" column="id"></id>
<property name="name" column="name" />
<property name="age" column="age" />
</class>
</hibernate-mapping>
2 在hibernate.cfg.xml中添加该映射文件Student.hbm.xml即可
<!-- 建立对应的配置文件关联相应的数据库表 -->
<mapping resource="com/model/Student.hbm.xml"/>注意包名的写法
3 示例
//读取配置文件hibernate.cfg.xml
Configuration cfg=new Configuration().configure();
//创建SessionFactory
SessionFactory sf=cfg.configure().buildSessionFactory();
//创建session
Session session=sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sf.close();
hibernate.cfg.xml配置
hibernate.hbm2ddl.auto属性
取值: validate | update | create | create-drop
在sessionfactory创建时,自动检查数据结构,或者将数据库schema(表)的DDL导出到数据库,使用create-drop时,在显式关闭sessionfactory时,将drop掉数据库schema.
validate 加载hibernate时,验证创建数据库表结构
create 每次加载hibernate,重新创建数据库表结构
create-drop 加载hibernate时创建,退出是删除表结构
update 加载hibernate自动更新数据库结构
表名和类名不同的情况时,对表名进行配置
1 在注解中
import javax.persistence.Table;
@Table(name=”TableName”)
2 在XML文件中
<hibernate-mapping package="com.model">
<class name="Teacher" table="Teacher" >配置对应的table属性为相应的表名
<id name="id" column="id"></id>
…
</class>
</hibernate-mapping>
字段名和属性名相同,默认为
对于annotation,如果什么注解都不写的话,相当于加了注解@Basic
实体bean中所有的非static非transient的属性都可以被持久化, 除非你将其注解为@Transient.所有没有定义注解的属性等价于在其上面添加了@Basic注解. 通过 @Basic注解可以声明属性的获取策略(fetch strategy)