跟我学Hibernate框架技术——在容器外实现“一对一”的关联(第1部分)_第1页
跟我学Hibernate框架技术——在容器外实现“一对一”的关联(第1部分)_第2页
跟我学Hibernate框架技术——在容器外实现“一对一”的关联(第1部分)_第3页
跟我学Hibernate框架技术——在容器外实现“一对一”的关联(第1部分)_第4页
跟我学Hibernate框架技术——在容器外实现“一对一”的关联(第1部分)_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、杨教授工作室 精心创作的优秀程序员 职业提升必读系列资料目 录1.1在容器外实现“一对一”的关联(第1部分)21.1.1对“一对一”的关联关系的分析21.1.2在容器外实现“一对一”的关联31.1.3映射前面所设计的ebook数据库表-声明它的映射元数据71.1.4测试该dao组件的应用效果151.1 在容器外实现“一对一”的关联(第1部分)1.1.1 对“一对一”的关联关系的分析1、什么是“一对一”关联关系ebook是由某个出版社所出版,即ebook与publish之间为“一对一”的关系。其中的ebook类为主控方,而publish类为被动方。one to onepublishebook2、

2、采用什么技术实现手段来解决“一对一”关联关系为什么可以应用“主键关联”?首先要了解对应的数据库表的结构,观察数据库表结构是否满足“主键关联”的数据库要求。上面的两个数据库表没有采用主外键设计方案,同时再观察两个数据库表中的主键的值是否一致性。因此,可以应用“主键关联”的技术实现方案。1.1.2 在容器外实现“一对一”的关联1、在项目中保证有hibernate.cfg.xml文件2、在项目中保证有perties属性文件3、在项目中添加一个ebook的po类(1)本例中的实体类ebook代码,类名称为ebook,包名称为com.px1987.sshwebcrm.dao.pobje

3、ct添加各个属性以及对应的get/set方法private integer ebook_id; private string ebookname; private char ebookkind; private float ebookprice;最后的代码如下在该java类中包含每个变量的getter和setter函数以及辅助函数equals等,请见下面的代码。package com.px1987.sshwebcrm.dao.pobject;本例将ebook_id 属性的类型设置为integerpublic class ebook private integer ebook_id; overr

4、idepublic int hashcode() final int prime = 31;int result = 1;result = prime * result + ebookkind;result = prime * result+ (ebookname = null) ? 0 : ebookname.hashcode();result = prime * result + float.floattointbits(ebookprice);result = prime * result+ (ebook_id = null) ? 0 : ebook_id.hashcode();retu

5、rn result;注意实现equals()方法的应用的场合overridepublic boolean equals(object obj) if (this = obj)return true;if (obj = null)return false;if (getclass() != obj.getclass()return false;ebook other = (ebook) obj;if (ebookkind != other.ebookkind)return false;if (ebookname = null) if (other.ebookname != null)return

6、 false; else if (!ebookname.equals(other.ebookname)return false;if (float.floattointbits(ebookprice) != float.floattointbits(other.ebookprice)return false;if (ebook_id = null) if (other.ebook_id != null)return false; else if (!ebook_id.equals(other.ebook_id)return false;return true;public integer ge

7、tebook_id() return ebook_id;public void setebook_id(integer ebook_id) this.ebook_id = ebook_id;public string getebookname() return ebookname;public void setebookname(string ebookname) this.ebookname = ebookname;public char getebookkind() return ebookkind;public void setebookkind(char ebookkind) this

8、.ebookkind = ebookkind;public float getebookprice() return ebookprice;public void setebookprice(float ebookprice) this.ebookprice = ebookprice;private string ebookname; private char ebookkind; private float ebookprice;public ebook() 1.1.3 映射前面所设计的ebook数据库表-声明它的映射元数据对象和关系数据库之间的映射通常是用一个xml文档(xml docum

9、ent)来定义的。这个映射文档被设计为易读的,并且可以手工修改。映射语言是以java为中心,这意味着映射文档是按照持久化类的定义来创建的,而非表的定义。1、在src的目录下新建一个ebook.hbm.xml映射文件2、设计其内容下面为ebook.hbm.xml映射文件的内容 在xml?映射文件中,写类的名字时一定要用类的全名(包+类名) !- - hibernate缺省使用属性名作为字段(column)名 3、在hibernate.cfg.xml文件中添加对ebook.hbm.xml的引用定义4、为应用系统提供一个threadlocal形式的session 管理方案新增一个hibernateu

10、til类5、为该应用提供一个dao接口和对应的实现类(1)添加一个dao的接口 webhibernatedaointerface,包名称为erpackage er;import java.util.arraylist;import org.hibernate.hibernateexception;public interface webhibernatedaointerface public arraylist doselectebookdatafromdb(string selec

11、thql) throws hibernateexception; public boolean doinsertebookdatatodb()throws hibernateexception,java.io.unsupportedencodingexception;6、为上面的dao的接口提供一个实现类webhibernatedaobean,包名称为com.px1987.sshwebcrm.dao.imple代码如下:package com.px1987.sshwebcrm.dao.imple;import java.io.unsupportedencodingexception;impor

12、t java.util.arraylist;import java.util.list;import mons.logging.log;import mons.logging.logfactory;import org.hibernate.hibernateexception;import org.hibernate.session;import org.hibernate.transaction;import er.webhibernatedaointerface;import c

13、om.px1987.sshwebcrm.dao.pobject.ebook;import com.px1987.sshwebcrm.dao.util.hibernateutil;public class webhibernatedaobean implements webhibernatedaointerface private static log log = logfactory.getlog(hibernatedaobean.class);public webhibernatedaobean() public boolean doinsertebookdatatodb() throws

14、hibernateexception,unsupportedencodingexception session session=null;transaction tx=null;trysession = hibernateutil.currentsession();/*下面的代码相当于我们执行了以下sql语句insert into ebook (ebook_id,ebookname,ebookkind,ebookpricfe) values (1, j2ee应用开发,1,7.4f) */tx= session.begintransaction();ebook oneebook = new eb

15、ook();/*对某些数据库系统如mssqlserver2000必须进行字符的编码转换,否则会出现中文乱码。而对于jdts驱动则不需要进行编码转换oneebook.setebookname(j2ee应用开发);*/oneebook.setebookname(j2ee应用开发);oneebook.setebookkind(1);oneebook.setebookprice(7.4f);session.save(oneebook); /保存该实体mit(); catch(hibernateexception he) if ( tx!=null ) tx.rollback(); log.

16、error(在doinsertebookdatatodb方法中出现了hibernateexception错误, he); throw he; finally/*这样我们就可以随心所欲的多次调用hibernateutil.currentsession();,我们每次都会得到同一个当前线程的session。不管是在我们的servlet代码中,或者在servlet filter中还是在http结果返回之前,我们都必须确保这个session在你的数据库访问工作完成后关闭。*/ hibernateutil.closesession(); return true;public arraylist dose

17、lectebookdatafromdb(string selecthql)throws hibernateexception session session=null;transaction tx=null;arraylist totalebooklist =new arraylist();trysession = hibernateutil.currentsession();tx= session.begintransaction();list result = session.createquery(selecthql).list();for (int index = 0; index result.size(); index+) ebook oneebook = (ebook) result.get(index); totalebooklist.add(oneebook);mit(); catch(hibernateexception he)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论