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

Hibernate下實現數據的級聯插入

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

  在本文中主要實現數據的級聯插入下面模擬的場景是用戶有多個訂單在插入用戶的時候將用戶的訂單添加到訂單表中

  下面是User 和 Order兩個pojo類的代碼

  [java]

  public class Order {

  private int id;

  private int whoid;   // thi id of the orders User

  private User user;

  public User getUser() {

  return user;

  }

  public void setUser(User user) {

  thisuser = user;

  }

  public int getId() {

  return id;

  }

  public void setId(int id) {

  thisid = id;

  }

  public int getWhoid() {

  return whoid;

  }

  public void setWhoid(int whoid) {

  thiswhoid = whoid;

  }

  }

  public class Order {

  private int id;

  private int whoid;   // thi id of the orders User

  private User user;

  public User getUser() {

  return user;

  }

  public void setUser(User user) {

  thisuser = user;

  }

  public int getId() {

  return id;

  }

  public void setId(int id) {

  thisid = id;

  }

  public int getWhoid() {

  return whoid;

  }

  public void setWhoid(int whoid) {

  thiswhoid = whoid;

  }

  }

  [java]

  public class User {

  private int id;

  private String username;

  private String password;

  //這裡需要注意的是使用集合必須使用Set而不能用HashSet否者在插入數據的時候會拋出一個參數不匹配異常

  private Set<Order> orders;

  public Set<Order> getOrders() {

  return orders;

  }

  public void setOrders(Set<Order> orders) {

  thisorders = orders;

  }

  public int getId() {

  return id;

  }

  public void setId(int id) {

  thisid = id;

  }

  public String getUsername() {

  return username;

  }

  public void setUsername(String username) {

  thisusername = username;

  }

  public String getPassword() {

  return password;

  }

  public void setPassword(String password) {

  thispassword = password;

  }

  }

  public class User {

  private int id;

  private String username;

  private String password;

  //這裡需要注意的是使用集合必須使用Set而不能用HashSet否者在插入數據的時候會拋出一個參數不匹配異常

  private Set<Order> orders;

  public Set<Order> getOrders() {

  return orders;

  }

  public void setOrders(Set<Order> orders) {

  thisorders = orders;

  }

  public int getId() {

  return id;

  }

  public void setId(int id) {

  thisid = id;

  }

  public String getUsername() {

  return username;

  }

  public void setUsername(String username) {

  thisusername = username;

  }

  public String getPassword() {

  return password;

  }

  public void setPassword(String password) {

  thispassword = password;

  }

  }

  下面這個是UserDao這個就沒有什麼好說的了

  [java]

  import orghibernateSession;

  import orghibernateTransaction;

  import comexampledomainUser;

  public class UserDao {

  private Session session;

  public UserDao(Session session )

  {

  thissession = session;

  }

  public void savaUser(User user)

  {

  Transaction tx = sessionbeginTransaction();

  try

  {

  sessionsave(user);

  mit();

  }

  catch(Exception ex)

  {

  exprintStackTrace();

  txrollback();

  }

  }

  }

  import orghibernateSession;

  import orghibernateTransaction;

  import comexampledomainUser;

  public class UserDao {

  private Session session;

  public UserDao(Session session )

  {

  thissession = session;

  }

  public void savaUser(User user)

  {

  Transaction tx = sessionbeginTransaction();

  try

  {

  sessionsave(user);

  mit();

  }

  catch(Exception ex)

  {

  exprintStackTrace();

  txrollback();

  }

  }

  }

  下面的這兩個hbmxml文件十分重要

  Userhbmxml

  [html]

  <?xml version= encoding=UTF?>

  <!DOCTYPE hibernatemapping PUBLIC

  //Hibernate/Hibernate Mapping DTD //EN

  hiber/hibernatemappingdtd>

  <hibernatemapping>

  <class name=comexampledomainUser table=user >

  <id name=id column=id type=javalangInteger>

  <generator class=increment></generator>

  </id>

  <property name=username column=username type=javalangString></property>

  <property name=password column=password type=javalangString></property>

  <set name=orders cascade=saveupdate inverse=false >

  <key column=whoid></key>

  <onetomany class=comexampledomainOrder/>

  </set>

  </class>

  </hibernatemapping>

  <?xml version= encoding=UTF?>

  <!DOCTYPE hibernatemapping PUBLIC

  //Hibernate/Hibernate Mapping DTD //EN

  hiber/hibernatemappingdtd>

  <hibernatemapping>

  <class name=comexampledomainUser table=user >

  <id name=id column=id type=javalangInteger>

  <generator class=increment></generator>

  </id>

  <property name=username column=username type=javalangString></property>

  <property name=password column=password type=javalangString></property>

  <set name=orders cascade=saveupdate inverse=false >

  <key column=whoid></key>

  <onetomany class=comexampledomainOrder/>

  </set>

  </class>

  </hibernatemapping>

  其中要理解的就是set這個標簽這個標簽對應了User對象中的集合對象name依然是User中屬性的名字inserse為false表示了這種一對多的關系由User來維護默認值就是false那麼那一方就有責任負責之間的關聯關系說白了就是hibernate如何生成Sql來維護關聯的記錄比如說當User的狀態發生了改變這種改變會延伸到每一個關聯到這個User的Order上

  key標簽用來表明這兩個關系是通過那個屬性來連接的那麼Order表中的外鍵是whoid那麼這兩個關系是通過whoid來維護的onetomany表示一對多的關系其中class表示Set集合中的對象類型也可以這麼理解即一對多的關系多的一方的類型是什麼

  Orderhbmxml

  [html]

  <?xml version= encoding=UTF?>

  <!DOCTYPE hibernatemapping PUBLIC

  //Hibernate/Hibernate Mapping DTD //EN

  hiber/hibernatemappingdtd>

  <hibernatemapping>

  <class name=comexampledomainOrder table=Orders>

  <id name=id column=id type=javalangInteger>

  <generator class=increment></generator>

  </id>

  <manytoone name=user class=comexampledomainUser column=whoid></manytoone>

  </class>

  </hibernatemapping>

  <?xml version= encoding=UTF?>

  <!DOCTYPE hibernatemapping PUBLIC

  //Hibernate/Hibernate Mapping DTD //EN

  hiber/hibernatemappingdtd>

  <hibernatemapping>

  <class name=comexampledomainOrder table=Orders>

  <id name=id column=id type=javalangInteger>

  <generator class=increment></generator>

  </id>

  <manytoone name=user class=comexampledomainUser column=whoid></manytoone>

  </class>

  </hibernatemapping>

  其中的 manytoone表示多對一的關系name表示Order中表示用來關聯用戶的那個屬性的名稱class表示一的那一方的類型column表示關聯關系中用來連接的列這裡仍然是whoid

  下面是測試代碼

  [java]

  public class Test {

  public static void main(String args[])

  {

  User user = new User();

  usersetUsername(Neway);

  usersetPassword(admin);

  HashSet<Order> orders  = new HashSet<Order>();

  Order o = new Order();

  Order o = new Order();

  Order o = new Order();

  ordersadd(o);

  ordersadd(o);

  ordersadd(o);

  usersetOrders(orders);

  Session session = HibernateSessionFactorygetSession();

  UserDao dao = new UserDao(session);

  daosavaUser(user);

  }

  }

  public class Test {

  public static void main(String args[])

  {

  User user = new User();

  usersetUsername(Neway);

  usersetPassword(admin);

  HashSet<Order> orders  = new HashSet<Order>();

  Order o = new Order();

  Order o = new Order();

  Order o = new Order();

  ordersadd(o);

  ordersadd(o);

  ordersadd(o);

  usersetOrders(orders);

  Session session = HibernateSessionFactorygetSession();

  UserDao dao = new UserDao(session);

  daosavaUser(user);

  }

  }


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