关联关系映射——一对一关联

                                一对一关联

   Hibernate映射实体的一对一关联关系有两种实现方式:共享主键方式和唯一外键方式。所谓共享主键方式就是限制两个数据表的主键使用相同的值,通过主键实现一对一映射关系,即主键相同。所谓唯一外键方式就是一个表的外键和另一个表的唯一主键对应形成一对一映射关系,多对一的特殊情况。

     第一种、共享主键方式

登录表login:


详细信息表detail:


(1)创建Java项目,命名为Hibernate_mapping1-1

(2)添加Hibernate框架

(3)生成数据库对应的Java类对象和映射文件

关联关系映射——一对一关联_第1张图片

(4)修改代码,建立表之间的关联

Login.java

package org.vo;

public class Login implements java.io.Serializable {
	private Integer id;
	private String username;
	private String password;
	private Detail detail;//添加属性字段
	public Detail getDetail() {
		return detail;
	}

	public void setDetail(Detail detail) {
		this.detail = detail;
	}
	public Login() {
	}
	public Login(String username, String password,Detail detail) {
		this.username = username;
		this.password = password;
		this.detail=detail;//完善构造函数
	}
	public Integer getId() {
		return this.id;
	}

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

	public String getUsername() {
		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
Detail.java

package org.vo;

public class Detail implements java.io.Serializable {
	private Integer id;
	private String truename;
	private String email;
	private Login login;
	public Login getLogin() {
		return login;
	}

	public void setLogin(Login login) {
		this.login = login;
	}

	public Detail() {
	}

	public Detail(String truename, String email,Login login) {
		this.truename = truename;
		this.email = email;
		this.login=login;
	}


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

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

	public String getTruename() {
		return this.truename;
	}

	public void setTruename(String truename) {
		this.truename = truename;
	}

	public String getEmail() {
		return this.email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

}
Login.hbm.xml





    
        
            
            
            
            	detail
            
        
        
            
        
        
            
        
        
        
    

Detail.hbm.xml





    
        
            
            
        
        
            
        
        
            
        
        
        
        
    

Test.java

package org.test;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.util.HibernateSessionFactory;
import org.vo.Detail;
import org.vo.Login;

public class test {
	Session session=HibernateSessionFactory.getSession();

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		test t=new test();
		t.save();
		t.query();
		HibernateSessionFactory.closeSession();

	}
	public void save(){
		Transaction tc=session.beginTransaction();
		Detail detail=new Detail();
		Login login=new Login();
		login.setUsername("pengtao");
		login.setPassword("123456");
		detail.setTruename("彭涛");
		detail.setEmail("[email protected]");
		login.setDetail(detail);
		detail.setLogin(login);
		session.save(detail);
		tc.commit();
	}
	public void query(){
		try{
			Query query1=session.createQuery("from Detail");
			Query query2=session.createQuery("from Login");
			List list1=query1.list();
			List list2=query2.list();
			for(int i=0;i
运行结果:



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