Java EE实用教程( 第 3 版 )Hibernate 实验二【完成教材第216页实例8.8】

完成教材第216页实例8.8

项目结构

Java EE实用教程( 第 3 版 )Hibernate 实验二【完成教材第216页实例8.8】_第1张图片

大部分还是通过Hibernate开发能力自动导的
如果还不会的话,请参考我的这篇文章:

Hibernate框架连接MySQL ( 使用Navicat ) 完整详细步骤

效果图

course表
Java EE实用教程( 第 3 版 )Hibernate 实验二【完成教材第216页实例8.8】_第2张图片
stu_cour表

Java EE实用教程( 第 3 版 )Hibernate 实验二【完成教材第216页实例8.8】_第3张图片
student表
Java EE实用教程( 第 3 版 )Hibernate 实验二【完成教材第216页实例8.8】_第4张图片

核心代码

HibernateTest.java

package org.test;

import java.text.ParseException;
import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.util.HibernateSessionFactory;
import org.vo.Course;
import org.vo.Student;


public class HibernateTest {
	public static Session session;
	public static void main(String[] args) throws ParseException {
		HibernateTest ht=new HibernateTest();
		ht.getCurrentSession();
		Transaction ts=session.beginTransaction();
		Course c1=new Course();
		Course c2=new Course();
		Course c3=new Course();
		c1.setCnumber("104");
		c1.setCname("C++");
		c2.setCnumber("105");
		c2.setCname("计算机系统结构");
		c3.setCnumber("106");
		c3.setCname("微机原理与接口技术");
		Set courses=new HashSet();
		courses.add(c1);
		courses.add(c2);
		courses.add(c3);
		Student stu=new Student();
		stu.setSnumber("201701020135");
		stu.setSname("miaoz");
		stu.setSage(20);
		stu.setCourses(courses);
		session.save(stu);
		ts.commit();
		System.out.println("操作成功!");
		ht.closeSession();
		
	}	
	//获得Session方法
	public void getCurrentSession(){
		session=HibernateSessionFactory.getSession();
	}
	//关闭Seesion方法
	public void closeSession(){
		if(session!=null){
			HibernateSessionFactory.closeSession();
		}
	}
}

Course.java

package org.vo;

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

/**
 * Course entity. @author MyEclipse Persistence Tools
 */

public class Course implements java.io.Serializable {

	// Fields

	private Integer id;
	private String cnumber;
	private String cname;
	private Set stus=new HashSet();
	// Constructors

	public Set getStus() {
		return stus;
	}

	public void setStus(Set stus) {
		this.stus = stus;
	}

	/** default constructor */
	public Course() {
	}

	/** minimal constructor */
	public Course(String cnumber) {
		this.cnumber = cnumber;
	}

	/** full constructor */
	public Course(String cnumber, String cname) {
		this.cnumber = cnumber;
		this.cname = cname;
	}

	// Property accessors

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getCnumber() {
		return this.cnumber;
	}

	public void setCnumber(String cnumber) {
		this.cnumber = cnumber;
	}

	public String getCname() {
		return this.cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}
}

Student.java

package org.vo;

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

/**
 * Student entity. @author MyEclipse Persistence Tools
 */

public class Student implements java.io.Serializable {

	// Fields

	private Integer id;
	private String snumber;
	private String sname;
	private Integer sage;
	private Set courses = new HashSet();
	
	// Constructors

	public Set getCourses() {
		return courses;
	}

	public void setCourses(Set courses) {
		this.courses = courses;
	}

	/** default constructor */
	public Student() {
	}

	/** minimal constructor */
	public Student(String snumber) {
		this.snumber = snumber;
	}

	/** full constructor */
	public Student(String snumber, String sname, Integer sage) {
		this.snumber = snumber;
		this.sname = sname;
		this.sage = sage;
	}

	// Property accessors

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getSnumber() {
		return this.snumber;
	}

	public void setSnumber(String snumber) {
		this.snumber = snumber;
	}

	public String getSname() {
		return this.sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

	public Integer getSage() {
		return this.sage;
	}

	public void setSage(Integer sage) {
		this.sage = sage;
	}
}

Course.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">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="org.vo.Course" table="course" catalog="test">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="cnumber" type="java.lang.String">
            <column name="CNUMBER" length="50" not-null="true" />
        </property>
        <property name="cname" type="java.lang.String">
            <column name="CNAME" length="50" />
        </property>
        <set name="stus" table="stu_cour" lazy="true" cascade="all">
        	<key column="CID"></key>
        	<many-to-many class="org.vo.Student" column="SID"/>
        </set>
        
    </class>
</hibernate-mapping>

Student.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">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="org.vo.Student" table="student" catalog="test">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="snumber" type="java.lang.String">
            <column name="SNUMBER" length="50" not-null="true" />
        </property>
        <property name="sname" type="java.lang.String">
            <column name="SNAME" length="500" />
        </property>
        <property name="sage" type="java.lang.Integer">
            <column name="SAGE" />
        </property>
        
        <set name="courses" table="stu_cour" lazy="true" cascade="all">
        	<key column="SID"></key>
        	<many-to-many class="org.vo.Course" column="CID"/>
        </set>
        
    </class>
</hibernate-mapping>

学如逆水行舟,不进则退

你可能感兴趣的:(Java)