欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java鬼混笔记:Hibernate:2、一对一关系之主键单向和双向关联

程序员文章站 2022-04-22 16:10:15
...

这次的笔记是Hibernate的一对一关系操作的,什么是一对一,比如一个人对应一个身份证,一个国家一个星爷,一个手指一个指纹等等。。。都是一对一的。

知识点:一对一主键关联(单向),一对一主键关联(双向)。(主键关联:比如person表的IDcard表的id的意思,两张表通过ID相同获取数据)

一对一外键关联(单向), 一对一外键关联(双向)(外键关联:比如card表中有个字段pid对应的是person表中的id,通过card.person.id = person.id去获取相关的数据)

什么是单向:只能由A访问到B,而不能从B访问A;双向就是AB都相互访问

接下来上实例代码:

比如有个用户Person和身份证Card

单向关联:

Persondx.java

package com.ywj.TestHibernate.e;

import java.io.Serializable;

// 一个人 Persondx 后面的dx是单向的意思
public class Persondx implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3189538608590684635L;

	private Long id;
	private String name;
	private Carddx carddx;// 他的身份证
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Carddx getCarddx() {
		return carddx;
	}
	public void setCarddx(Carddx carddx) {
		this.carddx = carddx;
	}
	
}
 Persondx.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.ywj.TestHibernate.e">
	<class name="Persondx" table="Persondx" >
		<id name="id" column="id">
			<generator class="foreign">  
                <param name="property">carddx</param> <!-- person的id是根据card的id来设置的 一对一嘛 -->
            </generator>
		</id>
		<property name="name" column="name" type="java.lang.String"/>
		<!-- 一对一设置 constrained="true" 进行约束操作 persondx表的个外键,参考表carddx.id -->
		<one-to-one name="carddx" class="Carddx" constrained="true"></one-to-one>	 
	</class>
</hibernate-mapping>
Carddx.java

package com.ywj.TestHibernate.e;

import java.io.Serializable;
// Carddx 后面的dx是单向的意思
public class Carddx implements Serializable {

	
	private static final long serialVersionUID = -5428059743879860845L;

	private Long id;
	private String number;
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
}
Carddx.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.ywj.TestHibernate.e">
	<class name="Carddx" table="carddx" >
		<id name="id" column="id">
			<generator class="identity" /><!-- card ID就自动增长,Person再根据Card ID 保存ID -->
		</id>
		<property name="number" column="number" type="java.lang.String"/>
	</class>
</hibernate-mapping>

测试Demo:

TestOneToOnedx.java

package com.ywj.TestHibernate.test;


import org.hibernate.Session;
import org.hibernate.Transaction;


import com.ywj.TestHibernate.e.Carddx;
import com.ywj.TestHibernate.e.Persondx;


public class TestOneToOnedx {


	public static void main(String[] args) {
		
		Session session = HibernateUtils.getCurrentSession();
		/*Transaction ts = null;
		
		Carddx carddx = new Carddx();
		carddx.setNumber("440882xxxxxxxxxxxxx");
		
		Persondx persondx = new Persondx();
		persondx.setName("Chester");
		persondx.setCarddx(carddx);// 能过这个来绑定 人和身份证的关系
		
		try {
			ts = session.beginTransaction();
			
			session.save(carddx);
			session.save(persondx);
			
			ts.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
			ts.rollback();// 回滚
		} finally {
			session.close();
		}*/
		
		// 查询可以能过Person 查到Card ,不是Cardi不能查到Person ,因为是单向关联,也是Person里有Card属性,Card里没有Person


		/*Persondx p = (Persondx) session.load(Persondx.class, 15L);
		System.out.println(p.getId()+":"+ p.getName());
		// Hibernate: select person0_.id as id1_0_0_, person0_.name as name2_0_0_ from Persondx person0_ where person0_.id=?
		// 15:Chester
		Carddx c = p.getCarddx();
		if(c != null) {
			System.out.println(c.getId()+":"+c.getNumber());
			// Hibernate: select card0_.id as id1_2_0_, card0_.number as number2_2_0_ from carddx card0_ where card0_.id=?
			// 15:440882xxxxxxxxxxxxx
		}
		session.close();
		*/
		
		/*Persondx p = (Persondx) session.load(Persondx.class, 4L);
		p.setId(3L);
		Transaction ts = session.beginTransaction();
		session.delete(p);
		ts.commit();*/
	}
	
}

就这个样子。。。下面是双向


双向关联:


Personsx.java

package com.ywj.TestHibernate.e;

import java.io.Serializable;


public class Personsx implements Serializable{


	private static final long serialVersionUID = -3189538608590684635L;

	private Long id;
	private String name;
	private Cardsx cardsx;// 他的身份证
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Cardsx getCardsx() {
		return cardsx;
	}
	public void setCardsx(Cardsx cardsx) {
		this.cardsx = cardsx;
	}
}

Personsx.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.ywj.TestHibernate.e">
	<class name="Personsx" table="Personsx" >
		<id name="id" column="id">
			<generator class="identity"></generator> <!-- 一个人的id是自增长的 -->
		</id>
		<property name="name" column="name" type="java.lang.String"/>
		<one-to-one name="cardsx" class="Cardsx"  cascade="all"></one-to-one>	 
	</class>
</hibernate-mapping>


Cardsx.java

package com.ywj.TestHibernate.e;

import java.io.Serializable;

public class Cardsx implements Serializable {

	
	private static final long serialVersionUID = -5428059743879860845L;

	private Long id;
	private String number;
	private Personsx personsx;
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public Personsx getPersonsx() {
		return personsx;
	}
	public void setPersonsx(Personsx personsx) {
		this.personsx = personsx;
	}
}

Cardsx.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.ywj.TestHibernate.e">
	<class name="Cardsx" table="cardsx" >
		<id name="id" column="id">
			<generator class="foreign">  
                <param name="property">personsx</param> <!--  一对一嘛 card的id就由person来决定-->
            </generator>
		</id>
		<property name="number" column="number" type="java.lang.String"/>
		<!-- 一对一设置 constrained="true" 进行约束操作 cardsx表id FK约束 参考表personsx.id-->
		<one-to-one name="personsx" class="Personsx" constrained="true"></one-to-one>
	</class>
</hibernate-mapping>

测试demo:

TestOneToOnesx.java

package com.ywj.TestHibernate.test;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ywj.TestHibernate.e.Cardsx;
import com.ywj.TestHibernate.e.Personsx;

public class TestOneToOnesx {

	public static void main(String[] args) {
		
		Session session = HibernateUtils.getCurrentSession();
		Transaction ts = null;
		
		Cardsx cardsx = new Cardsx();
		cardsx.setNumber("440882xxxxxxxxxxxxx");
		
		Personsx personsx = new Personsx();
		personsx.setName("Chester");
		personsx.setCardsx(cardsx);// 能过这个来绑定 人和身份证的关系
		
		cardsx.setPersonsx(personsx);
		
		
		try {
			ts = session.beginTransaction();
			
			session.save(cardsx);
			session.save(personsx);
			
			ts.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
			ts.rollback();// 回滚
		} finally {
			session.close();
		}
		
		// 可以通过person来查询card
		/*Personsx p = (Personsx) session.load(Personsx.class, 3L);
		System.out.println(p.getId()+":"+ p.getName());
		Cardsx c = p.getCardsx();
		if(c != null) {
			System.out.println(c.getId()+":"+c.getNumber());
		}
		System.out.println("xxxxxxxxxxxxxx");
		// 也可以通过card来查询person
		Cardsx cc = (Cardsx) session.load(Cardsx.class, 3L);
		System.out.println(cc.getNumber()+cc.getId());
		
		Personsx pp = cc.getPersonsx();
		System.out.println(pp.getName()+pp.getId());
		session.close();*/
		
	}
	
}


OK。。。。