hibernate中新增记录的问题

问题描述

问个用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 我就清楚了.

时间: 2024-09-20 01:08:09

hibernate中新增记录的问题的相关文章

hibernate-Oracle数据库中新增一个字段,Hibernate中映射怎样加?

问题描述 Oracle数据库中新增一个字段,Hibernate中映射怎样加? 问题:我在oracle数据库中新增一个字段,那么在Hibernate映射中,需要手动加进去吗?在中. 谢谢!! 解决方案 类似这样,第一个name对应实体中,第二个name对应数据库 解决方案二:

不同结构的表数据新增-SQL 不同表结构的两张表A和B,往表中新增表B的数据,判断条件如果表A中不存在B中的记录

问题描述 SQL 不同表结构的两张表A和B,往表中新增表B的数据,判断条件如果表A中不存在B中的记录 insert into A(no,name,status)values ( select no,name,'Add' from B where B.no not in (select no from A) ) A(no,name,status) B(no,name) no是主键,上面这么写报错,请问要怎么改 ? 解决方案 insert into A(no,name,status) select

JDBC获取新增记录的自增主键

常常在使用Hibernate.iBatis时候,当插入一条数据的时候,实体Bean的原空主键就 赋上新值了. 在JDBC中,也可以通过变相的手法来做到这点.下面是个测试的小例子. 一.环境 MySQL5.1 mysql-connector-java-5.1.10 jdk1.5 CREATE TABLE book( code bigint(20) unsigned NOT NULL AUTO_INCREMENT, kind varchar(45) NOT NULL, name varchar(45

Hibernate中新的TableGenerator 机制

从 initialValue 说起 问题的发现源自对 JPA 中 TableGenerator 的测试.测试的环境有这样几个条件: 为方便查询的测试,Employee 表格在初始化时会导入部分记录,这部分记录的主键在初始脚本中手动写好,比如 1.2.3.4.(参看文章所附示例代码中的import_data.sql 文件). Employee 实体使用 TableGenerator 主键生成器,initialValue 的值设置为 10. 在单元测试中添加新的 Employee 记录. Emplo

Hibernate学习(二)Hibernate中Session之get和load方法的真正区别

最近在学习SHH框架中的Hibernate,对Session的get和load方法,有点混不清楚,不知道区别在哪,或者对它们的区别感触不深.所以百度了一下,结果问题来了.百度的结果和实际测试的结果出入很大.主要是对get方法的说法跟实际运行的结果不一致. 先说一下观点吧: get不支持lazy,load支持lazy: 数据不存在时,get返回null,load则抛出ObjectNotFoundException异常. load方法可以返回实体的代理类实例,而get方法直接读取数据库,所以直接返回

Hibernate中load和get的区别

Hibernate中Session.load/get方法均可以根据指定的实体类和id从数据库读取记录,并返回与之对应的实体对象.其区别在于: 1.get()方法直接返回实体类,load()方法可以返回实体的代理类实例. 2.hibernate load是采用延迟机制(当lazy属性为true时) 而get不采用延迟机制(get语句马上读库) 3.找不到符合条件的数据 get方法将返回null load将会报出ObjectNotFoundExcepion 4.get支持多态查询,load只有在la

Hibernate中Session的缓存及对象的状态

对于session这个接口的学习可以说是最痛苦也是最复杂的,因为它所涉及的方面太多了,一些隐藏的机制也很多,谁让它是Central API呢. 对于它的几个最基本的方法如save().delete().flush()等的学习都花了我一定的时间.在深入了解这些这些方法前,了解session的缓存机制以及Hibernate中Java对象的状态对我们是很有帮助的. 一.Session的缓存 Java是纯面向对象的语言,因此不可能像C语言那样直接操纵内存,例如声明一段可用的内存空间.在Java里面,缓存

ASP获取新增记录ID值的方法

  这篇文章主要介绍了ASP获取新增记录ID值的方法,分别介绍了ASP+Access2000.ASP+SQL Server 2000两种数据库的获取方法,需要的朋友可以参考下 ASP+Access2000 1.要获取的ID值字段属性必须设为:自动编号(我们假设字段名为recordID) 2.添加记录格式:Rs.Open table,Cn,1,3 注意模式为:1,3 3.newID = rs.Fields("recordID") 4.newID为刚添加的记录的ID值 ASP+SQL Se

第四章 Hibernate中的持久化对象的生命周期

4.1提供对象状态管理的目的      使开发者不再需要理会底层数据库系统的细节      使用Hibernate的开发者应该总是关注对象的状态(state),不必考虑SQL语句的执行 4.2 Hibernate 仅仅定义了三种状态:瞬时(临时).持久和脱管(游离),对客户端代码隐藏了其内部实现的复杂性 4.2.1 瞬时对象(Transient Object):Hibernate中的持久化对象的生命周期         使用new操作符初始化的对象不是立刻就持久的.它们的状态是瞬时的,也就是说它