问题描述
问个用hibernate来实现的初级的问题:我现在有两张表:stu表stu_idstu_nameclass_id 是class表的外键class表class_idclass_nameclass表中有一些班级的数据我现在想增加一个学生,选择一个班级保存到stu表中.这个stu和class两个表的pojo文件和hbm文件应该怎么写,请大侠写个示例指点一下.
解决方案
这个就是一对多的关系Stu.hbm.xml<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" ><hibernate-mapping package="com.haha"><classname="Stu"table="stu"><meta attribute="sync-DAO">false</meta><idname="Id"type="integer"column="stu_id"><generator class="org.hibernate.id.IdentityGenerator"/></id><propertyname="StuName"column="stu_name"type="string"not-null="false"length="20"/><many-to-onename="Class"column="class_id"class="Myclazz"not-null="false"></many-to-one></class></hibernate-mapping>Stu类package com.haha.base;import java.io.Serializable;/** * This is an object that contains data related to the stu table. * Do not modify this class because it will be overwritten if the configuration file * related to this class is modified. * * @hibernate.class * table="stu" */public abstract class BaseStu implements Serializable {public static String REF = "Stu";public static String PROP_STU_NAME = "StuName";public static String PROP_CLASS = "Class";public static String PROP_ID = "Id";// constructorspublic BaseStu () {initialize();}/** * Constructor for primary key */public BaseStu (java.lang.Integer id) {this.setId(id);initialize();}protected void initialize () {}private int hashCode = Integer.MIN_VALUE;// primary keyprivate java.lang.Integer id;// fieldsprivate java.lang.String stuName;// many to oneprivate com.haha.Myclazz m_class;/** * Return the unique identifier of this class * @hibernate.id * generator-class="org.hibernate.id.IdentityGenerator" * column="stu_id" */public java.lang.Integer getId () {return id;}/** * Set the unique identifier of this class * @param id the new ID */public void setId (java.lang.Integer id) {this.id = id;this.hashCode = Integer.MIN_VALUE;}/** * Return the value associated with the column: stu_name */public java.lang.String getStuName () {return stuName;}/** * Set the value related to the column: stu_name * @param stuName the stu_name value */public void setStuName (java.lang.String stuName) {this.stuName = stuName;}/** * Return the value associated with the column: class_id */public com.haha.Myclazz getMyClass () {return m_class;}/** * Set the value related to the column: class_id * @param m_class the class_id value */public void setClass (com.haha.Myclazz m_class) {this.m_class = m_class;}public boolean equals (Object obj) {if (null == obj) return false;if (!(obj instanceof com.haha.Stu)) return false;else {com.haha.Stu stu = (com.haha.Stu) obj;if (null == this.getId() || null == stu.getId()) return false;else return (this.getId().equals(stu.getId()));}}public int hashCode () {if (Integer.MIN_VALUE == this.hashCode) {if (null == this.getId()) return super.hashCode();else {String hashStr = this.getMyClass().getClassName() + ":" + this.getId().hashCode();this.hashCode = hashStr.hashCode();}}return this.hashCode;}public String toString () {return super.toString();}}Myclazz.hbm.xml(也就是班级表)<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" ><hibernate-mapping package="com.haha"><classname="Myclazz"table="myclazz"><meta attribute="sync-DAO">false</meta><idname="Id"type="integer"column="class_id"><generator class="org.hibernate.id.IdentityGenerator"/></id><propertyname="ClassName"column="class_name"type="string"not-null="false"length="20"/><set name="Stus" inverse="true"><key column="class_id"/><one-to-many class="Stu"/></set></class></hibernate-mapping>MyClazz类package com.haha.base;import java.io.Serializable;/** * This is an object that contains data related to the myclazz table. * Do not modify this class because it will be overwritten if the configuration file * related to this class is modified. * * @hibernate.class * table="myclazz" */public abstract class BaseMyclazz implements Serializable {public static String REF = "Myclazz";public static String PROP_CLASS_NAME = "ClassName";public static String PROP_ID = "Id";// constructorspublic BaseMyclazz () {initialize();}/** * Constructor for primary key */public BaseMyclazz (java.lang.Integer id) {this.setId(id);initialize();}protected void initialize () {}private int hashCode = Integer.MIN_VALUE;// primary keyprivate java.lang.Integer id;// fieldsprivate java.lang.String className;// collectionsprivate java.util.Set<com.haha.Stu> stus;/** * Return the unique identifier of this class * @hibernate.id * generator-class="org.hibernate.id.IdentityGenerator" * column="class_id" */public java.lang.Integer getId () {return id;}/** * Set the unique identifier of this class * @param id the new ID */public void setId (java.lang.Integer id) {this.id = id;this.hashCode = Integer.MIN_VALUE;}/** * Return the value associated with the column: class_name */public java.lang.String getClassName () {return className;}/** * Set the value related to the column: class_name * @param className the class_name value */public void setClassName (java.lang.String className) {this.className = className;}/** * Return the value associated with the column: Stus */public java.util.Set<com.haha.Stu> getStus () {return stus;}/** * Set the value related to the column: Stus * @param stus the Stus value */public void setStus (java.util.Set<com.haha.Stu> stus) {this.stus = stus;}public void addToStus (com.haha.Stu stu) {if (null == getStus()) setStus(new java.util.TreeSet<com.haha.Stu>());getStus().add(stu);}public boolean equals (Object obj) {if (null == obj) return false;if (!(obj instanceof com.haha.Myclazz)) return false;else {com.haha.Myclazz myclazz = (com.haha.Myclazz) obj;if (null == this.getId() || null == myclazz.getId()) return false;else return (this.getId().equals(myclazz.getId()));}}public int hashCode () {if (Integer.MIN_VALUE == this.hashCode) {if (null == this.getId()) return super.hashCode();else {String hashStr = this.getClass().getName() + ":" + this.getId().hashCode();this.hashCode = hashStr.hashCode();}}return this.hashCode;}public String toString () {return super.toString();}}
解决方案二:
自己动手写,楼上的也太不厚道了,生成的吧?就事第一人回答的,看看一对多,orm这方面做的很好无论hibernate还是gorm,非常方便
解决方案三:
class 表中肯定要有stu表的一个外键外键弄好之后.看一下mony-to-one 我就清楚了.