• Hibernate学习笔记第二篇 关联关系映射 many –to –one


     many –to –one

    假设2个对象,分别是Person and Address .

    1个Address中含有多个Person

    分别建立类

    package cn.itcase.hibernate.domain;
    
    public class Address {
     private int id;
     private String street;
     
     public int getId() {
            return id;
        }
    public void setId(int id) {
            this.id = id;
        }
    
    public String getStreet() {
        return street;
    }
    public void setStreet(String street) {
        this.street = street;
    }
    
    }
    
    
    package cn.itcase.hibernate.domain;
    
    public class Person {
     private int id;
     private String name;
     private Address address;
     
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    }

    相对应的配置文件为

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
    	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="cn.itcase.hibernate.domain">
    	<class name="Address" table="address">
    		<id name="id" >
    			<generator class="native" />
    		</id>
    		<property name="street"></property>
    	
    	
    	</class>
    </hibernate-mapping>
    
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
    	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="cn.itcase.hibernate.domain">
    	<class name="Person" table="person">
    		<id name="id" >
    			<generator class="native" />
    		</id>
    		<property name="name" />
    	   <!--留意这里 name的名称必须和address类中的private Address addr;对象名称一致 
    	     列名可任意命名,最好是具有相对意义
    	    -->	
    		<many-to-one name="address" column="addressId"/>
    	</class>
    </hibernate-mapping>
    

    //测试多对一例子

    package cn.itcast.hibernate;
    
    import java.util.HashSet;
    import java.util.Set;
    
    import org.hibernate.Hibernate;
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    
    import cn.itcase.hibernate.domain.Address;
    import cn.itcase.hibernate.domain.Person;
    
    public class many2one2 {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Person depart=add();
    		Person emp=query(1);
    		//System.out.println(emp.getDepart().getName());
    		//queryDepart(3);
    		//System.out.println(addrquery(1).getPer().getName());
    	}
    	
    	
    
    	static Address addrquery(int id){
    		Session s=null;
    		Transaction tx=null;
    		try{
    			s=HibernateUtil.getSession();
    			tx=s.beginTransaction();
    			Address addr=(Address)s.get(Address.class, id);
    			//System.out.print(emp.getDepart().getName());
    			Hibernate.initialize(addr.getStreet());
    			tx.commit();
    			return addr;
    		}finally{
    			if(s!=null)
    				s.close();
    		}	
    	}
    
    	static Person query(int pid){
    		Session s=null;
    		Transaction tx=null;
    		try{
    			s=HibernateUtil.getSession();
    			tx=s.beginTransaction();
    			Person p=(Person)s.get(Person.class, pid);
    			tx.commit();
    			return p;
    		}finally{
    			if(s!=null)
    				s.close();
    		}		
    	}
    
    	static Person add(){
    		Session s=null;
    		Transaction tx=null;
    		try{
    			Address addr1=new Address();
    			addr1.setStreet("add1");	
    
    			Person p1=new Person();
    			p1.setName("p1");
    
    			Person p2=new Person();
    			p2.setName("p2");
    
    			p1.setAddress(addr1);
    			p2.setAddress(addr1);
    
    			s=HibernateUtil.getSession();
    			tx=s.beginTransaction();
    
    			s.save(addr1);
    			s.save(p1);
    			s.save(p2);
    			tx.commit();
    			return p1;
    		}finally{
    			if(s!=null)
    				s.close();
    		}	
    	}
    
    }
    
  • 相关阅读:
    使用私有api实现自己的iphone桌面,并根据app的使用次数对app排序
    坐标系的属性
    带坐标轴的几何画板
    空间几何体的直观图matlab
    设置npm的registry
    (原创)机器学习之numpy库中常用的函数介绍(一)
    (原创)交叉编译 tesseract
    (原创)计算机视觉之数学原理-基础篇
    (原创)nRF51 DFU 初始化包介绍及生成工具
    (原创)使用nRF51822/nRF51422创建一个简单的BLE应用 ---入门实例手册(中文)之五
  • 原文地址:https://www.cnblogs.com/meetweb/p/2819807.html
Copyright © 2020-2023  润新知