Hibernate多表关联

Hibernate的关联映射关系:

多对一 ---- many-to-one
一对多 ---- one-to-many
一对一 ---- one-to-one
多对多 ---- many-to-many

比较常用的是多对一和一对一关联映射多对一关联映射:

场景:用户和组;从用户角度来看,多个用户属于一个组(多对一关联)
使用Hibernate开发的思路:先建立对象模型,把实体抽取出来。目前两个实体:用户和组两个实体,多个学生拥有同一个地址
,所有用户实体中应该有一个持有组的引用

实体类:

package com.entity;

/**
 * Student entity. @author MyEclipse Persistence Tools
 */

public class Student implements java.io.Serializable {

    // Fields

    private Integer id;
    private String name;
    private Integer addid;
    
    private Adrress adss;

    // Constructors

    public Adrress getAdss() {
        return adss;
    }

    public void setAdss(Adrress adss) {
        this.adss = adss;
    }

    /** default constructor */
    public Student() {
    }

    /** full constructor */
    public Student(String name, Integer addid) {
        this.name = name;
        this.addid = addid;
    }

    // Property accessors

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAddid() {
        return this.addid;
    }

    public void setAddid(Integer addid) {
        this.addid = addid;
    }

}
package com.entity;

/**
 * Adrress entity. @author MyEclipse Persistence Tools
 */

public class Adrress implements java.io.Serializable {

    // Fields

    private Integer idAdrress;
    private String detail;

    // Constructors

    /** default constructor */
    public Adrress() {
    }

    /** full constructor */
    public Adrress(String detail) {
        this.detail = detail;
    }

    // Property accessors

    public Integer getIdAdrress() {
        return this.idAdrress;
    }

    public void setIdAdrress(Integer idAdrress) {
        this.idAdrress = idAdrress;
    }

    public String getDetail() {
        return this.detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

}

hibernate映射表的内容





    
        
            
            
        
        
            
        
        
        
        
    

Spring中配置了事务,利用的是注解的方式



        
        
        
            
        
        
            

                
                    org.hibernate.dialect.SQLServerDialect                     
                
                true
                true

                true         
                false              
                org.hibernate.cache.EhCacheProvider    

            
        
        

        

        
            
                com/entity
            
        
    

        
    

        
    


        
            
            
        
    

    
    
        
     
    
 

测试代码

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserDao  ud1=(UserDao)ctx.getBean("userDao");//改成userDao就是注解方式的代理

System.out.println("OK");
ud1.insert("liu");

 

UserDao中的插入数据,因为没有用到接口Spring采用的CGLib代理。

public void insert(String s) {

        System.out.println("插入数据!");
        // sessionFactory.openSession().save(arg0)

            Admin adm = new Admin();
            Session sess = sessionFactory.getCurrentSession();
            
            Adrress ad = new Adrress();
            
            ad.setDetail("shan xi");
            
            Student st = new Student();
            
            st.setName("liuyu");
            st.setAdss(ad);
            sess.save(st);
            //Transaction tx = sess.beginTransaction();
            //tx.begin();
            /*Userinfo user = new Userinfo();
                    
            System.out.println("查找数据!");
            

        // TODO Auto-generated method stub

    }

你可能感兴趣的:(Hibernate多表关联)