Hibernate中one-to-many/many-to-one和many-to-many

<1>one-to-many/many-to-one

(1)表结构

/**客户表**/
create table CUSTOMERS(
ID int not null auto_increment,
Name varchar(15),
primary key(ID)
);

/**订单表**/
create table ORDERS(
ID bigint not null auto_increment,
ORDER_NUMBER varchar(15),
CUSTOMER_ID int not null,
primary key(ID),
foreign key (CUSTOMER_ID) references CUSTOMERS(ID)
);

(2)Customers.hbm.xml

Java代码 复制代码
  1. <set    
  2.     name="Orders"    
  3.     inverse="true"  
  4.     lazy="false"  
  5.     cascade="all">    
  6.        <key column="CUSTOMER_ID"/>   
  7.        <one-to-many class="Orders"/>   
  8. </set>  
<set 
	name="Orders" 
	inverse="true"
	lazy="false"
	cascade="all"> 
       <key column="CUSTOMER_ID"/>
       <one-to-many class="Orders"/>
</set>



说明:inverse="true",Customers端无须维护这个关联关系。也就是当Customers对象变化时,不用同步更新它的关联的Orders(Orders这端的inverse="false",表明由它来维护关系),这样避免双方重复更新同一关系。

(3)Orders.hbm.xml

Java代码 复制代码
  1. <many-to-one   
  2.     name="Customer"  
  3.     column="CUSTOMER_ID"  
  4.     class="Customers"  
  5.     not-null="true"  
  6.     >   
  7. </many-to-one>  
<many-to-one
	name="Customer"
	column="CUSTOMER_ID"
	class="Customers"
	not-null="true"
	>
</many-to-one>



<2>many-to-many

(1)表结构

create table AUTHORIZATION(
ID bigint(20) not null auto_increment,
AUTH int(11),
AUTHORIZE int(11),
PRIMARY KEY (ID)
);

create table USR(
ID bigint(20) not null auto_increment,
NAME varchar(16),
PRIMARY KEY (ID)
);

/**双向关联的中间连接表**/
create table USERAUTH(
ID bigint(20) not null auto_increment,
USER_ID bigint(20),
AUTH_ID bigint(20),
PRIMARY KEY (ID)
);

(2)Authorization.hbm.xml

Java代码 复制代码
  1. <set name="users"  
  2.      table="userauth"  
  3.      inverse="false"  
  4.      cascade="all">   
  5.     <key column="AUTH_ID"/>   
  6.     <many-to-many class="Usr" column="USER_ID"/>   
  7. </set>  
<set name="users"
     table="userauth"
     inverse="false"
     cascade="all">
	<key column="AUTH_ID"/>
	<many-to-many class="Usr" column="USER_ID"/>
</set>



(3)Usr.hbm.xml

Java代码 复制代码
  1. <set name="auths"  
  2.      table="userauth"  
  3.      cascade="all"  
  4.      inverse="true"><!--由Authorization一端来维护关系-->   
  5.          <key column="USER_ID"/>   
  6.     <many-to-many class="Authorization" column="AUTH_ID"/>   
  7. </set>  
<set name="auths"
     table="userauth"
     cascade="all"
     inverse="true"><!--由Authorization一端来维护关系-->
         <key column="USER_ID"/>
	<many-to-many class="Authorization" column="AUTH_ID"/>
</set>



说明:多对多关系,通过一个中间连接表,分离成了两个一对多关系。

测试:

Java代码 复制代码
  1. Usr userA = new Usr();   
  2. Usr userB = new Usr();   
  3. userA.setName("jinming");   
  4. userB.setName("genghua");   
  5.   
  6. Authorization auth1 = new Authorization();   
  7. Authorization auth2 = new Authorization();   
  8. auth1.setAuth(0);   
  9. auth1.setAuth(10);   
  10. auth2.setAuth(1);   
  11. auth2.setAuthorize(11);   
  12.   
  13. Set<Authorization> set1 = new HashSet<Authorization>();   
  14. set1.add(auth1);   
  15. set1.add(auth2);   
  16. userA.setAuths(set1);   
  17. userB.setAuths(set1);   
  18.   
  19. Set<Usr> set2 = new HashSet<Usr>();   
  20. set2.add(userA);   
  21. set2.add(userB);   
  22. auth1.setUsers(set2);   
  23. auth2.setUsers(set2);   
  24.   
  25. userDao.save(userA);   
  26. userDao.save(userB);  

你可能感兴趣的:(Hibernate,xml)