geekwang 2008-09-15 01:04
浏览 241
已采纳

hibernate中新增记录的问题

问个用hibernate来实现的初级的问题:
我现在有两张表:
stu表
stu_id
stu_name
class_id 是class表的外键

class表
class_id
class_name

class表中有一些班级的数据

我现在想增加一个学生,选择一个班级保存到stu表中.这个stu和class两个表的pojo文件和hbm文件应该怎么写,请大侠写个示例指点一下.

  • 写回答

3条回答 默认 最新

  • iteye_18036 2008-09-15 17:38
    关注

    这个就是一对多的关系
    [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;

    /**

    • 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";
    
    
    // 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;

    /**

    • 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";
    
    
    // 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]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!