2020-11-30

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.

      1).实体类studentinfo.java的配置(studentinfp-mapping.hbm.

        

<?

  

      2). 实体类studentxxb.java的配置(studentxxb-mapping.hbm.

          

<?            		<many-to-one name="studentinfos" column="student_infos"></many-to-one>			</class></hibernate-mapping>

  3)接下来配置cfg.

    

<?

  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)数据库运行结果:

          

 

 

 *先导包

 

 

    

 









原文转载:http://www.shaoqun.com/a/494414.html

沃尔码:https://www.ikjzd.com/w/220

易速:https://www.ikjzd.com/w/2389

c88是什么:https://www.ikjzd.com/w/1017.html


1.一对多 1).首先创建两个实体类studeninfo.java跟studentxxb.java    1)studentinfo.java表如图:packagemodel;importjava.util.Date;importjava.util.HashSet;importjava.util.Set;publicclassstudentinfo{ privateIntegerid; priva
锦桥纺织网:锦桥纺织网
tiki:tiki
去厦门旅游如何打车?:去厦门旅游如何打车?
泉州购物指引 - :泉州购物指引 -
日本传统节日简介 :日本传统节日简介

No comments:

Post a Comment