hibernate 單向一對多對映

2021-08-22 09:41:18 字數 2531 閱讀 8116

好久沒用j2ee開發呢,昨天寫呢乙個hibernate的對映,幾次出現異常,翻呢一遍書熟悉呢一下才搞定,這裡把幾種常用的對映放上來,希望大家方便查詢:

一:hibernate 單向一對多對映 乙個team 對應多個student;

(一):hibernate.cfg.xml

public "-//hibernate/hibernate configuration dtd//en"

"">

jdbc:mysql://localhost:3306/test

com.mysql.jdbc.driver

root

admin

org.hibernate.dialect.mysqldialect

true

org.hibernate.transaction.jdbctransactionfactory

(二);team.hbm.xml

(三):student.hb,.xml

(四)測試類

package hibernate.dao;

import org.hibernate.session;

import org.hibernate.transaction;

public class one_manydao

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

#  */

public class hibernatesessionfactory

/** 

#      * returns the threadlocal session instance.  lazy initialize 

#      * the sessionfactory if needed. 

#      * 

#      *  @return session 

#      *  @throws hibernateexception 

#      */

public static session getsession() throws hibernateexception

session = (sessionfactory != null) ? sessionfactory.opensession()

: null;

threadlocal.set(session);

}return session;

}/** 

#      *  rebuild hibernate session factory 

#      * 

#      */

public static void rebuildsessionfactory() catch (exception e)

}/** 

#      *  close the single hibernate session instance. 

#      * 

#      *  @throws hibernateexception 

#      */

public static void closesession() throws hibernateexception

}/** 

#      *  return session factory 

#      * 

#      */

public static org.hibernate.sessionfactory getsessionfactory()

/** 

#      *  return session factory 

#      * 

#      *  session factory will be rebuilded in the next call 

#      */

public static void setconfigfile(string configfile)

/** 

#      *  return hibernate configuration 

#      * 

#      */

public static configuration getconfiguration()

}

hibernate之關於一對多單向關聯對映

基於外來鍵的一對多關聯對映!一對多,group 組 對於person 人 乙個組可以有多個人 ok?hibernate主要有兩種配置方法,一種是annotations 一種是xml!下面是annotations的配置方法!group 類 entity table name t group publi...

hibernate單向一對多和雙向一對多

單向一對多 例如有部門封裝類 private int deptno private string deptname private string location 有職員封裝類 private int empno private string empname private dept dept 在多...

Hibernate筆記整理 一對多(單向)

hibernate 一對多關聯和多對一關聯在實際應用中式非常普遍的。例如乙個會員 member 可以有多個訂單 order 而每個訂單只能屬於某個特定的會員,這便是乙個典型的一對多關聯。本示例要用到的兩個pojo類如下 public class member public class order 會...