自己顶下来偶顶,偶顶,,偶再顶,,我快疯狂了。学java一年多了,从来没有为了这么个简单问题疯狂过。。顶顶顶。

解决方案 »

  1.   

    http://matrix.foresee.cn:8080/forum/thread.jspa?messageID=20866
    上面那个是基于Hibernate3,想想应该是很明了的了。
      

  2.   


    编写pojo类:
    java.io.Serializable;/**
     * A class that represents a row in the hibernate table. 
     * You can customize the behavior of this class by editing the class, {@link Hibernate()}.
     * WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized * by MyEclipse Hibernate tool integration.
     */
    public class qq 
        implements Serializable
    {
        public String name;
        
        public void setName(String name){
           this.name=name;
          }
        
        public String getName(){
            return name;
    }    
        public String pasd;
        public void setPasd(String pasd){
            this.pasd=pasd;
       }
      
       public Sring getPasd(){
            return pasd;
         }    public boolean equals(Object rhs)
        {
            if (rhs == null)
                return false;
            if (! (rhs instanceof Hibernate))
                return false;
            Hibernate that = (Hibernate) rhs;
            if (this.getId() != null && that.getId() != null)
            {
                if (! this.getId().equals(that.getId()))
                {
                    return false;
                }
            }
            return true;
        }    /**
         * Implementation of the hashCode method conforming to the Bloch pattern with
         * the exception of array properties (these are very unlikely primary key types).
         * @return int
         */
        public int hashCode()
        {
            if (this.hashValue == 0)
            {
                int result = 17;
                int idValue = this.getId() == null ? 0 : this.getId().hashCode();
                result = result * 37 + idValue;
                this.hashValue = result;
            }
            return this.hashValue;
        }
    }编写qq.hbm.xml映射文件:
            <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
                                "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
                                "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" ><!-- DO NOT EDIT: This is a generated file that is synchronized -->
    <!-- by MyEclipse Hibernate tool integration.                   -->
    <!-- Created Sat Apr 30 01:03:52 CST 2005                         -->
    <hibernate-mapping package="net.xiaobo">    <class name="Hibernate" table="hibernate">
            <id name="name" column="name" type="java.lang.String">
                <generator class="(自己选择)"/>
            </id>
     
            <property name="pasd" column="pasd" type="java.lang.String" />
                </class>
        
    </hibernate-mapping>编写hibernate.cfg.xml配置文件:
    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 2.0//EN"
              "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"><!-- DO NOT EDIT: This is a generated file that is synchronized -->
    <!-- by MyEclipse Hibernate tool integration.                   -->
    <hibernate-configuration>    <session-factory>
            <!-- properties -->
            <property name="connection.username">zhutouzip</property>
            <property name="connection.url">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=dan</property>
            <property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property>
            <property name="connection.password">12388321</property>
            <property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
            <property name="hibernate.show_sql">true</property>
            <!-- mapping files -->
            <mapping resource="net/xiaobo/qq.hbm.xml"/>    </session-factory></hibernate-configuration>编写一个取得sessionFactory的类:
    public class FirstSessionFactory {    /** 
         * Location of hibernate.cfg.xml file.
         * NOTICE: Location should be on the classpath as Hibernate uses
         * #resourceAsStream style lookup for its configuration file. That
         * is place the config file in a Java package - the default location
         * is the default Java package.<br><br>
         * Examples: <br>
         * <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml". 
         * CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code> 
         */
        private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";    /** Holds a single instance of Session */
        private static final ThreadLocal threadLocal = new ThreadLocal();    /** The single instance of hibernate configuration */
        private static final Configuration cfg = new Configuration();    /** The single instance of hibernate SessionFactory */
        private static net.sf.hibernate.SessionFactory sessionFactory;    /**
         * Returns the ThreadLocal Session instance.  Lazy initialize
         * the <code>SessionFactory</code> if needed.
         *
         *  @return Session
         *  @throws HibernateException
         */
        public static Session currentSession() throws HibernateException {
            Session session = (Session) threadLocal.get();        if (session == null) {
                if (sessionFactory == null) {
                    try {
                        cfg.configure();//************************(1)
                        sessionFactory = cfg.buildSessionFactory();
                    }
                    catch (Exception e) {
                        System.err.println("%%%% Error Creating SessionFactory %%%%");
                        e.printStackTrace();
                    }
                }
                session = sessionFactory.openSession();
                threadLocal.set(session);
            }        return session;
        }    /**
         *  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();
            }
        }    /**
         * Default constructor.
         */
        private FirstSessionFactory() {
        }}
    编写测试类:
    import net.sf.hibernate.*;
    import org.apache.log4j.*;
    /**
     * @author Lovenoprice
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    public class HibernateTest {
        public static void main(String[] args) {
         Hibernate person=new Hibernate();
    qq q=new qq();

    try{
    Session session=FirstSessionFactory.currentSession();
    Transaction tx=session.beginTransaction();
    session.save(qq);

    tx.commit(); 
    System.out.println("save successfully!");
    }catch(HibernateException e){
    e.printStackTrace();
    }finally{
    try{
    FirstSessionFactory.closeSession();
    }catch(HibernateException he){
    System.err.println("close session error!");
    }
    }
    }
    }
    运行HibernateTest.java
    你就可以看到效果了!其中要注意我的注释(1)处,此处是读取配置文件地点:
    (1)Configuration cfg=new Configuration().configure()是读取默认hibernate.cfg.xml文件,这个文件放到classes目录下一下继续
      

  3.   

    webapps tomcat中的目录
          | 
        myHiberApp1  偶的web主程序目录
          └─WEB-INF 
              ├─classes 
              │  │ 
              │  │ hibernate.cfg.xml 
              │  │ 
              │  └─qq-|
                         qq.class(对应表里的各个字段) 
                         qq.hbm.xml          
                    /lib 
                          hibernate2.jar 
                   ... 
      

  4.   

    (1)处可以改成:
      cfg.configure(new File("d:/project/hibernate.cfg.xml"));
    这种情况下你的hibernate.cfg.xml文件位置可以任意,你只需要将hibernate.cfg.xml文件的绝对路径作为一个File对象的参数构成一个File对象就行了!
    (2)
      也可以是cfg.configure(Document);
    其中这个Document是你用一些xml api分析的到的Document对象,这个可以看看dom
    (3)
      也可以是cfg.configure(String resource)
    这个resource是在hibernate.configuration.dtd中定义的格式,这个我没用过,不说!
    不知道楼主能否看明白!
      

  5.   

    去www.javafan.net的hibernate专栏有电子书下载
    如果你只要入门,肯定可以满足你的。  :)