论坛首页 编程语言技术论坛

hibernate 单向一对多映射

浏览 3352 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-07-20  
好久没用j2ee开发呢,昨天写呢一个hibernate的映射,几次出现异常,翻呢一遍书熟悉呢一下才搞定,这里把几种常用的映射放上来,希望大家方便查找:
作者:waitbin
一:hibernate 单向一对多映射 一个team 对应多个student;
(一):hibernate.cfg.xml


    PUBLIC "-//hibernate/hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>

       
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">admin</property>
       

       
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
          
               <mapping resource="hibernate/mapping/Team.hbm.xml">
               <mapping resource="hibernate/mapping/Student.hbm.xml">
    </mapping>
   
</mapping>
(二);team.hbm.xml


    "-//hibernate/hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="hibernate.mapping">
    <class table="team" name="Team">
        <id type="java.lang.Integer" column="id" name="id">
            <generator class="identity"></generator>
        </id>
        <property type="java.lang.String" column="name" name="name"></property>
       
            <key column="t_id"></key>
            <one-to-many class="hibernate.mapping.Student">
        </one-to-many>
   
</class>
(三):student.hb,.xml


    "-//hibernate/hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
    <hibernate-mapping package="hibernate.mapping">
        <class table="student" name="Student">
           <id type="java.lang.Integer" column="id" name="id">
                 <generator class="identity"></generator>
           </id>
                 <property type="java.lang.String" column="name" name="name">
         </property>
    </class>

(四)测试类
package hibernate.dao;

import hibernate.mapping.Student;
import hibernate.mapping.Team;

import org.hibernate.Session;
import org.hibernate.Transaction;


public class One_manyDAO {
    public static void main(String[] args) {
        Session session = HibernateSessionFactory.getSession();
        Transaction ts = session.beginTransaction();
        Team t = new Team();
        t.setName("0102class");
        Student s = new Student();
        s.setName("zhangsan");
        t.getStudent().add(s);
        session.save(t);
        ts.commit();

    }
}
(五)HibernateSessionFactory封装呢Session
package hibernate.dao;

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 threadLocal = new ThreadLocal();
    private static Configuration configuration = new Configuration();
    private static org.hibernate.SessionFactory sessionFactory;
    private static String configFile = CONFIG_FILE_LOCATION;

    private HibernateSessionFactory() {
    }

    /** 
    #      * Returns the ThreadLocal Session instance.  Lazy initialize 
    #      * the SessionFactory 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;
    }

}


<hibernate-configuration><session-factory><hibernate-mapping package="hibernate.mapping"><hibernate-mapping package="hibernate.mapping">
</hibernate-mapping></hibernate-mapping></session-factory></hibernate-configuration></hibernate-mapping></hibernate-mapping></session-factory></hibernate-configuration>
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics