---
title: "Hibernate起步尝试"
author: "dr2tr"
date: "2006-09-08T16:53:00Z"
tags: []
slug: "hibernate"
draft: false
lastmod: "2006-09-08T16:53:00Z"
id: 233435
url: "https://dr2tr.iteye.com/blog/233435"
---

首先是寻找使用Hibernate3.0的Eclipse插件。官方站上提供了Hibernate Tools，这个东西看Manual是很不错的，但是实际用起来，却总是没能成功（也许是版本问题，我用Eclipse3.1.2，还有一个Lomboz，试了这个tool的3.1Beta和3.2beta，要么就是根本没有相关文件生成，要么就是缺文件，原因不明）。经过一天多的尝试，决定暂时先放弃这个工具。

后来用的是一个叫Hibernate Synchronizer的工具，这个终于摸出点头绪。（需要注意的是，网上很多文章给出的它的安装地址是不对的，现在可以到sourceforge上查到）。

接着是看了一些基本的东西。有关ORM的概念是知道的，但是实际用还是有些问题。比如在Hibernate的hbm文件中，似乎必须要指定一个关键字，如果没有，似乎就不行。具体因为我才刚开始用，所以以后应该会了解得多些。

还有一个是有关于thread-local变量的概念。在Java里有直接的java.lang.ThreadLocal类存在。这个类，顾名思义，是线程局部类，实现原理是在每个线程中维护一个该类的副本，并能够在各线程中单独修改该值。这个类在牵涉到线程变量共享冲突的时候是很有用的。在Hibernate中用到，主要是在获取session的时候，有一种简单的工具类写法：

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.hibernate.\*;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import org.hibernate.cfg.\*;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public class HibernateUtil ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public static final SessionFactory sessionFactory;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    static ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        try ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            // Create the SessionFactory from hibernate.cfg.xml  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            sessionFactory = new Configuration().configure().buildSessionFactory();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        } catch (Throwable ex) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            // Make sure you log the exception, as it might be swallowed  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            System.err.println("Initial SessionFactory creation failed." + ex);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            throw new ExceptionInInitializerError(ex);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public static final ThreadLocal session = new ThreadLocal();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public static Session currentSession() throws HibernateException ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Session s = (Session) session.get();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        // Open a new Session, if this thread has none yet  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        if (s == null) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            s = sessionFactory.openSession();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            // Store it in the ThreadLocal variable  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            session.set(s);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return s;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public static void closeSession() throws HibernateException ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Session s = (Session) session.get();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        if (s != null)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            s.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        session.set(null);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

（上面的 代码摘自Hibernate3手册）。在上面的代码中，

public static final ThreadLocal session = new ThreadLocal();

的使用，使得在多个Thread使用这个工具类时，不会发生session共享冲突的问题。

 

这段时间打算体会一下Hibernate，接下来我会整理出一些基本操作的方法