Hibernate多对一关系映射

12720171106


package com.tiger.main;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.tiger.bean.School;
import com.tiger.bean.Student;
/**
 * n - 1(many to one)
 * 多位学生属于共同的一所大学(many 指的是学生)
 * 一所大学包含多位学生(one 指的是学校)
 * @author tiger
 * @date 2017年11月6日
 */
public class Main_many_one {
	public static void main(String[] args) {
		Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
		SessionFactory sf = cfg.buildSessionFactory();
		Session session = sf.openSession();
		Transaction tx = session.beginTransaction();
		
		//new 一座大学
		School school01 = new School();
		school01.setName("北理工");
		
		School school02 = new School();
		school02.setName("北师珠");
		
		//new 多位学生
		Student student01 = new Student();
		student01.setName("小明");
		student01.setSchool(school01);
		
		Student student02 = new Student();
		student02.setName("小红");
		student02.setSchool(school01);
		
		Student student03 = new Student();
		student03.setName("小黄");
		student03.setSchool(school02);
		
		Student student04 = new Student();
		student04.setName("小黑");
		student04.setSchool(school02);
		
		session.save(student01);
		session.save(student02);
		session.save(student03);
		session.save(student04);
		
		tx.commit();
		
		session.close();
		sf.close();
	}
}

   
   


   
   

   
   
    
    
    
        
     
     
            
      
      
            
      
      
        
     
     
        
     
     
            
      
      
        
     
     
        
     
     
        
     
     
            
      
      
        
     
     
    
    
    

   
   
package com.tiger.bean;
/**
 * n 学生有很多
 * @author tiger
 * @date 2017年11月6日
 */
public class Student {
	
	private Integer id;
	private String name;
	private School school;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public School getSchool() {
		return school;
	}
	public void setSchool(School school) {
		this.school = school;
	}
	public Student() { }
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", school=" + school + "]";
	}
}

   
   


   
   

   
   
    
    
    
        
     
     
            
      
      
            
      
      
        
     
     
        
     
     
            
      
      
        
     
     
    
    
    

   
   
package com.tiger.bean;
/**
 * 1
 * 一所学生被多位学生共享
 * @author tiger
 * @date 2017年11月6日
 */
public class School {
	
	private Integer id;
	private String name;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public School() {
		super();
	}
	@Override
	public String toString() {
		return "School [id=" + id + ", name=" + name + "]";
	}
}

   
   


   
   
	
    
    
		
     
     
      
      org.hibernate.dialect.MySQL5InnoDBDialect
     
     
		
     
     
		
     
     
      
      com.mysql.jdbc.Driver
     
     
		
     
     
      
      jdbc:mysql://localhost:3306/book_many_one
     
     
		
     
     
      
      root
     
     
		
     
     
      
      123456
     
     

		
     
     
		
     
     
      
      30
     
     
		
     
     
      
      10
     
     
		
     
     
      
      5000
     
     

		
     
     
		
     
     
      
      create
     
     

		
     
     
      
      true
     
     
		
     
     
		
     
     
	
    
    

   
   

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ljt-tiger

thanks

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值