Hibernate 双向一对一&双向一对多

纸上得来终觉浅

1.双向一对多

双向一对多与双向多对一是相同的。  这中情况下B有多个A,B能够加载A,A也能够加载B:

package roadArchitectWeb;

import java.util.HashSet;
import java.util.Set;

public class Teacher {
	private Integer Id;
	private String Name;
	private String PhoneNum;
	private Set<Student> Students = new HashSet<>();

	public Set<Student> getStudents() {
		return Students;
	}
	public void setStudents(Set<Student> students) {
		Students = students;
	}
	public Integer getId() {
		return Id;
	}
	public void setId(Integer id) {
		Id = id;
	}
	public String getName() {
		return Name;
	}
	public void setName(String name) {
		Name = name;
	}
	public String getPhoneNum() {
		return PhoneNum;
	}
	public void setPhoneNum(String phoneNum) {
		PhoneNum = phoneNum;
	}
	public  Teacher() {
	}
	@Override
	public String toString() {
		return "Teacher [Id=" + Id + ", Name=" + Name + ", PhoneNum=" + PhoneNum + ", Students=" + Students + "]";
	}
}
package roadArchitectWeb;

public class Student {
	private Integer Id;
	private String sName;
	private String Age;
	private Teacher teacher;
	public Teacher getTeacher() {
		return teacher;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	public Integer getId() {
		return Id;
	}
	public void setId(Integer id) {
		Id = id;
	}
	public String getsName() {
		return sName;
	}
	public void setsName(String sName) {
		this.sName = sName;
	}
	public String getAge() {
		return Age;
	}
	public void setAge(String age) {
		Age = age;
	}
	@Override
	public String toString() {
		return "Student [Id=" + Id + ", sName=" + sName + ", Age=" + Age + ", teacher=" + teacher + "]";
	}
	public Student(){
	}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2016-4-21 19:24:31 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="roadArchitectWeb.Teacher" table="TEACHER">
        <id name="Id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="Name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="PhoneNum" type="java.lang.String">
            <column name="PHONENUM" />
        </property>
        <set name="Students" table="STUDENT">
<!--    这里的column与many-to-one中生成的column一致 -->
        <key column="teacherId"></key>
        <one-to-many class="roadArchitectWeb.Student"/>
        </set>
    </class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2016-4-21 19:12:13 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping package="roadArchitectWeb">
    <class name="Student" table="STUDENT">
        <id name="Id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native">
            </generator>
        </id>
        <property name="sName" type="java.lang.String">
            <column name="SNAME" />
        </property>
        <property name="Age" type="java.lang.String">
            <column name="AGE" />
        </property>
        <many-to-one name="teacher" class="Teacher"
        column="teacherId">
        </many-to-one>
    </class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
		"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
    <property name="connection.username">root</property>
    <property name="connection.password">123456</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost/roadarchitectweb</property>
    
    <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <property name="hbm2ddl.auto">update</property>
    
    <property name="hibernate.c3p0.max_size">10</property>
    <property name="hibernate.c3p0.min_size">5</property>
    <property name="c3p0.acquire_increment">2</property>
    
    <property name="c3p0.idle_test_period">2000</property>
    <property name="c3p0.timeout">2000</property>
    
    <property name="c3p0.max_statements">10</property>
    
    <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
    <mapping resource="roadArchitectWeb/Student.hbm.xml"/>
    <mapping resource="roadArchitectWeb/Teacher.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
package roadArchitectWeb;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import com.mysql.jdbc.interceptors.ServerStatusDiffInterceptor;
public class HibernateTest {
	@Test
	public void test() {
		SessionFactory sessionFactory = null;
		Configuration configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
		
		/*begin*/
		Teacher teacher = new Teacher();
		Student student = new Student();
		teacher.setName("chenT");
		teacher.setPhoneNum("18788837117");
		student.setsName("ma");
		student.setAge("11");
		student.setTeacher(teacher);
		teacher.getStudents().add(student);
		
		session.save(student);
		session.save(teacher);
		
//		student = (Student)session.get(Student.class, 1);
//		teacher = (Teacher)session.get(Teacher.class,1);
//		System.out.println("HibernateTest.test()S:"+student.getTeacher().getName());
//		System.out.println("HibernateTest.test()T:"+teacher.getStudents().size());
		
		transaction.commit();
		session.close();
		sessionFactory.close();
	}
}

2.双向一对一

一对一双向关联就是在一对一外键关联的基础上,在另一个类中在加上对方的属性;

接着之前做的的一对一外键的基础上:

在B(Teacher)中增加A的属性,同时设置one-to-one标签即可;这样的话就可以互相加载。
3.另外学习过程中注意到了下面一点:一和多,一的一方设置对象的属性,多的一方设置Set集合属性。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值