一個完整的hibernate的one-to-many的例子
阿新 • • 發佈:2019-02-08
前段時間一直在研究hibernate的one-to-many,看了不少資料也在本論壇上求教過,但由於本人對Hibernate研究的時間不是很長,所以花了不少時間和精力.昨天終於弄出來了,現在與大家分享,希望對初學者有幫助!
1、設定資料庫,本人使用的資料庫是Oracle 9i
設定hibernate.properties
## Oracle
hibernate.dialect net.sf.hibernate.dialect.Oracle9Dialect
#hibernate.dialect net.sf.hibernate.dialect.OracleDialect
hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
hibernate.connection.username sa
hibernate.connection.password sa
hibernate.connection.url jdbc:oracle:thin:@xxx.xxx.xxx.xxx:1521:myoracle
2、假設一個使用者可以同時有幾個帳戶,能通過帳戶對應到使用者,即為一個雙向的one-to-many.這是引用我在論壇上發表的文章的回覆,很謝謝geniouc給我的參考。
3、javaBean: customer & account
4、xml配置檔案中:(關係的建立)
Account.hbm.xml
Customer.hbm.xml
5、測試程式,Test.java
1、設定資料庫,本人使用的資料庫是Oracle 9i
設定hibernate.properties
## Oracle
hibernate.dialect net.sf.hibernate.dialect.Oracle9Dialect
#hibernate.dialect net.sf.hibernate.dialect.OracleDialect
hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
hibernate.connection.username sa
hibernate.connection.password sa
hibernate.connection.url jdbc:oracle:thin:@xxx.xxx.xxx.xxx:1521:myoracle
2、假設一個使用者可以同時有幾個帳戶,能通過帳戶對應到使用者,即為一個雙向的one-to-many.這是引用我在論壇上發表的文章的回覆,很謝謝geniouc給我的參考。
3、javaBean: customer & account
- public
- private cid;
- private custName;
- private Set accounts= new HashSet();
- public Customer(){
- }
- public getCid() {
- return cid;
- }
- public void setCid( cid) {
- this.cid = cid;
- }
- public getCustName() {
- return custName;
- }
- public void setCustName( custName) {
- this.custName = custName;
- }
- public Set getAccounts(){
- return accounts;
- }
- public void setAccounts(Set accounts){
- this.accounts = accounts;
- }
- }
- public class Account {
- private long aid;
- private accNumber;
- private Customer customer;
- public Account(){
- }
- public long getAid() {
- return aid;
- }
- public void setAid(long aid) {
- this.aid = aid;
- }
- public getAccNumber() {
- return accNumber;
- }
- public void setAccNumber( accNumber) {
- this.accNumber = accNumber;
- }
- public Customer getCustomer(){
- return customer;
- }
- public void setCustomer(Customer customer){
- this.customer = customer;
- }
- }
4、xml配置檔案中:(關係的建立)
Account.hbm.xml
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping
- PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
- <hibernate-mapping>
- <class name="one2many.Account" table="Account">
- <id name="aid" type="long" column="aid" unsaved-value="0">
- <generator class="increment"/>
- </id>
- <property name="accNumber" type="string"/>
- <many-to-one name="customer" column="cidForCustomer"/>
- </class>
- </hibernate-mapping>
Customer.hbm.xml
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping
- PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
- <hibernate-mapping>
- <class name="one2many.Customer" table="Customer">
- <id name="cid" type="string" column="cid">
- <generator class="assigned"/>
- </id>
- <property name="custName" type="string"/>
- <set name="accounts" lazy="true" inverse="true" cascade="all" >
- <key column="cidForCustomer"/>
- <one-to-many class="one2many/Account"/>
- </set>
- </class>
- </hibernate-mapping>
5、測試程式,Test.java
- package one2many;
- import net.sf.hibernate.*;
- import net.sf.hibernate.cfg.*;
- public class Test {
- public static void main([] args) throws HibernateException {
- Configuration conf= new Configuration();
- conf.addClass(Account.class);
- conf.addClass(Customer.class);
- SessionFactory sessionFactory = conf.buildSessionFactory();
- Customer cust = new Customer();
- cust.setCid("aa");
- cust.setCustName("Kelvin");
- Account acc = new Account();
- acc.setAccNumber("acc");
- acc.setCustomer(cust);
- Account acc1 = new Account();
- acc1.setAccNumber("acc1");
- acc1.setCustomer(cust);
- cust.getAccounts().add(acc);
- cust.getAccounts().add(acc1);
- Session session = sessionFactory.openSession();
- Transaction tx= session.beginTransaction();
- session.save(cust);
- tx.commit();
- session.close();
- sessionFactory.close();
- }