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

                                一对一关联

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

     第一种、共享主键方式

登录表login:


详细信息表detail:


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

(2)添加Hibernate框架

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

(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

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="org.vo.Login" table="login" schema="dbo" catalog="Test">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <!-- 采用foreign标识生成器,直接采用外键的属性值,达到共享主键的目的 -->
            <generator class="foreign">
            	<param name="property">detail</param>
            </generator>
        </id>
        <property name="username" type="java.lang.String">
            <column name="username" length="50" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="password" length="50" />
        </property>
        <!-- name表示属性名字,class表示被关联的类的名字,constrained="true"标识当前主键上存在一个外键约束 -->
        <one-to-one name="detail" class="org.vo.Detail" constrained="true"></one-to-one>
    </class>
</hibernate-mapping>
Detail.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="org.vo.Detail" table="detail" schema="dbo" catalog="Test">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native" />
        </id>
        <property name="truename" type="java.lang.String">
            <column name="truename" length="50" />
        </property>
        <property name="email" type="java.lang.String">
            <column name="email" length="50" />
        </property>
        <!-- cascade="all"表示主控类的所有操作,对关联类也执行同样的操作,lazy="false"表示此关联为立即加载 -->
        <one-to-one name="login" class="org.vo.Login" cascade="all" lazy="false">
        </one-to-one>
    </class>
</hibernate-mapping>
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("1234@qq.com");
		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<list1.size();i++){
				Detail detail=(Detail)list1.get(i);
				Login login=(Login)list2.get(i);
				System.out.println("ID"+detail.getId()+","+"Truename"+","+detail.getTruename());
				System.out.println("ID"+login.getId()+","+"username"+","+login.getUsername());
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}
运行结果:



阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/huanhuanxiaoxiao/article/details/53726238
文章标签: Hibernate
个人分类: Hibernate
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭