最近在学习Hibernate,先把学习的过程记录一下,方便自己以后复习.

1.使用工具

MyEclipse 10

2.

1)新建Java程序

2)右键程序,选择MyEclipse-> Add Hibernate Capabilities.

会自动生成HibernateSessionFactory.java文件和hibernate.cfg.xml文件

package com.water.testhibernate;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration; /**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory { /**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION; static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
} /**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
} return session;
} /**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
} /**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null); if (session != null) {
session.close();
}
} /**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
} /**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
} /**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
} }

创建Grade文件

public class Grade implements Serializable {
private int gid;
private String gname;
private String gdesc;
private Set<Student> students; public int getGid() {
return gid;
} public void setGid(int gid) {
this.gid = gid;
} public String getGname() {
return gname;
} public void setGname(String gname) {
this.gname = gname;
} public String getGdesc() {
return gdesc;
} public void setGdesc(String gdesc) {
this.gdesc = gdesc;
} public Grade() {
super();
} public Set<Student> getStudents() {
return students;
} public void setStudents(Set<Student> students) {
this.students = students;
} public Grade(int gid, String gname, String gdesc) {
super();
this.gid = gid;
this.gname = gname;
this.gdesc = gdesc;
} public Grade(String gname, String gdesc) {
super();
this.gname = gname;
this.gdesc = gdesc;
} }

创建Grade.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="com.water.model.Grade" table="grade">
<id name="gid" column="gid" type="java.lang.Integer">
<generator class="increment"></generator>
</id>
<property name="gname" type="java.lang.String">
<column name="gname" length="20" not-null="true"></column>
</property>
<property name="gdesc">
<column name="gdesc"></column>
</property>
<!-- 配置一对多关联关系 -->
<set name="students" table="student">
<key column="gid"></key>
<one-to-many class="com.water.model.Student"/>
</set>
</class>
</hibernate-mapping>

创建Student文件

public class Student implements Serializable {
private int sid;
private String sname;
private String sex;
// 在多方定义一个一方的引用
private Grade grade; public Grade getGrade() {
return grade;
} public void setGrade(Grade grade) {
this.grade = grade;
} public int getSid() {
return sid;
} public void setSid(int sid) {
this.sid = sid;
} public String getSname() {
return sname;
} public void setSname(String sname) {
this.sname = sname;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public Student() {
super();
} public Student(String sname, String sex) {
super();
this.sname = sname;
this.sex = sex;
} }

创建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="com.water.model.Student" table="student">
<id name="sid" column="sid" type="java.lang.Integer">
<generator class="increment"></generator>
</id>
<property name="sname" type="java.lang.String">
<column name="sname" length="20" not-null="true"></column>
</property>
<property name="sex">
<column name="sex"></column>
</property>
<!-- 配置多对一关联关系 -->
<!-- <many-to-one name="grade" class="com.water.model.Grade" column="gid" cascade="all"></many-to-one> -->
</class>
</hibernate-mapping>

最后hibernate.cfg.xml文件的配置如下:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration> <session-factory> <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="show_sql">true</property>
<!-- create and update the database automaticlly -->
<property name="hbm2ddl.auto">update</property>
<property name="myeclipse.connection.profile">ms_sql</property>
<property name="connection.url">jdbc:mysql://localhost:3306/mysql</property> <mapping resource="com/water/model/Student.hbm.xml" />
<mapping resource="com/water/model/Grade.hbm.xml" />
</session-factory> </hibernate-configuration>

<property name="connection.username">root</property>数据库用户名

<property name="connection.password">root</property> 数据库密码

<property name="connection.url">jdbc:mysql://localhost:3306/mysql</property> mysql为数据库名

创建测试类Test.java

public class Test {
public static void main(String[] args) {
add();
} public static void add(){
Grade g=new Grade("software developer", "Java Android");
Student stu1=new Student("zhasan", "boy");
Student stu2=new Student("lisi", "girl"); Session session=HibernateSessionFactory.getSession();
Transaction tx=session.beginTransaction();
session.save(g);
session.save(stu1);
session.save(stu2);
tx.commit();
HibernateSessionFactory.closeSession();
}
}

运行后的效果如下:

说明已经插入成功.

MyEclipse Hibernate 学习总结的更多相关文章

  1. Hibernate学习之——搭建log4j日志环境

    昨天讲了Hibernate开发环境的搭建以及实现一个Hibernate的基础示例,但是你会发现运行输出只有sql语句,很多输出信息都看不见.这是因为用到的是slf4j-nop-1.6.1.jar的实现 ...

  2. Hibernate学习笔记(二)

    2016/4/22 23:19:44 Hibernate学习笔记(二) 1.1 Hibernate的持久化类状态 1.1.1 Hibernate的持久化类状态 持久化:就是一个实体类与数据库表建立了映 ...

  3. Hibernate学习笔记(一)

    2016/4/18 19:58:58 Hibernate学习笔记(一) 1.Hibernate框架的概述: 就是一个持久层的ORM框架. ORM:对象关系映射.将Java中实体对象与关系型数据库中表建 ...

  4. Hibernate 学习笔记一

    Hibernate 学习笔记一 今天学习了hibernate的一点入门知识,主要是配置domain对象和表的关系映射,hibernate的一些常用的配置,以及对应的一个向数据库插入数据的小例子.期间碰 ...

  5. MyEclipse Spring 学习总结三 SpringMVC

    MyEclipse Spring 学习总结三 SpringMVC 一.SpringMVC原理 1.Springmvc 框架介绍 1)Spring 框架停工了构建Web应用程序的全功能MVC模块.Spr ...

  6. Hibernate学习笔记-Hibernate HQL查询

    Session是持久层操作的基础,相当于JDBC中的Connection,通过Session会话来保存.更新.查找数据.session是Hibernate运作的中心,对象的生命周期.事务的管理.数据库 ...

  7. Hibernate 报错:this project is not a myeclipse hibernate project . assuming hibernate 3 cap

    问题描述:  web 项目中 打开 hibernate.cfg.xml 文件时 提示:The project is not a myEclipse hibernate project. 并且:在 db ...

  8. 我的hibernate学习记录(二)

    通过上一篇文章我的hibernate学习记录(一)基本上的入门了hibernate,但是,里面还有还多东西是通过迷迷糊糊的记忆,或者说copy直接弄进去的,所以这篇文章就需要对上篇的一些文件.对象进行 ...

  9. Hibernate学习(二)关系映射----基于外键的单向一对一

    事实上,单向1-1与N-1的实质是相同的,1-1是N-1的特例,单向1-1与N-1的映射配置也非常相似.只需要将原来的many-to-one元素增加unique="true"属性, ...

随机推荐

  1. 【转】使用Xcode 6将你的项目本地化

    原文转自:http://www.cocoachina.com/ios/20141004/9827.html iOS和OSX支持40种语言的本地化,Xcode无疑为这一过程提供了强有力的支持.苹果将这一 ...

  2. 六、CCLayer

    一个游戏中可以有很多个场景,每个场景里面又可能包含有多个图层,这里的图层一般就是CCLayer对象.CCLayer本身几乎没什么功能,对比CCNode,CCLayer可用于接收触摸和加速计输入.其实, ...

  3. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  4. CSS 实现:checkbox

    <div class="wrap"> <label>性别:</label> <div class="cb-wrap"& ...

  5. 《JS高程》引用类型学习笔记

    2月圆满的结束了,结束之前是如凤凰般的涅槃.一边上班,一边搞科研的忙碌有点让人透不过气,心会不由得浮躁起来.但是,无论什么事情,只要充满耐心.专心去做,总会朝好的方向发展,心态真的很重要.Anyway ...

  6. input type=file

    (1)首先来说一下,如何让 <input type='file' >成为你想要的模样. 最简单的方法就是在让<input type='file' >的透明度为0(完全透明),然 ...

  7. spring ioc 原理 spring aop原理

    大家一直都说spring的IOC如何如何的强大,其实我倒觉得不是IOC如何的强大,说白了IOC其实也非常的简单.我们先从IOC说起,这个概念其实是从我们平常new一个对象的对立面来说的,我们平常使用对 ...

  8. Apache模块管理

    Apache是一个模块化设计的服务,核心只包含主要功能,扩展功能通过模块实现,不同模块可以被静态的编辑进程序,也可以动态加载. # /usr/local/apache/bin/httpd -M  查看 ...

  9. POJ1236 Network of Schools (强连通)(缩点)

                                                                Network of Schools Time Limit: 1000MS   ...

  10. 使用jquery插件实现图片延迟加载技术(懒加载)

    有时我们看到一些大型网站,页面如果有很多图片的时候,当你滚动到相应的行时,当前行的图片才即时加载的,这样子的话页面在打开只加可视区域的图片,而其它隐藏的图片则不加载,一定程序上加快了页面加载的速度,对 ...