myeclipse hbm2java_myeclipse试用小记----Hibernate多对一双向关联(2)
myeclipse試用小記----Hibernate多對一雙向關聯(2)
在上篇文章“myeclipse試用小記----Hibernate多對一單向關聯(1)”中,講到了“Hibernate多對一單向關聯”,現在我打算把這個做成雙向的,也就是多對一雙向關聯,看看myeclipse是如何實現的。
環境、數據庫還和上篇文章的一樣。只是表中的數據清空了。
注意:對于有外鍵的表,清空數據有兩種方法:第一是先drop掉外鍵約束后,進行清除。第二種是先清除從表數據(orders),然后清除主表的數據(customers),具體我及不說了,有空我在寫寫數據庫外鍵約束的方面的文章,這點也很重要的。
步驟
1、清除orders、customers兩表數據。
2、用myeclipse新建web工程sx_d2y,加入hibernate支持。
3、同時選中orders、customers生成實體Bean和mapping文件,并對mapping做小的改動。
4、寫兩個測試類,分別保存Orders對象和Customers的實體對象,看看能否保存到數據庫中。
廢話不說了,看過程吧!
一、通過myeclipse生成實體和配置文件:
Customers.java
---------------------
public class Customers implements java.io.Serializable {
// Fields
private Long id;
private String name;
private Set orderses = new HashSet(0);
// Constructors
/** default constructor */
public Customers() {
}
/** full constructor */
public Customers(String name, Set orderses) {
this.name = name;
this.orderses = orderses;
}
// Property accessors
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Set getOrderses() {
return this.orderses;
}
public void setOrderses(Set orderses) {
this.orderses = orderses;
}
}
Orders.java
---------------------
public class Orders implements java.io.Serializable {
// Fields
private Long id;
private Customers customers;
private String orderNumber;
// Constructors
/** default constructor */
public Orders() {
}
/** minimal constructor */
public Orders(Customers customers) {
this.customers = customers;
}
/** full constructor */
public Orders(Customers customers, String orderNumber) {
this.customers = customers;
this.orderNumber = orderNumber;
}
// Property accessors
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public Customers getCustomers() {
return this.customers;
}
public void setCustomers(Customers customers) {
this.customers = customers;
}
public String getOrderNumber() {
return this.orderNumber;
}
public void setOrderNumber(String orderNumber) {
this.orderNumber = orderNumber;
}
}
Customers.hbm.xml
-------------------
/p>
"[url]http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd[/url]">
Orders.hbm.xml
-------------------
/p>
"[url]http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd[/url]">
二、寫測試類進行測試
在測試之前,先清空這兩個關聯表的數據。
D:\mysql-5.0.37-win32\bin>mysql -uroot -pleizhimin
Welcome to the MySQL monitor. ?Commands end with ; or \g.
Your MySQL connection id is 32
Server version: 5.0.37-community MySQL Community Edition (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use testdb;
Database changed
mysql> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| card ? ? ? ? ? ? |
| customers ? ? ? ?|
| orders ? ? ? ? ? |
| person ? ? ? ? ? |
| t_user ? ? ? ? ? |
| user ? ? ? ? ? ? |
+------------------+
6 rows in set (0.00 sec)
mysql> delete from orders;
Query OK, 2 rows affected (0.03 sec)
mysql> delete from customers;
Query OK, 1 row affected (0.03 sec)
看清了,我已經干掉這個兩個表的所有數據了。
然后,我寫一個測試類TestbyOrder,從保存Orders實體對象,看能否級聯保存相關的Customers對象:
package org.lavasoft;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class TestbyOrder {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
if(HibernateSessionFactory.getSession()==null)System.out.println("null");
Session session=HibernateSessionFactory.getSession();
Transaction tx=session.beginTransaction();
Customers c=new Customers();
c.setName("c1");
Orders o1 =new Orders();
o1.setOrderNumber("11");
Orders o2 =new Orders();
o2.setOrderNumber("22");
c.getOrderses().add(o1);
c.getOrderses().add(o2);
o1.setCustomers(c);
o2.setCustomers(c);
try {
session.save(o1);
session.save(o2);
//session.save(c);
tx.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
tx.rollback();
}finally{
session.close();
}
}
}
運行這個類,控制臺信息如下:
Hibernate: select max(ID) from orders
Hibernate: select max(ID) from customers
Hibernate: insert into customers (NAME, ID) values (?, ?)
Hibernate: insert into testdb.orders (CUSTOMER_ID, ORDER_NUMBER, ID) values (?, ?, ?)
Hibernate: insert into testdb.orders (CUSTOMER_ID, ORDER_NUMBER, ID) values (?, ?, ?)
從控制臺信息發現已經級聯保存進去了。
在從開庫看看,到底寫的進去的數據是什么:
mysql> select * from orders;
+----+--------------+-------------+
| ID | ORDER_NUMBER | CUSTOMER_ID |
+----+--------------+-------------+
| ?1 | 11 ? ? ? ? ? | ? ? ? ? ? 1 |
| ?2 | 22 ? ? ? ? ? | ? ? ? ? ? 1 |
+----+--------------+-------------+
2 rows in set (0.00 sec)
mysql> select * from customers;
+----+------+
| ID | NAME |
+----+------+
| ?1 | c1 ? |
+----+------+
1 row in set (0.00 sec)
mysql>
開庫查結果表明,寫入Orders對象的同時,寫入了相關的Customers對象。
下面再進行一下反向測試:
先清空兩個表
mysql> delete from orders;
Query OK, 2 rows affected (0.03 sec)
mysql> ?delete from customers;
Query OK, 1 row affected (0.00 sec)
mysql>
然后寫測試類:
package org.lavasoft;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class TestbyCustomer {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
if(HibernateSessionFactory.getSession()==null)System.out.println("null");
Session session=HibernateSessionFactory.getSession();
Transaction tx=session.beginTransaction();
Customers c=new Customers();
c.setName("c1");
Orders o1 =new Orders();
o1.setOrderNumber("11");
Orders o2 =new Orders();
o2.setOrderNumber("22");
c.getOrderses().add(o1);
c.getOrderses().add(o2);
o1.setCustomers(c);
o2.setCustomers(c);
try {
session.save(c);
tx.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
tx.rollback();
}finally{
session.close();
}
}
}
運行這個測試類,控制臺信息如下:
Hibernate: select max(ID) from customers
Hibernate: select max(ID) from orders
Hibernate: insert into customers (NAME, ID) values (?, ?)
Hibernate: insert into testdb.orders (CUSTOMER_ID, ORDER_NUMBER, ID) values (?, ?, ?)
Hibernate: insert into testdb.orders (CUSTOMER_ID, ORDER_NUMBER, ID) values (?, ?, ?)
開庫查,看看寫入數據是什么:
mysql> select * from orders;
+----+--------------+-------------+
| ID | ORDER_NUMBER | CUSTOMER_ID |
+----+--------------+-------------+
| ?1 | 11 ? ? ? ? ? | ? ? ? ? ? 1 |
| ?2 | 22 ? ? ? ? ? | ? ? ? ? ? 1 |
+----+--------------+-------------+
2 rows in set (0.00 sec)
mysql> select * from customers;
+----+------+
| ID | NAME |
+----+------+
| ?1 | c1 ? |
+----+------+
1 row in set (0.00 sec)
mysql>
開庫查結果表明,寫入Customers對象的同時,寫入了相關的Orders對象。
從而可以證明:這個雙向關聯是成功的。
呵呵,myeclipse不錯,是個好東西。
myeclipse試用小記----Hibernate多對一單向關聯(1)
附件:http://down.51cto.com/data/2348079
?著作權歸作者所有:來自51CTO博客作者leizhimin的原創作品,如需轉載,請與作者聯系,否則將追究法律責任
職場Hibernate休閑ORM/持久化
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的myeclipse hbm2java_myeclipse试用小记----Hibernate多对一双向关联(2)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 爸妈约会儿子弹钢琴伴奏 一块钱就能打发儿
- 下一篇: java直接对list使用sql语句_J