您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 公司方案 > 实验一Hibernate的应用-Java企业级开发技术
实验一Hibernate的应用一.目的要求1.掌握Hibernate的关系映射。2.掌握用关系映射操纵关系及要注意的问题。二.实验任务接续JavaEE架构与程序设计实验五的内容,在book数据库中创建表bookType。表结构如下:表1-1bookType表结构列名数据类型长度是否允许为空说明idint4不允许为空主键,保持自动增长,步长为1typeNamevarchar60不允许为空图书类别名1.建立BookInfo和BookType的多对一的关联映射,并映射到bookinfo表和bookType表的外键关联。从多方完成以下任务:(1)建立图书类别“科技类”(2)建立图书《JavaEE架构与程序设计》、《SQLServer2012数据库应用与开发教程》、《嵌入式系统原理与接口技术》(3)将图书《JavaEE框架技术进阶式教程》、《SQLServer2012数据库应用与开发教程》、《嵌入式系统原理与接口技术》划入“科技类”。(4)获取图书《JavaEE框架技术进阶式教程》及其图书类别。2.建立BookType和BookInfo的一对多关联映射并映射到bookinfo表和bookType表的外键关联。从一方完成以下任务:获取科技类图书(1)建立图书类别“科技类”(2)建立图书《JavaEE架构与程序设计》、《SQLServer2012数据库应用与开发教程》、《嵌入式系统原理与接口技术》(3)将图书《JavaEE框架技术进阶式教程》、《SQLServer2012数据库应用与开发教程》、《嵌入式系统原理与接口技术》划入“科技类”。(4)获取图书《JavaEE框架技术进阶式教程》及其图书类别。三.实验环境、设备PC、Hibernate3.3.2和MyEclipse四.实验指导任务1:(1)创建数据库及表;(2)创建Java项目并加hibernate的jar包和MySQL数据库的驱动程序JAR包导入工程;(3)建立hibernate配置文件:hibernate.cfg.xml;(4)建立图书信息持久化类:BookInfoVo;(5)建立图书信息持久化类对应的映射文件;(6)建立图书类别持久化类:BookTypeVo;(7)建立图书类别持久化类对应的映射文件(8)创建图书信息管理的测试类,实现图书的添加:BookManage.java;(9)执行Java程序,显示运行结果。五.实验结果(1)源代码:?xmlversion=1.0encoding=UTF-8?!DOCTYPEhibernate-mappingPUBLIC-//Hibernate/HibernateMappingDTD3.0//EN=com.shxt.lesson03.hibernate.models!--每一个类class对应一个表table、table必须包含主键--classname=BookTypeidname=typeIdlength=10generatorclass=sequenceparamname=sequencetype_seq/param/generator/idpropertyname=typeNametype=stringlength=20/property/class/hibernate-mapping?xmlversion=1.0encoding=UTF-8?!DOCTYPEhibernate-mappingPUBLIC-//Hibernate/HibernateMappingDTD3.0//EN=com.shxt.lesson03.hibernate.modelsclassname=BookTypeidname=typeIdtype=integergeneratorclass=sequenceparamname=sequencetypeId_seq/param/generator/idpropertyname=typeNamelength=20/propertysetname=infoModelSetcascade=allkeycolumn=infomodel_ref_type/keyone-to-manyclass=bookInfoModel//set/classclassname=bookInfoModelidname=bookIdlength=10generatorclass=sequenceparamname=sequencebook_seq/param/generator/idpropertyname=bookNametype=stringlength=20/propertypropertyname=authortype=stringlength=20/propertypropertyname=bookIsbntype=stringlength=20/propertypropertyname=typeIdtype=stringlength=20/propertypropertyname=publishertype=stringlength=20/propertypropertyname=pricetype=double/propertymany-to-onename=typecolumn=infomodel_ref_typecascade=save-updateclass=BookType/many-to-one/class/hibernate-mappingpackagecom.shxt.lesson03.hibernate.models;importjava.util.Set;publicclassBookType{privateinttypeId;privateStringtypeName;privateSetbookInfoModelinfoModelSet;publicSetbookInfoModelgetInfoModelSet(){returninfoModelSet;}publicvoidsetInfoModelSet(SetbookInfoModelinfoModelSet){this.infoModelSet=infoModelSet;}publicintgetTypeId(){returntypeId;}publicvoidsetTypeId(inttypeId){this.typeId=typeId;}publicStringgetTypeName(){returntypeName;}publicvoidsetTypeName(StringtypeName){this.typeName=typeName;}}packagecom.shxt.lesson03.hibernate.test;importjava.util.List;importorg.hibernate.Query;importorg.hibernate.SessionFactory;importorg.hibernate.classic.Session;importcom.shxt.lesson03.hibernate.models.BookType;importcom.shxt.lesson03.hibernate.models.bookInfoModel;importcom.shxt.lesson03.hibernate.util.HibernateUtil;publicclassBookManage{publicvoidtestSave(){try{Sessionsession=HibernateUtil.getSessionFactory().openSession();session.beginTransaction();session.beginTransaction();BookTypebt=newBookType();bt.setTypeId(1);bt.setTypeName(科技类);session.save(bt);session.getTransaction().commit();session.close();}catch(Exceptione){e.printStackTrace();}}publicvoidtestList(){try{SessionFactoryfactory=HibernateUtil.getSessionFactory();Sessionsession=factory.openSession();Queryquery=session.createQuery(fromBookType);List?list=query.list();System.out.println(类型id\t类型);for(inti=0;ilist.size();i++){BookTypebt=(BookType)list.get(i);System.out.println(bt.getTypeId()+\t+bt.getTypeName());}session.close();}catch(Exceptione){e.printStackTrace();}}publicvoidtestUpdateMany(){try{Sessionsession=HibernateUtil.getSessionFactory().openSession();session.beginTransaction();bookInfoModelbim=newbookInfoModel();bim.setTypeId(1);BookTypebt=(BookType)session.load(BookType.class,1);bim.setType(bt);session.save(bim);session.getTransaction().commit();session.close();}catch(Exceptione){e.printStackTrace();}}publicvoidtestUpdateOne(){try{Sessionsession=HibernateUtil.getSessionFactory().openSession();session.beginTransaction();BookTypebt=(BookType)session.load(BookType.class,1);bookInfoModelbim=newbookInfoModel();bim.setTypeId(1);bt.getInfoModelSet().add(bim);session.getTransaction().commit();session.close();}catch(Exceptione){e.printStackTrace();}}publicstaticvoidmain(String[]args){BookManagebm=newBookManage();bm.testUpdateOne();}}(2)运行效果截图六.思考题1.试举例说明单向一对一,和双向多对多关联关系的配置步骤。hibernate一对一主键关联映射(单向关联Person----IdCard)一对一主键关联映射:让两个实体对象的id保持相同,这样可以避免多余的字段被创建具体映射:id
本文标题:实验一Hibernate的应用-Java企业级开发技术
链接地址:https://www.777doc.com/doc-2530811 .html