精通HIBERNATE---------读书笔记第十章 映射组成关系

晶粒度对象模型:把类中多个属性抽象出一个类,方便代码重用

public class Customer3 implements java.io.Serializable{
	private Long id;
	private String name;
	private Address homeAddress;
	private Address comAddress;


 

public class Address implements java.io.Serializable{
	private final String province;
	private final String city;
	private final String street;
	private final String zipcode;

把Address抽象出来了,注意Address没有主键!!
get,set方法省略,

对于表,应尽量减少外键,而不是可以定义2个表Customer和address

如下

create table CUSTOMERS (ID bigint not null, NAME varchar(15), HOME_PROVINCE varchar(255), HOME_CITY varchar(255), HOME_STREET varchar(255), HOME_ZIPCODE varchar(255), COM_PROVINCE varchar(255), COM_CITY varchar(255), COM_STREET varchar(255), COM_ZIPCODE varchar(255), primary key (ID));


然后用component组件定义映射关系

hibernate-mapping>
	
		
			
		


 


			
		 
            
            
            
	


name是属性名,class是对应的类,parent是Address所属的整体类,即Customer,并且在Address类里要有setCustomer,getCustomer,这个要特别注意!!

区分一下Value类型和Entity类型

比如Address就是value类型,他没有自己的主键,不能单独持久化,session.save(address)是会报错的,而entity类型是有主键的,并且可以单独持久化。

删除customer时,所以value类型都会删除,而entity类型,要看cascade是否支持级联删除。

应用组成关系的持久化类:

 public void saveCustomer(){
    Session session = sessionFactory.openSession();
    Transaction tx = null;
    try {
      tx = session.beginTransaction();
      
      Customer customer=new Customer();
      Address homeAddress=new Address("province1","city1","street1","100001",customer);
      Address comAddress=new Address("province2","city2","street2","200002",customer);
      customer.setName("Tom");
      customer.setHomeAddress(homeAddress);
      customer.setComAddress(comAddress);

      session.save(customer);
     
      tx.commit();

    }catch (RuntimeException e) {
      if (tx != null) {
        tx.rollback();
      }
      throw e;
    } finally {
        session.close();
    }
  }


 

 public void printCustomerAddress(Customer customer){
    Address homeAddress=customer.getHomeAddress();
    Address comAddress=customer.getComAddress();
    if(homeAddress==null)
      System.out.println("Home Address of "+customer.getName()+" is null.");
    else
      System.out.println("Home Address of "+customer.getName()+" is: " 
      +homeAddress.getProvince()+" "
      +homeAddress.getCity()+" "
      +homeAddress.getStreet());

  if(comAddress==null)
      System.out.println("Company Address of "+customer.getName()+" is null.");
    else
      System.out.println("Company Address of "+customer.getName()+" is: "
      +comAddress.getProvince()+" "
      +comAddress.getCity()+" "
      +comAddress.getStreet());
  }
 public void deleteCustomer(Customer customer){
    Session session = sessionFactory.openSession();
    Transaction tx = null;
    try {
      tx = session.beginTransaction();

      session.delete(customer);
      tx.commit();

    }catch (RuntimeException e) {
      if (tx != null) {
        tx.rollback();
      }
      throw e;
    } finally {
        session.close();
    }
  }


映射符合组成关系,就是组件套组件,组件里面还有多对一,看代码吧



  
    
      
    

    
        
    
    
    
            
            
            
                
            
            
            
               
              
               
                  
               

            

            
    
  
 


还有一个主意的就是value类型是不能有映射文件的,比如这个例子中 graphicsCard和cpuBox都不能有hbm.xml而entity就要有了,如Vendor.hbm.xml

最后希望大家一同提高吧!

你可能感兴趣的:(Hibernate)