问个用hibernate来实现的初级的问题:
我现在有两张表:
stu表
stu_id
stu_name
class_id 是class表的外键
class表
class_id
class_name
class表中有一些班级的数据
我现在想增加一个学生,选择一个班级保存到stu表中.这个stu和class两个表的pojo文件和hbm文件应该怎么写,请大侠写个示例指点一下.
问个用hibernate来实现的初级的问题:
我现在有两张表:
stu表
stu_id
stu_name
class_id 是class表的外键
class表
class_id
class_name
class表中有一些班级的数据
我现在想增加一个学生,选择一个班级保存到stu表中.这个stu和class两个表的pojo文件和hbm文件应该怎么写,请大侠写个示例指点一下.
这个就是一对多的关系
[color=red]Stu.hbm.xml[/color]
[code="java"]
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
name="Stu"
table="stu"
>
false
name="Id"
type="integer"
column="stu_id"
>
<property
name="StuName"
column="stu_name"
type="string"
not-null="false"
length="20"
/>
<many-to-one
name="Class"
column="class_id"
class="Myclazz"
not-null="false"
>
</many-to-one>
</class>
[/code]
Stu类
[code="java"]
package com.haha.base;
import java.io.Serializable;
/**
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";
// constructors
public 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 key
private java.lang.Integer id;
// fields
private java.lang.String stuName;
// many to one
private 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();
}
}
[/code]
[color=red]Myclazz.hbm.xml/color
[code="java"]
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
name="Myclazz"
table="myclazz"
>
false
name="Id"
type="integer"
column="class_id"
>
<property
name="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>
[/code]
MyClazz类
[code="java"]
package com.haha.base;
import java.io.Serializable;
/**
public abstract class BaseMyclazz implements Serializable {
public static String REF = "Myclazz";
public static String PROP_CLASS_NAME = "ClassName";
public static String PROP_ID = "Id";
// constructors
public 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 key
private java.lang.Integer id;
// fields
private java.lang.String className;
// collections
private 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();
}
}
[/code]