hibernate一对多,细节讲解

1.一对多

 1).首先创建两个实体类studeninfo.java跟studentxxb.java

    1)studentinfo.java表如图:

package model;

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



public class studentinfo {
	private Integer id;
	private String name;
	private String aihao;
	private Date sjina;
	private Set<studentxxb> studentxxbs=new HashSet<>();
	public studentinfo() {
		super();
	}
	public studentinfo(Integer id, String name, String aihao, Date sjina, Set<studentxxb> studentxxbs) {
		super();
		this.id = id;
		this.name = name;
		this.aihao = aihao;
		this.sjina = sjina;
		this.studentxxbs = studentxxbs;
	}
	@Override
	public String toString() {
		return "studentinfo [id=" + id + ", name=" + name + ", aihao=" + aihao + ", sjina=" + sjina + ", studentxxbs="
				+ studentxxbs + "]";
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAihao() {
		return aihao;
	}
	public void setAihao(String aihao) {
		this.aihao = aihao;
	}
	public Date getSjina() {
		return sjina;
	}
	public void setSjina(Date sjina) {
		this.sjina = sjina;
	}
	public Set<studentxxb> getStudentxxbs() {
		return studentxxbs;
	}
	public void setStudentxxbs(Set<studentxxb> studentxxbs) {
		this.studentxxbs = studentxxbs;
	}
}

  

   2)stdentxxb.java如下图:

    

package model;

import java.util.Date;

public class studentxxb {
	private Integer id;
	private String dhua;
	private String diz;
	private studentinfo studentinfos;
	public studentxxb() {
		super();
	}
	public studentxxb(Integer id, String dhua, String diz, studentinfo studentinfos) {
		super();
		this.id = id;
		this.dhua = dhua;
		this.diz = diz;
		this.studentinfos = studentinfos;
	}
	@Override
	public String toString() {
		return "studentxxb [id=" + id + ", dhua=" + dhua + ", diz=" + diz + ", studentinfos=" + studentinfos + "]";
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getDhua() {
		return dhua;
	}
	public void setDhua(String dhua) {
		this.dhua = dhua;
	}
	public String getDiz() {
		return diz;
	}
	public void setDiz(String diz) {
		this.diz = diz;
	}
	public studentinfo getStudentinfos() {
		return studentinfos;
	}
	public void setStudentinfos(studentinfo studentinfos) {
		this.studentinfos = studentinfos;
	}
}

  2).两个实体类创建好了,接下来配置两个实体类的hbm.xml文件

      1).实体类studentinfo.java的配置(studentinfp-mapping.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">
<hibernate-mapping>
	<class name="model.studentinfo" table="studentinfo">
		<id name="id" column="id">
			<generator class="native"></generator>
		</id>
		<property name="name" column="name"></property>
		<property name="aihao" column="aihao"></property>
		<property name="sjina" column="sjina"></property>
		<set name="studentxxbs">
			<key>
				<column name="student_xxbs"></column>
			</key>
		<one-to-many class="model.studentxxb"/>
		</set>
	</class>

</hibernate-mapping>

  

      2). 实体类studentxxb.java的配置(studentxxb-mapping.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">
<hibernate-mapping>
	<class name="model.studentxxb" table="studentxxb">
		<id name="id" column="id">
			<generator class="native"></generator>
		</id>
		<property name="dhua" column="dhua"></property>
		<property name="diz" column="diz"></property>
             <many-to-one name="studentinfos" class="model.studentinfo" column="student_infos"></many-to-one> </class> </hibernate-mapping>

  3)接下来配置cfg.xml文件这里命名为(hibernate.cfg.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">
<hibernate-mapping>
	<class name="model.studentinfo" table="studentinfo">
		<id name="id" column="id">
			<generator class="native"></generator>
		</id>
		<property name="name" column="name"></property>
		<property name="aihao" column="aihao"></property>
		<property name="sjina" column="sjina"></property>
		<set name="studentxxbs">
			<key>
				<column name="student_xxbs"></column>
			</key>
		<one-to-many class="model.studentxxb"/>
		</set>
	</class>

</hibernate-mapping>

  4)测试类 Testmain.java:

    

package test;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import model.studentinfo;
import model.studentxxb;

public class Testmain1 {
	public static void main(String[] args) {
			Session session=new Configuration().configure().buildSessionFactory().openSession();
			 Transaction tt=session.beginTransaction();
			 
			 studentinfo studentinfo=new studentinfo();
			 studentinfo.setName("王胖子");
			 studentinfo.setAihao("爬山");
			 studentinfo.setSjina(new Date());
			 
			 studentxxb xxb=new studentxxb();
			 xxb.setDiz("是是是");
			 xxb.setDhua("1111111");
			 studentinfo.getStudentxxbs().add(xxb);
			 xxb.setStudentinfos(studentinfo);
			 session.save(xxb);
			 session.save(studentinfo);
			 tt.commit();
			 session.close();
	}
}

  5)控制台运行结果:

    

 

 

 

 

 

   6)数据库运行结果:

          

 

 

 *先导包

 

 

    

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值