Hibernate(3.6)之初识
来源:互联网 发布:javascript 引号 转义 编辑:程序博客网 时间:2024/06/05 14:31
一 环境:W7+Eclipse3.6
二 所用Hibernate版本:hibernate-distribution-3.6.0.Final
工程目录结构如下
三 参考资料
1 Hibernate3.6的Annotation问题
http://mayuchuan99.blog.163.com/blog/static/320023442011029438354/
hibernate3.6之前的版本使用Annotation,还需要下载Annotation库,需要添加ejb3-persistence.jar hibernate-annotations.jar hibernate-commons-annotations.jar,而获得SessionFactory必须以下这样写
在hibernate3.6的这个版本中,Annotation类库集成到了hibernate3.6,所以不在需要添加hibernate-annotations.jar hibernate-commons-annotations.jar等类库了。但是必须添加hibernate-jpa-2.0-api-1.0.0.Final.jar。
hibernate 3.6要获取一个SessionFactory,我可以直接像用xml来配置实体与数据库表的映射关系那样。代码如下:
四 具体代码
1 hibernate.cfg.xml
2 Student.hbm.xml
3 Student.java
4 HibernateUtil.java
5 Test.java
6 mysql.sql
二 所用Hibernate版本:hibernate-distribution-3.6.0.Final
工程目录结构如下
三 参考资料
1 Hibernate3.6的Annotation问题
http://mayuchuan99.blog.163.com/blog/static/320023442011029438354/
hibernate3.6之前的版本使用Annotation,还需要下载Annotation库,需要添加ejb3-persistence.jar hibernate-annotations.jar hibernate-commons-annotations.jar,而获得SessionFactory必须以下这样写
- Configuration cfg=new AnnotationConfiguration();
- SessionFactory s=cfg.configure().buildSessionFactory();
在hibernate3.6的这个版本中,Annotation类库集成到了hibernate3.6,所以不在需要添加hibernate-annotations.jar hibernate-commons-annotations.jar等类库了。但是必须添加hibernate-jpa-2.0-api-1.0.0.Final.jar。
hibernate 3.6要获取一个SessionFactory,我可以直接像用xml来配置实体与数据库表的映射关系那样。代码如下:
- Configuration cfg=new Configuration();
- SessionFactory s=cfg.configure().buildSessionFactory();
四 具体代码
1 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">com.mysql.jdbc.Driver</property>
- <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
- <property name="connection.username">root</property>
- <property name="connection.password">root</property>
- <!-- JDBC connection pool (use the built-in)
- <property name="connection.pool_size">1</property>
- -->
- <!-- 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>
- -->
- <!-- Disable the second-level cache -->
- <property name="cache.provider_class">org.hibernate.cache.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="org/hibernate/model/Student.hbm.xml"/>
- -->
- <mapping class="org.hibernate.model.Student"/>
- </session-factory>
- </hibernate-configuration>
2 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 >
- <class name="org.hibernate.model.Student">
- <id name="id" column="id">
- <generator class="native"/>
- </id>
- <property name="name"/>
- <property name="age"/>
- </class>
- </hibernate-mapping>
3 Student.java
- @Entity
- public class Student {
- @Id
- @GeneratedValue
- private int id;
- private String name;
- private int age;
- //get set
- }
4 HibernateUtil.java
- package org.hibernate.model;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- public class HibernateUtil {
- private static final SessionFactory sessionFactory = buildSessionFactory();
- private static SessionFactory buildSessionFactory() {
- try {
- // Create the SessionFactory from hibernate.cfg.xml
- return new Configuration().configure().buildSessionFactory();
- }catch (Throwable ex) {
- // Make sure you log the exception, as it might be swallowed
- System.err.println("Initial SessionFactory creation failed." + ex);
- throw new ExceptionInInitializerError(ex);
- }
- }
- public static SessionFactory getSessionFactory() {
- return sessionFactory;
- }
- }
5 Test.java
- import org.hibernate.Session;
- public class Test {
- public static void main(String[] args) {
- Student s = new Student();
- // 设置了ID的自动递增,就不用在指定ID值
- // s.setId(1);
- s.setName("zhangsan");
- s.setAge(8);
- Session session = HibernateUtil.getSessionFactory().getCurrentSession();
- session.beginTransaction();
- session.save(s);
- session.getTransaction().commit();
- HibernateUtil.getSessionFactory().close();
- }
- }
6 mysql.sql
- DROP TABLE IF EXISTS `student`;
- CREATE TABLE `student` (
- `id` int(20) NOT NULL AUTO_INCREMENT,
- `name` varchar(20) DEFAULT NULL,
- `age` int(3) DEFAULT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
- INSERT INTO `student` VALUES ('1', 'zhangsan', '8');
0 0
- Hibernate(3.6)之初识
- Hibernate之hibernate初识
- 千里之行,始于足下之:初识Hibernate
- 初识Hibernate之环境搭建
- 初识Hibernate之关联映射
- 初识Hibernate之继承映射
- Hibernate框架学习之初识Hibernate
- Java框架之Hibernate--初识Hibernate
- Hibernate征途(一)之初识
- Snail—Hibernate之初识配置
- Hibernate 之 初识 uuid 和 native
- 初识Hibernate之增删改查
- 初识Hibernate之理解持久化类
- 初识Hibernate之关联映射(一)
- 初识Hibernate之关联映射(二)
- 初识Hibernate之关联映射(二)
- 初识Hibernate之关联映射(一)
- 初识Hibernate之理解持久化类
- 「MICCAI 2016」Reading Note
- 用java实现自定义随机数发生器
- gdb调试多文件程序
- hibernate3.3.2学习笔记---将图片从本地读到数据库里
- hdu 1394 母函数入门题
- Hibernate(3.6)之初识
- css required,focus,valid和invalid介绍
- hdu2196 (树的直径)
- 软导作业
- MySql sqlstate代码大全
- C#中属性PropertyInfo的使用
- 【openjudge 计算概论(A)】[基础编程练习1]
- 如果一个进程出了问题,怎么判断它是否运行?能不能重启这个进程?尝试使用守护进程来操作
- Codeforces 732C-Sanatorium