Hibernate framework-04-01-Hibernate one-to-one association mapping hbm file configuration


Feelings:In order to automatically save the foreign key table when the cascade mapping saves the foreign key table, you need to create the object of the foreign key table in the save operation. How do I know that I want to save it without operation! !


basis

  • Relationship (Relationship) In the
    real world, the connections between things and between things are reflected in the information world as the connections between entities and the connections between entities.

  • The connections between entities can be divided into:
    one-to-one connections (1:1); 
    one-to-many connections (1:n);
    many-to-many connections (m:n).

Insert picture description here

Insert picture description here

Both of these methods require the creation of foreign keys.

Primary key association mapping

Insert picture description here
The ID corresponding to the user table is stored in the person table;
the primary keys of the two tables are the same.
Both are primary keys and foreign keys.

File structure

Insert picture description here

A foreign key is set in the id column of the user table in the database, and the object is the id column of the person table.
Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here

<?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.hibernate.entity">
	<class name="User" table="hibernate_04_01_user">
		<id name="id">
			<!-- 设置自动递增 -->
			<generator class="identity"/>
		</id>
		
		<property name="userName" column="user_name"/>
		<property name="password" column="password"/>
		
		
			<!-- name=实体类中属性名,class=对应的元素的类型 / 关联的属性的类型 
			 一对一关联关系, 
			 如果Person对象不在本包的下的话,就需要配置的是全线路径了。com.hibernate.entitty.Person.java -->
			<!-- one-to-one:一对一
    	    name:User实体类中某个属性名
    	    class:同User关联的类型
    	    cascade:级联操作,当操作user对象时,级联操作同user关联的person对象
    	        -all:所有操作都级联
    	        -none:所有操作都不级联(默认取值)
    	        -save-update:插入或者更新操作级联
    	        -delete:删除操作级联 -->
		<one-to-one name="person" class="Person" cascade="all"></one-to-one>
		
	</class>

</hibernate-mapping>

Insert picture description here

Insert picture description here

<?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.hibernate.entity">
    <class name="Person" table="hibernate_04_01_person">
    	<id name="id">
    	    <generator class="foreign"><!-- 设置主键的生成策略,表明既是主键又是外键 -->
    	    	<!-- name="property"表示当前配置的是某个属性,user是这个属性的名字 -->
    	    	<param name="property">user</param>
    	    </generator>
    	</id>
    	<property name="realName" column="real_name"/>
    	<property name="idNumber" column="id_number"/>
    	
    	
    	<!-- 映射user属性,实质上是在映射Person和User之间的一对一关联关系 -->
    	<!-- one-to-one:一对一
    	    name:User实体类中某个属性名
    	    class:同User关联的类型 
    	    constrained="true"表示约束关系-->
    	<one-to-one name="user" class="User" constrained="true" ></one-to-one>
    </class>
</hibernate-mapping>   
package com.hibernate.ui;



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

import com.hibernate.entity.Person;
import com.hibernate.entity.User;
import com.hibernate.util.HibernateUtil;

public class Test {
    
    
	public static void main(String[] args) {
    
    
		saveUser();		
		
//		addPerson();
		
//		deleteUser();
		
		//关闭SessionFactory
		HibernateUtil.closeSessionFactory();
	}//-----------------------------------------------------------------------------------------

	
	public static void saveUser() {
    
    
		Session session = HibernateUtil.openSession();
		Transaction tx = session.beginTransaction();
		
		User u = new User();
		u.setUserName("name6");
		u.setPassword("0006");
		session.save(u);
		
		tx.commit();
		session.close();
	}
	

	
	//给已经存在的账户添加实名认证信息
		public static void addPerson() {
    
    
			Session session = HibernateUtil.openSession();
			Transaction tx = session.beginTransaction();
			//检索出已经存在的账户信息
			User u = session.get(User.class, new Integer(3));
			
			Person p = new Person();
			p.setRealName("小小张");
			p.setIdNumber("12222222220121");
			
			//建立关联
			u.setPerson(p);
			p.setUser(u);
			
			//保存
			session.save(p);
			
			tx.commit();
			session.close();
		}
		
		
		public static void deleteUser() {
    
    
			Session session = HibernateUtil.openSession();
			Transaction tx = session.beginTransaction();
			User u = session.get(User.class, new Integer(3));
			session.delete(u);
			tx.commit();
			session.close();
		}
}

Insert picture description here
Insert picture description here
Insert picture description here





Unique foreign key association mapping

Insert picture description here

The only way to associate foreign key values.

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here
After adding a unique identifier to the above foreign key, many-to-one is a one-to-one correspondence. If it is not added, it becomes a many-to-one relationship.

Insert picture description here

<?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.hibernate.entity">
	<class name="User" table="hibernate_04_02_user">
		<id name="id">
			<!-- 设置自动递增 -->
			<generator class="identity"/>
		</id>
		
		<property name="userName" column="user_name"/>
		<property name="password" column="password"/>
		
		
			<!-- 下面配置的是外键列 -->
			<!-- name=实体类中属性名,class=对应的元素的类型 / 关联的属性的类型 
			 一对一关联关系, 
			 如果Person对象不在本包的下的话,就需要配置的是全线路径了。com.hibernate.entitty.Person.java -->
			<!-- one-to-one:一对一
 			many-to-one:使用唯一外键关联映射,实质上是多对一的一种特殊情况,unique="true"(外键字段的值不能重复)
    	    name:User实体类中某个属性名
    	    class:同User关联的类型
    	    column:外键字段列名
    	    cascade:级联操作,当操作user对象时,级联操作同user关联的person对象
    	        -all:所有操作都级联
    	        -none:所有操作都不级联(默认取值)
    	        -save-update:插入或者更新操作级联
    	        -delete:删除操作级联 -->
    	        <!-- 此处的column属性必须配置 -->
		<many-to-one name="person" class="Person" cascade="all" unique="true"
		column="PERSON_ID"></many-to-one>
		
	</class>

</hibernate-mapping>

Insert picture description here
Insert picture description here

<?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.hibernate.entity">
    <class name="Person" table="hibernate_04_02_person">
    	<id name="id">
    	    <generator class="identity">
    	    	<!-- name="property"表示当前配置的是某个属性,user是这个属性的名字 -->
    	    	<param name="property">user</param>
    	    </generator>
    	</id>
    	<property name="realName" column="real_name"/>
    	<property name="idNumber" column="id_number"/>
    	
    	
    	<!-- 映射user属性,实质上是在映射Person和User之间的一对一关联关系 -->
    	<!-- one-to-one:一对一
    	    name:User实体类中某个属性名
    	    class:同User关联的类型 
    	    property-ref:外键列对应的属性名-->
    	<one-to-one name="user" class="User" property-ref="person"></one-to-one>
    	<!-- 与Person类关联的User类中关联的哪个属性 -->
    </class>
</hibernate-mapping>   
package com.hibernate.ui;



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

import com.hibernate.entity.Person;
import com.hibernate.entity.User;
import com.hibernate.util.HibernateUtil;

public class Test {
    
    
	public static void main(String[] args) {
    
    
		saveUser();		
		
//		addPerson();
		
//		deleteUser();
		
		//关闭SessionFactory
		HibernateUtil.closeSessionFactory();
	}//-----------------------------------------------------------------------------------------

	
	public static void saveUser() {
    
    
		Session session = HibernateUtil.openSession();
		Transaction tx = session.beginTransaction();
		User u = new User();
		u.setUserName("张三");
		u.setPassword("123123");
		
		Person p = new Person();
		p.setRealName("大张");
		p.setIdNumber("3421324232323");
		
		//建立关联
		u.setPerson(p);
		p.setUser(u);
		
		session.save(u);
		
		tx.commit();
		session.close();
	}
	

	
	//给已经存在的账户添加实名认证信息
		public static void addPerson() {
    
    
			Session session = HibernateUtil.openSession();
			Transaction tx = session.beginTransaction();
			//检索出已经存在的账户信息
			User u = session.get(User.class, new Integer(3));
			
			Person p = new Person();
			p.setRealName("小小张");
			p.setIdNumber("12222222220121");
			
			//建立关联
			u.setPerson(p);
			p.setUser(u);
			
			//保存
			session.save(p);
			
			tx.commit();
			session.close();
		}
		
		
		public static void deleteUser() {
    
    
			Session session = HibernateUtil.openSession();
			Transaction tx = session.beginTransaction();
			User u = session.get(User.class, new Integer(3));
			session.delete(u);
			tx.commit();
			session.close();
		}
}

Guess you like

Origin blog.csdn.net/qq_44627608/article/details/114482795