03Hibernate映射

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Hibernate映射基本映射主键生成策略uuid:随机生成通用唯一标识符,实体类的ID类型必须为Stringassigned:需要手动设定id,默认native:让数据库自动选择一种策略(根据底层数据库的能力选择identity、sequence或者hilo中的一个。)Number!DOCTYPEhibernate-mappingPUBLIC-//Hibernate/HibernateMappingDTD3.0//EN=net.vzhang.po.Usertable=t_useridname=idcolumn=idgeneratorclass=native//idpropertyname=namecolumn=name/propertyname=passwordcolumn=password//class/hibernate-mapping关联映射域模型(实体)之间的关联关系一对一(one-one)一个人一个身份证一对多(one-many,many-one)用户和用户组多对多(many-many)老师和学生域模型和关系模型一些区别:域模型中有继承关系,关系模型不能直接表示继承关系域模型中有多对多的关联关系,关系模型通过连接表来表示多对多的关联关系比如新闻表和类别表一条新闻可能属于多个类别薄熙来开庭,属于国内新闻同时也属于重大要闻New表NewsIdTitleContent8薄熙来开庭提出上诉ClassIdTitle1国内新闻2重大要闻IdNewsIdClassId181282域模型中有双向关联关系,关系模型只有单向参照关系,而且总是many方参照one方所推崇的粒度不一样,域模型提倡精粒度模型,而关系模型提倡粗粒度模型关联映射将关联关系映射到数据库,在实体模型中关联关系就是对象模型在内存中的一个或多个引用Many-One在many方的映射文件里使用many-to-one标签,会在该模型的关系模型里面加入一个外键,默认是与one的一方的主键关联的。数据表:这里讲一下创建数据库写实体类先叫Groupackageorg.zshwd.hibernate.vo;importjavax.persistence.Entity;importjavax.persistence.Id;@EntitypublicclassGrou{privateintid;privateStringname;@IdpublicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}}配置文件mappingclass=org.zshwd.hibernate.vo.Grou/写TestGrou.javapackageorg.zshwd.hibernate.test;importorg.hibernate.Session;importorg.hibernate.SessionFactory;importorg.zshwd.hibernate.util.HibernateUtil;importorg.zshwd.hibernate.vo.Grou;publicclassTestGroup{publicstaticvoidmain(String[]args){Grougroup=newGrou();group.setName(测试);SessionFactorysf=HibernateUtil.getSessionFactory();Sessionsession=sf.openSession();session.beginTransaction();session.save(group);session.getTransaction().commit();session.close();sf.close();}}propertyname=hbm2ddl.autocreate/property以上完成了Grop表的建立现在我们再来写Group.javapackageorg.zshwd.hibernate.vo;publicclassGroup{privateintid;privateStringname;publicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}}User表实体类packageorg.zshwd.hibernate.vo;publicclassUser{privateintid;privateStringname;privateStringpassword;privateGroupgroup;publicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicStringgetPassword(){returnpassword;}publicvoidsetPassword(Stringpassword){this.password=password;}publicGroupgetGroup(){returngroup;}publicvoidsetGroup(Groupgroup){this.group=group;}}写hibernate.cfg.xmlmappingresource=org/zshwd/hibernate/vo/User.hbm.xml/mappingresource=org/zshwd/hibernate/vo/Group.hbm.xml/配置Group.hbm.xml!DOCTYPEhibernate-mappingPUBLIC-//Hibernate/HibernateMappingDTD3.0//EN=org.zshwd.hibernate.voclassname=Grouptable=t_groupidname=idcolumn=gidgeneratorclass=native//idpropertyname=namelength=20//class/hibernate-mapping配置User.hbm.xml!DOCTYPEhibernate-mappingPUBLIC-//Hibernate/HibernateMappingDTD3.0//EN=org.zshwd.hibernate.voclassname=Usertable=t_useridname=idcolumn=uidgeneratorclass=native//idpropertyname=namelength=20/propertyname=password/many-to-onename=groupforeign-key=fk_user_groupid_group_gidcolumnname=group_id//many-to-one/class/hibernate-mappingmany-to-onename=groupcolumn=group_idforeign-key=fk_user_group_id_group_idcascade=all/cascade:create,merge,save-update,delete,lock,refresh,evict,replicate写一个导出类org.zshwd.hibernate.utilpackageorg.zshwd.hibernate.util;importorg.hibernate.cfg.Configuration;importorg.hibernate.tool.hbm2ddl.SchemaExport;publicclassExportDB{publicstaticvoidmain(String[]args){Configurationcfg=newConfiguration().configure();SchemaExportexport=newSchemaExport(cfg);export.create(true,true);System.out.println(数据库导出成功!);}}最后写一个测试类TestClientpackageorg.zshwd.hibernate.test;importjunit.framework.TestCase;importorg.hibernate.Session;importorg.hibernate.SessionFactory;importorg.hibernate.Transaction;importorg.zshwd.hibernate.util.HibernateUtil;importorg.zshwd.hibernate.vo.Group;importorg.zshwd.hibernate.vo.User;publicclassTestClientextendsTestCase{publicvoidtestSave1(){SessionFactoryuitl=HibernateUtil.getSessionFactory();Sessionsession=null;Transactiontx=null;try{session=uitl.openSession();tx=session.beginTransaction();Groupg1=newGroup();g1.setName(贵宾);session.save(g1);Useru1=newUser();u1.setName(Well);u1.setPassword(123);u1.setGroup(g1);Useru2=newUser();u2.setName(Direct);u2.setPassword(123456);u2.setGroup(g1);session.save(u1);session.save(u2);tx.commit();}catch(Exceptione){//TODO:handleexceptione.printStackTrace();if(tx!=null){tx.rollback();}}finally{session.close();}}publicvoidtestSave2(){SessionFactoryuitl=HibernateUtil.getSessionFactory();Sessionsession=null;Transactiontx=null;try{session=uitl.openSession();tx=session.beginTransaction();Groupg1=newGroup();g1.setName(贵

1 / 11
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功