熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Hibernate初學之一對多、多對一關系模型

2013-11-23 20:22:49  來源: Java開源技術 

  舉例訂單與客戶

  多對一關系模型

  · 首先是導包這個省略不寫了

  · 其次寫domain類

  分為Client Order兩個類因為是多對一的關系所以外鍵應該放在Order這個類裡

  配置文件hibernatecfgxml

  [java]

  <hibernateconfiguration>

  <sessionfactory name=foo>

  <property name=nnectiondriver_class>commysqljdbcDriver</property>

  <property name=connectionurl>jdbc:mysql:///test</property>

  <property name=connectionusername>root</property>

  <property name=connectionpassword>root</property>

  <property name=dialect>orghibernatedialectMySQLDialect</property>

  <property name=show_sql>true</property>

  <property name=hbmddlauto>update</property>

  <mapping resource=com/hbsi/domain/Clienthbmxml/>

  <mapping resource=com/hbsi/domain/Orderhbmxml/>

  </sessionfactory>

  </hibernateconfiguration>

  定義一個Client的變量

  ·然後寫配置文件Clienthbmxml

  <hibernatemapping package=comhbsidomain>

  <class name=Client table=client>//類名和表名

  <id name=id column=id>//變量和字段

  <generator class= native />//根據數據庫裡的設置自定義

  </id>

  <property name=name />

  </class>

  </hibernate mapping>

  Orderhbmxml

  <hibernatemapping>

  <class name=Order table=ordershop>

  <id name=id>

  <generator class=native />

  </id>

  <property name=shopname column=shopname />

  <property name=number column=number/>

  <property name = price column=price />

  <manytoone name=client column=client_id />

  <class>

  </hibernatemapping>

  配置文件這樣就寫完了接下來就是添加查找

  Session session=null;

  @Test

  public void add(Client clientOrder order){

  try{

  session = HibernateUtilgetSession()//獲取工具類

  sessionbeginTransaction()//開啟事務

  /*Client client = new Client()

  clientsetName(劉總

  Order order = new Order()

  ordersetShopname(航空母艦

  ordersetNumber(

  ordersetPrice(

  ordersetCt(client)

  Order order = new Order()

  ordersetShopname(航空母艦

  ordersetNumber(

  ordersetPrice(

  ordersetCt(client)*/

  sessionsave(client)

  sessionsave(order)

  sessionbeginTransaction()commit()

  }finally{

  if(session!=null){

  sessionclose()

  }

  }

  }

  public void find(int id){

  try{

  session = HibernateUtilgetSession()

  sessionbeginTransaction()

  Order order = (Order) sessionget(Orderclass id)

  Systemoutprintln(ordergetCt()getName())

  sessionbeginTransaction()commit()

  }finally{

  if(session!=null){

  sessionclose()

  }

  }

  }

  測試

  public static void main(String[] args ){

  Client client = new Client()

  clientsetName(xiao總

  Order order = new Order()

  ordersetShopname(美國

  ordersetNumber(

  ordersetPrice(

  ordersetCt(client)

  Order order = new Order()

  ordersetShopname(日本

  ordersetNumber(

  ordersetPrice(

  ordersetCt(client)

  Manyone mo = new Manyone()

  moadd(client order

  mofind(

  }

  關系模型一對多

  Clienthbmxml

  <hibernatemapping package=comhbsidomain>

  <class name=Client table=client>

  <id name=id column=id>

  <generator class = native />

  </id>

  <property name=name />

  <set name=order table=ordershop cascade=saveupdate>

  <key column=user_id />

  <onetomany class=Order  />

  </set>

  </class>

  </hibernatemapping>

  Orderhbmxml

  <hibernatemapping package=comhbsidomain>

  <class name=Order table=ordershop>

  <id name=id>

  <generator class = native />

  </id>

  <property name=shopname column=shopname />

  <property name=number column=number />

  <property name=price column=price />

  </class>

  </hibernatemapping>

  添加方法

  public void add(Client clientOrder order){

  try{

  session = HibernateUtilgetSession()

  sessionbeginTransaction()

  sessionsave(client)

  //  sessionsave(order)

  sessionbeginTransaction()commit()

  }finally{

  if(session!=null){

  sessionclose()

  }

  }

  }

  public void find(int id){

  try{

  session = HibernateUtilgetSession()

  sessionbeginTransaction()

  Client client = (Client) sessionget(Orderclass id)

  Set<Order> orders  =clientgetOrder()

  orderssize()

  sessionbeginTransaction()commit()

  }finally{

  if(session!=null){

  sessionclose()

  }

  }

  }


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28399.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.