您好,欢迎访问三七文档
Hibernate关联关系映射实例速查Hibernate的映射关系很多,也比较复杂,也很容易忘记。这个基本上占据了Hibernate学习的七成时间。熟悉这些映射模型,需要大量的实践才能掌握。下面是我对Hibernate关联关系映射的一个总结,放到blog上一是自己查看方便,二来也可以和更多Hibernate开发人员交流分享。希望各位多多留言哦:)。本文主要参考夏昕翻译的“Hibernate参考文档V3.12”,也在附件中给出了。本文的模块较多,映射关系部分分为一下模块:Hibernate关联关系映射目录│├─单向关联│├─一对一外键单向关联│├─一对一主键单向关联│├─一对一连接表单向关联│├─一对多外键单向关联│├─一对多连接表单向关联│├─多对一外键单向关联│├─多对一连接表单向关联│└─多对多单向关联└─双向关联├─一对一外键双向关联├─一对一主键双向关联├─一对一连接表双向关联├─一对多外键双向关联├─一对多连接表双向关联└─多对多双向关联本系列实例的开发环境:WindowsXPProfessional简体中文版MySQL5.0.45Hibernate3.12JavaSDK1.5.06IntelliJIDEA5.12系列实例中所用的Hibernate配置文件如下:?xmlversion='1.0'encoding='gb2312'?!DOCTYPEhibernate-configurationPUBLIC-//Hibernate/HibernateConfigurationDTD3.0//EN[url][/url]hibernate-configurationsession-factory!--指定连接数据库驱动--propertyname=connection.driver_classcom.mysql.jdbc.Driver/property!--指定连接数据库的url,hibernate连接的数据库名--propertyname=connection.urljdbc:mysql://localhost:3306/hbstudy/property!--指定连接数据库的用户名--propertyname=connection.usernameroot/property!--指定连接数据库的用户密码--propertyname=connection.passwordleizhimin/property!--指定连接池的大小--propertyname=connection.pool_size5/property!--指定数据库的方言--propertyname=dialectorg.hibernate.dialect.MySQLDialect/property!--根据需要自动创建数据库,测试环境用--propertyname=hbm2ddl.autocreate/property!--在控制台显示执行的SQL语句--propertyname=show_sqltrue/property!--EnableHibernate'sautomaticsessioncontextmanagement--propertyname=current_session_context_classthread/property!--映射文件列表--!--单向关联--mappingresource=com/lavasoft/dx/_n_1_fk/Addressn1fk.hbm.xml/mappingresource=com/lavasoft/dx/_n_1_fk/Personn1fk.hbm.xml/mappingresource=com/lavasoft/dx/_n_1_tab/Addressn1tab.hbm.xml/mappingresource=com/lavasoft/dx/_n_1_tab/Personn1tab.hbm.xml/mappingresource=com/lavasoft/dx/_1_1_fk/Address11fk.hbm.xml/mappingresource=com/lavasoft/dx/_1_1_fk/Person11fk.hbm.xml/mappingresource=com/lavasoft/dx/_1_1_tab/Address11tab.hbm.xml/mappingresource=com/lavasoft/dx/_1_1_tab/Person11tab.hbm.xml/mappingresource=com/lavasoft/dx/_1_1_pk/Address11pk.hbm.xml/mappingresource=com/lavasoft/dx/_1_1_pk/Person11pk.hbm.xml/mappingresource=com/lavasoft/dx/_1_n_fk/Address1nfk.hbm.xml/mappingresource=com/lavasoft/dx/_1_n_fk/Person1nfk.hbm.xml/mappingresource=com/lavasoft/dx/_1_n_tab/Address1ntab.hbm.xml/mappingresource=com/lavasoft/dx/_1_n_tab/Person1ntab.hbm.xml/mappingresource=com/lavasoft/dx/_n_n/Addressnn.hbm.xml/mappingresource=com/lavasoft/dx/_n_n/Personnn.hbm.xml/!--双向关联--mappingresource=com/lavasoft/sx/_1_n_fk/Address1nfk_sx.hbm.xml/mappingresource=com/lavasoft/sx/_1_n_fk/Person1nfk_sx.hbm.xml/mappingresource=com/lavasoft/sx/_1_n_tab/Address1ntab_sx.hbm.xml/mappingresource=com/lavasoft/sx/_1_n_tab/Person1ntab_sx.hbm.xml/mappingresource=com/lavasoft/sx/_n_n/Addressnn_sx.hbm.xml/mappingresource=com/lavasoft/sx/_n_n/Personnn_sx.hbm.xml/mappingresource=com/lavasoft/sx/_1_1_fk/Address11fk_sx.hbm.xml/mappingresource=com/lavasoft/sx/_1_1_fk/Person11fk_sx.hbm.xml/mappingresource=com/lavasoft/sx/_1_1_pk/Address11pk_sx.hbm.xml/mappingresource=com/lavasoft/sx/_1_1_pk/Person11pk_sx.hbm.xml/mappingresource=com/lavasoft/sx/_1_1_tab/Address11tab_sx.hbm.xml/mappingresource=com/lavasoft/sx/_1_1_tab/Person11tab_sx.hbm.xml//session-factory/hibernate-configuration系列实例中所用到Session工厂是:publicclassHibernateUtil{privatestaticfinalSessionFactorysessionFactory;static{try{//CreatetheSessionFactoryfromhibernate.cfg.xmlsessionFactory=newConfiguration().configure().buildSessionFactory();}catch(Throwableex){//Makesureyoulogtheexception,asitmightbeswallowedSystem.err.println(初始化SessionFactory失败!+ex);thrownewExceptionInInitializerError(ex);}}publicstaticfinalThreadLocalsession=newThreadLocal();publicstaticSessiongetCurrentSession()throwsHibernateException{Sessions=(Session)session.get();//当原Session为空或已关闭时,打开一个新的Sessionif(s==null||!s.isOpen()){s=sessionFactory.openSession();session.set(s);}returns;}publicstaticvoidcloseSession()throwsHibernateException{Sessions=(Session)session.get();session.set(null);if(s!=null){s.close();}}}Hibernate一对一外键单向关联________________________________________2007-08-2213:17:58标签:Hibernate一对一[推送到技术圈]版权声明:原创作品,如需转载,请与作者联系。否则将追究法律责任。st1\:*{behavior:url(#ieooui)}Hibernate一对一外键单向关联事实上,单向1-1与N-1的实质是相同的,1-1是N-1的特例,单向1-1与N-1的映射配置也非常相似。只需要将原来的many-to-one元素增加unique=true属性,用于表示N的一端也必须是唯一的,在N的一端增加了唯一的约束,即成为单向1-1。基于外键的单向1-1的配置将与无连接表N-1关联的many-to-one增加unique=true属性即可。一、模型介绍一个人(Person)对应一个地址(Address)。二、实体(省略getter、setter方法)publicclassPerson11fk{privateintpersonid;privateStringname;privateintage;privateAddress11fkaddress11fk;publicclassAddress11fk{privateintaddressid;privateStringaddressdetail;三、表模型mysqldescaddress_11fk;+---------------+--------------+------+-----+---------+----------------+|Field|Type|Null|Key|Default|Extra|+---------------+--------------+------+-----+---------+----------------+|addressid|int(11)|NO|PRI|NULL|auto_increment||addressdetail|varchar(255)|YES||NULL||+-------------
本文标题:java关联映射
链接地址:https://www.777doc.com/doc-5536400 .html