浏览 4696 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-11-06
Student.java package com.egt.vo; public class Student implements java.io.Serializable { private Long id; private Stuinfo stuinfo = new Stuinfo(); private String name; private String pass; /** default constructor */ public Student() { } /** minimal constructor */ public Student(Long id) { this.id = id; } /** full constructor */ public Student(Long id, Stuinfo stuinfo, String name, String pass) { this.id = id; this.stuinfo = stuinfo; this.name = name; this.pass = pass; } // Property accessors public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public Stuinfo getStuinfo() { return this.stuinfo; } public void setStuinfo(Stuinfo stuinfo) { this.stuinfo = stuinfo; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getPass() { return this.pass; } public void setPass(String pass) { this.pass = pass; } }
Student.hbm.xml <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse - Hibernate Tools --> <hibernate-mapping> <class name="com.egt.vo.Student" table="STUDENT" schema="CHEN"> <id name="id" type="java.lang.Long"> <column name="ID" precision="22" scale="0" /> <generator class="increment" /> </id> <many-to-one name="stuinfo" class="com.egt.vo.Stuinfo" fetch="select" cascade="all"> <column name="STUID" precision="22" scale="0" /> </many-to-one> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <property name="pass" type="java.lang.String"> <column name="PASS" /> </property> </class> </hibernate-mapping>
Stuinfo.java package com.egt.vo; import java.util.HashSet; import java.util.Set; /** * Stuinfo generated by MyEclipse - Hibernate Tools */ public class Stuinfo implements java.io.Serializable { // Fields private Long stuid; private String sex; private Long age; private Set students = new HashSet(0); // Constructors /** default constructor */ public Stuinfo() { } /** minimal constructor */ public Stuinfo(Long stuid) { this.stuid = stuid; } /** full constructor */ public Stuinfo(Long stuid, String sex, Long age, Set students) { this.stuid = stuid; this.sex = sex; this.age = age; this.students = students; } // Property accessors public Long getStuid() { return this.stuid; } public void setStuid(Long stuid) { this.stuid = stuid; } public String getSex() { return this.sex; } public void setSex(String sex) { this.sex = sex; } public Long getAge() { return this.age; } public void setAge(Long age) { this.age = age; } public Set getStudents() { return this.students; } public void setStudents(Set students) { this.students = students; } }
Stuinfo.hbm.xml <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse - Hibernate Tools --> <hibernate-mapping> <class name="com.egt.vo.Stuinfo" table="STUINFO" schema="CHEN"> <id name="stuid" type="java.lang.Long"> <column name="STUID" precision="22" scale="0" /> <generator class="increment" /> </id> <property name="sex" type="java.lang.String"> <column name="SEX" /> </property> <property name="age" type="java.lang.Long"> <column name="AGE" precision="22" scale="0" /> </property> <set name="students" inverse="true" cascade="all"> <key> <column name="STUID" precision="22" scale="0" /> </key> <one-to-many class="com.egt.vo.Student" /> </set> </class> </hibernate-mapping>
Hibernate.cfg.xml <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="dialect"> org.hibernate.dialect.Oracle9Dialect </property> <property name="connection.url"> jdbc:oracle:thin:@127.0.0.1:1521:DB </property> <property name="connection.username">chen</property> <property name="connection.password">123</property> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </property> <property name="myeclipse.connection.profile">DB</property> <property name="show_sql">true</property> <mapping resource="com/egt/vo/Student.hbm.xml" /> <mapping resource="com/egt/vo/Stuinfo.hbm.xml" /> </session-factory> </hibernate-configuration>
以下是对两表中的相应的增、删、查、改的操作的方法; package com.egt.dao; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.egt.vo.Student; import com.egt.vo.Stuinfo; public class Dao { Session session; public Dao(){ Configuration con = new Configuration().configure(); SessionFactory sessionFactory = con.buildSessionFactory(); session = sessionFactory.openSession(); } public void sel(){ String hql = "from Student student left join fetch student.stuinfo info where student.stuinfo = info.stuid "; Query query = this.session.createQuery(hql);//编译查询语句; List list = query.list();//将结果放入一个容器中; System.out.println("姓名"+" "+"密码" ); //以循环的方式把所有的记录都输出来; for(int i=0;i<list.size();i++){ Student stu = (Student)list.get(i); System.out.println(stu.getName()+"|| "+stu.getPass()+" ||"+stu.getId()+" ||"+stu.getStuinfo().getSex()+" ||"+stu.getStuinfo().getAge());; } session.close();//关闭事务; } public void add(Student vo){ Transaction tra = session.beginTransaction();//创建一个事务; Stuinfo stuinfo = vo.getStuinfo();//得到stuinfo表中的信息; session.save(stuinfo);//保存子表的信息; session.save(vo);//保存主表的信息; tra.commit();//提交事务; session.close();//关闭session对象; } public void del(Student vo){ Transaction tra = session.beginTransaction();//创建一个事务; String hql = "from Student where id="+vo.getId();//查询要删除的记录的ID号; Query query = session.createQuery(hql);//编译查询语句; Student student = (Student) query.list().get(0);//将查询出来的记录放入一个对象中; session.delete(student);//将查询出来的记录给删除; tra.commit();//提交事务; session.close();//关闭session对象; } public void upd(Student vo){ Transaction tra = session.beginTransaction();//创建一个事务; session.saveOrUpdate(vo);//调用修改方法; tra.commit();//提交事务; session.close();//关闭事务; } public Student getStudent(Student stu){ String hql = "from Student where id="+stu.getId();//查询要修改的记录的ID号; Query q = session.createQuery(hql);//编译查询语句; Student stu1 = (Student) q.list().get(0);//把查询到的记录放入到一个对象中; System.out.println(stu1.getName()+" "+stu1.getPass()+" "+stu1.getId()+" "+stu1.getStuinfo().getSex()+" "+stu1.getStuinfo().getAge()); // Transaction tra = session.beginTransaction(); // tra.commit(); // session.close(); return stu1;//把值返回; // return (Student) session.load(Student.class,stu.getId()); } }
对以上的方法的调用; package com.egt.daoImp; import com.egt.dao.Dao; import com.egt.vo.Student; import com.egt.vo.Stuinfo; public class DaoImp { /** * @param args */ public static void main(String[] args) { // 查询两表中所有的记录; Dao dao = new Dao(); // dao.sel(); //向两表添加一条数据; // Student student = new Student();//创建一个对象; // Stuinfo info = new Stuinfo(); // info.setAge(333L);//设置stuinfo表中的年龄、性别的值; // info.setSex("1"); // student.setName("ses you");//设置主表student表中的姓名、密码的值; // student.setPass("123456"); // student.setStuinfo(info);//将了表stuinfo中的值保存到表中; // dao.add(student);//将主表student中的值保存到表中; //以ID号来删除两表中的记录; // Student student = new Student();//创建一个对象; // Stuinfo info = new Stuinfo(); // student.setId(15L);//设置主表中要删除的ID字段的值; // dao.del(student);//删除主表中对ID号的字段,子表中的记录也随着删除; //以ID号得到的记录来修改记录; Student student = new Student();//创建一个对象; student.setId(14L);//设置主表中ID号的值; Student stu = dao.getStudent(student);//调用DAO中以ID号来查询单条记录的方法; stu.setName("来吧");//设置主表中要修改的字段的值; stu.setPass("liucljik"); Stuinfo info = stu.getStuinfo();//以学生来找到对应的学生信息; info.setAge(99L);//设置子表中要修改的字段的值; info.setSex("0"); stu.setStuinfo(info);//修改子表中的对应字段的值; dao.upd(stu);//修改主表中的对应字段的值; //以ID号来查询单一的记录; // Student stu = new Student();//创建一个对象; // stu.setId(3L);//设置要查询的记录的ID号; // System.out.println(stu.getName()+" "+stu.getPass()+" "+stu.getId()+" "+stu.getStuinfo().getSex()+" "+stu.getStuinfo().getAge()); // dao.getStudent(stu);//调用方法; } } 这是自己写的一个两表的增、删、查、改的操作; 如有错误,请指出,我是一个刚接触编程的菜鸟; 以下是我所操作的数据库表; 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-11-07
LZ写的不错,加油呼
|
|
返回顶楼 | |
发表时间:2008-11-07
有反馈的的学习,学会分享了了。好样的
|
|
返回顶楼 | |
发表时间:2008-11-07
有什么不对的地方还请多多指出!
|
|
返回顶楼 | |
发表时间:2008-11-12
楼主很不错 但是我还很疑问
Transaction tra = session.beginTransaction();//创建一个事务; Stuinfo stuinfo = vo.getStuinfo();//得到stuinfo表中的信息; session.save(stuinfo);//保存子表的信息; session.save(vo);//保存主表的信息; tra.commit();//提交事务; session.close();//关闭session对象; ------------------------------------ 看你这里 我都分不清哪个是子表哪个是主表了 <set name="students" inverse="true" cascade="all"> <key> <column name="STUID" precision="22" scale="0" /> </key> <one-to-many class="com.egt.vo.Student" /> </set> ----------------- 主表应该是Stuinfo吧? |
|
返回顶楼 | |
发表时间:2008-11-20
pyzheng 写道 楼主很不错 但是我还很疑问Transaction tra = session.beginTransaction();//创建一个事务; Stuinfo stuinfo = vo.getStuinfo();//得到stuinfo表中的信息; session.save(stuinfo);//保存子表的信息; session.save(vo);//保存主表的信息; tra.commit();//提交事务; session.close();//关闭session对象;------------------------------------看你这里 我都分不清哪个是子表哪个是主表了<set name="students" inverse="true" cascade="all"> <key> <column name="STUID" precision="22" scale="0" /> </key> <one-to-many class="com.egt.vo.Student" /> </set> -----------------主表应该是Stuinfo吧? 我这里的主表是student,子表是stuinfo! |
|
返回顶楼 | |
发表时间:2009-07-13
ok,你把timestamp和date类型的数据显示一下,谢谢~!
|
|
返回顶楼 | |