`

Hibernate(3.6)之初识

阅读更多
一 环境: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必须以下这样写
   
 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');

  • 大小: 45.7 KB
分享到:
评论

相关推荐

    低清版 大型门户网站是这样炼成的.pdf

    2.1 初识mvc新秀struts 2 33 2.1.1 mvc概述 33 .2.1.2 struts 2的mvc实现 35 2.1.3 struts 2的基本组成 36 2.1.4 struts 2的常用类介绍 38 2.1.5 struts 2的业务控制器action实现 39 2.1.6 struts 2的处理...

    JAVA程序开发大全---上半部分

    第1章 初识MyEclipse 1 1.1 MyEclipse简介 1 1.2 MyEclipse的安装 1 1.2.1 JDK的安装与配置 1 1.2.2 MyEclipse 7.0的安装和运行 4 1.3 获取和阅读MyEclipse帮助文档 5 1.4 本章小结 5 第2章 MyEclipse集成开发环境的...

    Java Web编程宝典-十年典藏版.pdf.part2(共2个)

    主要包括Java Web开发环境、JSP语法、JSP内置对象、Java Bean技术、Servlet技术、EL与JSTL标签库、数据库应用开发、初识Struts2基础、揭密Struts2高级技术、Hib锄劬e技术入门、Hibernate高级应用、Spring核心之IoC、...

    Struts2 in action中文版

    第2章 初识Struts 2 16 2.1 声明性架构 16 2.1.1 两种配置 16 2.1.2 声明架构的两种方式 17 2.1.3 智能默认值 20 2.2 简单的HelloWorld示例 20 2.2.1 部署示例应用程序 20 2.2.2 探索HelloWorld应用程序 24 2.3 使用...

    深入浅出Struts2(附源码)

    第2章初识Struts 14 2.1 Struts的优点 14 2.2 Struts的动作处理流程 15 2.3 拦截器 17 2.4 Struts配置文件 18 2.4.1 struts.xml文件 19 2.4.2 struts.properties文件 26 2.5 Struts应用程序示例 26 2.5.1 ...

    深入浅出Struts 2 .pdf(原书扫描版) part 1

    3.6 异常处理:exception-mapping元素 47 3.7 通配符映射 48 3.8 动态方法调用 51 3.9 对动作类进行测试 51 3.10 小结 51 第4章 OGNL 52 4.1 Value Stack栈 52 4.2 读取Object Stack里的对象的属性 53 4.3 读取...

Global site tag (gtag.js) - Google Analytics