Hibernate集合映射与关联关系

1.利用hibernate配置文件生成信息表并系统生成dao包和Bean包及配置文件信息

2.studentJavaBean文件里添加Scoremap集合并在其*.hbm.xml中添加与Score联系的配置信息

3.*.hbm.xml配置文件中进行映射,关联

双向

<set name="student"table="class">

       <key column="classNo"></key>

       <one-to-many class="Bean.Student"/>

       </set>

单向

<many-to-one name="class"class="Bean.Class" column="classNo"></many-to-one>

4.建立对数据库操作的execute包,包含多对一,一对多情况下的对成绩的增删,学生班级的增删该查以及使用map集合对学生成绩的增删操作

Class.java

public  class Class implementsjava.io.Serializable {

 

    // Fields

 

    private Integer classNo;

    private String className;

    private String classDate;

   

    //单向时删除

  //  privateSet<Student> student = new HashSet<Student>();

 

Score.java

public  class Score implementsjava.io.Serializable {

 

    // Fields

 

    private Integer id;

    private String studentName;

    private String studentScore ;

Student.java

public  class Student implementsjava.io.Serializable {

 

    // Fields

 

    private Integer studentNo;

    private String studentName;

    private String studentSex;

    private String studentBirthday;

    private Class studentClass;

    private Map<String,String> Score= new HashMap<String,String>();

 

    // Constructors

 Dao包中以class1.java为例

   

class1.java

package Dao;

 

importorg.hibernate.Session;

 

/**

 * Data access object (DAO) for domain model

 * @author MyEclipse Persistence Tools

 */

public class class1 implements IBaseHibernateDAO {

   

    public Session getSession() {

        //FIXME: Implement this method

        return null;

    }

   

}
根据系统自动创建

HibernateSessionFactory.java

importorg.hibernate.HibernateException;

importorg.hibernate.Session;

importorg.hibernate.cfg.Configuration;

importorg.hibernate.service.ServiceRegistry;

importorg.hibernate.service.ServiceRegistryBuilder;

 

/**

 * Configures and provides access to Hibernatesessions, 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 classpathas Hibernate uses 

     * #resourceAsStream style lookup for itsconfiguration file.

     * The default classpath location ofthe hibernate config file is

     * in the default package. Use#setConfigFile() to update

     * the location of the configuration filefor the current session.  

     */

    private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();

    private static org.hibernate.SessionFactory sessionFactory;

    private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml"; 

    private static Configuration configuration = new Configuration();

    private static ServiceRegistry serviceRegistry;

    private static String configFile = CONFIG_FILE_LOCATION;

    static {

    try {

            configuration.configure(configFile);

            serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();

            sessionFactory = configuration.buildSessionFactory(serviceRegistry);

        } catch (Exception e) {

            System.err.println("%%%% Error Creating SessionFactory%%%%");

            e.printStackTrace();

        }

    }

    private HibernateSessionFactory() {

    }

   

    /**

     * Returns the ThreadLocal Sessioninstance.  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();

            serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();

            sessionFactory = configuration.buildSessionFactory(serviceRegistry);

        } 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.SessionFactorygetSessionFactory() {

        return sessionFactory;

    }

     public static void setConfigFile(String configFile){ 

         HibernateSessionFactory.configFile = configFile; 

           sessionFactory = null; 

       } 

   

   

    /**

     * return hibernate configuration

     *

     */

    public static Configuration getConfiguration() {

        return configuration;

    }

 

}

Classs.hbm.xml


Score.hbm.xml


Student.hbm.xml

源码:

https://download.csdn.net/download/qq_40567438/10468253