下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Hibernate实体关联关系---总结收集整理:Volunteer20091116版权声明:作品,允许,时请务必以超形式标明文章原始出处、作者信息和本。否则将 。/62575/39398 看了这些是不是后悔浪费时间学习映射关系了?呵呵,Hibernate的ORMap是HibernHibernate在实际项目开发中,hbm.xml包括数据库都是通过Xdoclet生成的,在此不采 关联关系映射实例速查HibernateHibernateHibernate关联│││││││││└─多对多双向关联WindowsXPProfessionalMySQLHibernateJavaSDK liJIDEA<?xml<?xmlversion='1.0'<!DOCTYPEhibernate-configuration"-//Hibernate/HibernateConfigurationDTD3.0//EN"<property<propertyname="connection.url">jdbc:mysql://localhost:3306/hbstudy</propert<property <property<property<property<property<property<!--EnableHibernate'sautomaticsessioncontextmanagement--<property publicpublicclassHibernateUtilprivatestaticfinalSessionFactorysessionFactory;static{try//CreatetheSessionFactoryfromsessionFactory=new}catch(Throwableex)Makesureyoulogtheexception,asitmightbeswallowedSystem.err.println("初始化SessionFactory失败!"+ex);thrownewExceptionInInitializerError(ex);}}publicstaticfinalThreadLocalsession=newpublicstaticSessiongetCurrentSession()throws{Sessions=(Session)if(s==null||!s.isOpen()){s=sessionFactory.openSession();}return}publicstaticvoidcloseSession()throws{Sessions=(Session)session.get();if(s!=null)}}} 事实上,单向1-1与N-1的实质是相同的,1-1是N-1的特例,单向1-1与N-1的映射配的配置将与无连接表N-1关联的many-to-one增加unique="true"属性即可。 二、实体(省略getter、setter方法)publicpublic {privateint privateStringname;privateintage;privateAddress11fkpublicpublicclass{privateintaddre privateStringaddressdetail;+|+|+|+|+|+|++++|Null|Key|Default|+|++++|+|PRI||auto_increment|addressdetail|varchar(255)|YES | +|+||||++|+|++++|Null|Key|Default|+|++++|+|PRI||auto_increment|varchar(255)|||+||+|||||UNI|++|||+|||+mysql>CREATECREATETABLE`address_11fk` `int(11)NOTNULL`addressdetail`varchar(255)defaultNULL,PRIMARYKEY(`addre )ENGINE=InnoDBAUTO_INCREMENT=2DEFAULTCREATECREATETABLE_11fk` id`int(11)NOTNULL`name`varchar(255)default`age`int(11)default `int(11)defaultNULL,PRIMARYKEY(` KEY`FK68A8818F3F45AA77` CONSTRAINT`FK68A8818F3F45AA77`FOREIGNKEY `)CES`address_11fk` )ENGINE=InnoDBAUTO_INCREMENT=2DEFAULT在在<many-to-onename="address11fk" "><id "><generatorclass="identity"/><property ><class11fk"<id id"><generatorclass="identity"/><property<property<many-to-onename="address11fk" " publicpublicclassTest_11fk 11fk();Address11fkAddress11fkadd1=newAddress11fk();Sessionsession=HibernateUtil.getCurrentSession();Transactiontx=session.beginTransaction();}}:正常保存推荐这么干Hibernate:insertintoADDRESS_11fk(addressdetail)valuesHibernate:insertinto_11fk(name,age, )values(?,?,Hibernate:insertinto_11fk(name,age,addre )values(?,?,?)Hibernate:insertintoADDRESS_11fk(addressdetail)values(?)Hibernate:update_11fksetname=?,age=?,addre =?wherepersHibernate:insertintoADDRESS_11fk(addressdetail)values:发生异常,不能保存Hibernate:insertinto_11fk(name,age,addre )values(?,?,?)Exceptioninthread"main"org.hibernate.TransientObjectException:com.lavasofHibernate一对一主键单向关联(见1-1的关联可以基于主键关联,但基于主键关联的持久化类不能拥有自己的主键生成策略,one-to-oneone-to-one元素增加constrained="true"属性,表明该类主键由关联类生成。二、实体(省略getter、setter方法)publicpublic {privateint privateStringname;privateintage;privateAddress11pkpublicpublicclass{privateintaddre privateStringaddressdetail;+|+|++|+|+|+|++++|Null|Key|Default|+|++++|+|PRI||auto_increment|addressdetail|varchar(255)|YES | +|+|||++|+id|+++++|Null|Key|Default|Extra+|++|PRI|varchar(255)||+|+||+||++|||++|||+mysql>/*/*Formattedon2007/08/2214:40(QP5v5.50)*/CREATETABLE`address_11pk`( `int(11)NOTNULL`addressdetail`varchar(255)defaultNULL,PRIMARYKEY(`addre ))ENGINE=InnoDBAUTO_INCREMENT=2DEFAULT/*/*Formattedon2007/08/2214:41(QP5v5.50)*/CREATETABLE` _11pk`(`presonid`int(11)NOT`name`varchar(255)default`age`int(11)defaultNULL,PRIMARYKEY(`presonid`),KEY`FK68A882C591BB393E`CONSTRAINT`FK68A882C591BB393E`FOREIGNKEY(`presonid`)REFERENCES`address_11pk` )ENGINE=InnoDBDEFAULT <id<id<generator<param<one-to-onename="address11pk" <class<id id"<generator<param<property<property <id <generator<generator<property publicpublicclassTest_11pkpublicstaticvoid 11pkp1=new Address11pkadd1=newAddress11pk();Sessionsession=HibernateUtil.getCurrentSession();Transactiontx=session.beginTransaction();}}Hibernate:insertintoADDRESS_11fk(addressdetail)valuesHibernate:insertinto_11fk(name,age,addre)values(?,?,Hibernate:insertinto_11fk(name,age,addre)values(?,?,?)Hibernate:insertintoADDRESS_11fk(addressdetail)values(?)Hibernate: _11fksetname=?,age=?,addre=?whereHibernate:insertintoADDRESS_11fk(addressdetail)valuesHibernate:insertinto_11fk(name,age,addre)values(?,?,?) 将N-1many-to-one元素增加unique="true"属性即可。Address二、实体(省略getter、setter方法)publicpublic {privateint privateStringname;privateintage;privateAddress11tabpublicpublicclass{privateintaddre privateStringaddressdetail;+|+|+|+|+|+|++++|Null|Key|Default|+|++++|+|PRI||auto_increment|addressdetail|varchar(255)|YES | +|+|++|+|+|++++++|Null|Key|Default|Extra+++|int(11)||PRI+|+|||address11tab|int(11)|YES|UNI| |+|+++|+|||++|+id|++++|Null|Key|Default|+|++++|+|PRI||auto_increment|varchar(255)||+|+||+||+||+||+mysql>/*Formatted/*Formattedon2007/08/2016:52(QP5v5.50)CREATETABLE`join_11tab` id`int(11)NOT`address11tab`int(11)defaultNULL,PRIMARYKEY(` UNIQUEKEY`address11tab`(`address11tab`),KEY`FK6B44BE20C4CC3D33`(`address11tab`),KEY`FK6B44BE209049BB1F`(` id`)REFERENCES `address_11tab` )ENGINE=InnoDBDEFAULT/*Formattedon2007/08/2016:53(QP5v5.50)*/CREATETABLE`address_11tab`( `int(11)NOTNULL`addressdetail`varchar(255)defaultNULL,PRIMARYKEY(`addre )ENGINE=InnoDBAUTO_INCREMENT=2DEFAULT/*Formattedon2007/08/2016:53(QP5v5.50)*/CREATETABLE` _11tab`( id`int(11)NOTNULL`name`varchar(255)default`age`int(11)defaultNULL,PRIMARYKEY(` )ENGINE=InnoDBAUTO_INCREMENT=2DEFAULT <join<join<key <class<id <generator<property<property <join<key <id <generator<property publicpublicclassTest_11tabpublicstaticvoidmain(String[] 11tabp1=newAddress11tabadd1=newSessionSessionsession=HibernateUtil.getCurrentSession();Transactiontx=session.beginTransaction();}}Hibernate:insertintoADDRESS_11tab(addressdetail)values(?)Hibernate:insertinto_11tab(name,age)values(?,?)Hibernate:insertintojoin_11tab(address11tab, id)values(?,?)Hibernate:insertinto_11tab(name,age)values(?,?)Hibernate:insertintojoin_11tab(address11tab, id)values(?,?)Hibernate:insertintoADDRESS_11tab(addressdetail)values(?)Hibernate:updatejoin_11tabsetaddress11tab=?where Hibernate:insertintoADDRESS_11tab(addressdetail)valuesHibernate:insertinto_11tab(name,age)values(?,?)Hibernate:insertintojoin_11tab(address11tab, id)values(?,?) Hibernate一对多外键单向关联(见将N-1many-to-one元素增加unique="true"属性即可。(Address二、实体(省略getter、setter方法)publicpublic 1nfkimplements{privateint privateStringname;privateintage;privateSetaddresses=newpublicpublicclassAddress1nfkimplements{privateintaddre privateString+|+|+|+|+|+|++++|Null|Key|Default|+|++++|+|PRI||auto_increment|addressdetail|varchar(255)|YES | | |+|MUL|++||+||++|+|||++|+id|++++|Null|Key|Default|+|++++|+|PRI||auto_increment|varchar(255)||+|+||+||+||+||+mysql>/*/*Formattedon2007/08/2110:06(QP5v5.50)*/CREATETABLE`address_1nfk`( `int(11)NOTNULL`addressdetail`varchar(255)default`addresses`int(11)defaultPRIMARYPRIMARYKEY KEY`FK9B93456DC08D1667`)ENGINE=InnoDBAUTO_INCREMENT=3DEFAULT/*/*Formattedon2007/08/2110:07(QP5v5.50)*/CREATETABLE` _1nfk`( id`int(11)NOTNULL`name`varchar(255)default`age`int(11)defaultNULL,PRIMARYKEY(` )ENGINE=InnoDBAUTO_INCREMENT=2DEFAULT <setname="addresses"><key <one-to-many <class<id <generator<property<property <setname="addresses"><key <one-to-many <id <generator<property publicpublicclassTest_1nfk Address1nfk();Address1nfkTransactiontx=session.beginTransaction();}}Hibernate:insertinto_1nfk(name,age)values(?,?)Hibernate:insertintoADDRESS_1nfk(addressdetail)values(?)Hibernate:insertintoADDRESS_1nfk(addressdetail)values(?)Hibernate:updateADDRESS_1nfkset id=?whereaddre=?Hibernate:updateADDRESS_1nfk id=?whereaddreHibernate:insertintoADDRESS_1nfk(addressdetail)values(?)Hibernate:insertintoADDRESS_1nfk(addressdetail)values(?)Hibernate:insertinto_1nfk(name,age)values(?,?)Hibernate:updateADDRESS_1nfkset id=?whereaddre=?Hibernate:updateADDRESS_1nfk id=?whereaddreHibernate:insertintoADDRESS_1nfk(addressdetail)values(?)Hibernate:insertintoADDRESS_1nfk(addressdetail)valuesHibernate一对多连接表单向关联(见(Address二、实体(省略getter、setter方法)publicpublic {privateint privateStringname;privateintage;privateSetaddresses=newpublicpublicclassAddress1nfkimplements{privateint privateString+|+|+|+|+++++|Null|Key|Default|Extra++||+++++|int(11)||int(11)||PRI|PRI+++++||++||+++|+|||++|+id|++++|Null|Key|Default|+|++++|+|PRI||auto_increment|varchar(255)||+|+||+||+||+||+mysql>+|+|+|+|+|+|++++|Null|Key|Default|+|++++|+|PRI||auto_increment|addressdetail|varchar(255)|YES | /*Formatted/*Formattedon2007/08/2110:58(QP5v5.50)*/CREATETABLE`address_1ntab`( `int(11)NOTNULL`addressdetail`varchar(255)defaultNULL,PRIMARYKEY(`addre )ENGINE=InnoDBAUTO_INCREMENT=3DEFAULT/*Formattedon2007/08/2110:58(QP5v5.50)CREATETABLE`join_1ntab` id`int(11)NOT `int(11)NOTNULL,PRIMARYKEY UNIQUEKEY `KEY`FK6B6078C3C8DF5BFF`KEY`FK6B6078C3C2B11347` )ENGINE=InnoDBDEFAULT/*Formattedon2007/08/2110:58(QP5v5.50)CREATECREATETABLE_1ntab` id`int(11)NOTNULL`name`varchar(255)default`age`int(11)defaultNULL,PRIMARYKEY(` )ENGINE=InnoDBAUTO_INCREMENT=2DEFAULT <class<id <generator<property<property<setname="addresses"><key<!-- <id <generator<property publicpublicclassTest_1ntabpublicstaticvoidmain(String[]Address1ntabAddress1ntabadd1=newAddress1ntab();Address1ntabadd2=newAddress1ntab();Address1ntabadd3=new1ntabp1=new1ntabp2=newadd3.setAddressdetail("安路");Transactiontx=session.beginTransaction();}}1)Hibernate:insertinto_1nfk(name,age)values(?,?)Hibernate:insertintoADDRESS_1nfk(addressdetail)values(?)Hibernate:insertintoADDRESS_1nfk(addressdetail)values(?)Hibernate:updateADDRESS_1nfksetid=?whereaddre=?Hibernate:updateADDRESS_1nfksetid=?whereaddreHibernate多对一外键单向关联(见多个人()对应一个地址(Addresspublicpublic {privateint privateStringname;privateintage;privateAddressn1fkpublicclass{privateintaddre privateStringaddressdetail;||||||Null|Key|Default|||| |PRI||auto_increment|addressdetail|varchar(255)|YES | |||Null|Key|Default|||||+|||PRI||||MUL||auto_increment|varchar(255)|||+||mysql>CREATECREATETABLE`address_n1kf` `int(11)NOTNULL`addressdetail`varchar(255)defaultNULL,PRIMARYKEY(`addre )ENGINE=InnoDBDEFAULTDROPTABLEIFEXISTSCREATECREATETABLE_n1kf` id`int(11)NOTNULL`name`varchar(255)default`age`int(11)default `int(11)defaultNULL,PRIMARYKEY(` KEY`FK4571AF54A2A3EE48` `address_n1kf` )ENGINE=InnoDBDEFAULT<hibernate-<hibernate-><class<id <generator<property<property<!--用来映射关POcolumn是Address在该表中的外键<many-to-onename="addressn1fk" </hibernate- <hibernate-><id <generator<property</hibernate- publicpublicclassTest_n1fkpublicstaticvoid n1fkp1=new n1fk Addressn1fkadd=newAddressn1fk();Addressn1fkadd=newAddressn1fk();Transactiontx=session.beginTransaction();}}Hibernate:insertintoADDRESS_n1kf(addressdetail)valuesHibernate:insertinto_n1kf(name,age,addre)values(?,?,?)Hibernate:insertinto_n1kf(name,age,addre)values(?,?,:正常保Hibernate:insertinto_n1kf(name,age,addre)values(?,?,?)Hibernate:insertinto_n1kf(name,age,addre)values(?,?,?)Hibernate:insertintoADDRESS_n1kf(addressdetail)values(?)Hibernate:update_n1kfsetname=?,age=?,addre=?whereid=?Hibernate:update_n1kfsetname=?,age=?,addre=?whereid=?:Hibernate:insertintoADDRESS_n1kf(addressdetail)valuesHibernate:insertinto_n1kf(name,age,addre)values(?,?,?)Hibernate:insertinto_n1kf(name,age,addre)values(?,?,?) Hibernate多对一连接表单向关联(见多个人()对应一个地址(Addresspublicpublic {privateint privateStringname;privateintage;privateAddressn1tabpublicclass{privateintaddre privateStringaddressdetail;|||||Null|Key|Default|||| |PRI||auto_increment|addressdetail|varchar(255)|YES | mysql>desc||||Null|Key|Default|Extra| |int(11)| |PRI|addressn1tab|int(11)|YES|MUL| mysql>mysql> ||||Null|Key|Default||||id|||varchar(255)||||PRI||||auto_incrementCREATETABLECREATETABLE`address_n1tab` `int(11)NOTNULL`addressdetail`varchar(255)defaultNULL,PRIMARYKEY(`addre )ENGINE=InnoDBAUTO_INCREMENT=2DEFAULTCREATETABLE`join_n1tab` id`int(11)NOT`address11fk`int(11)defaultNULL,PRIMARYKEY(` KEY`FKAC780AAADAE3A82C` CONSTRAINT`FKAC780AAAC6242A64`FOREIGNKEY(`address11fk`)`address_n1tab` _n1tab` )ENGINE=InnoDBDEFAULTCREATETABLE_n1tab` id`int(11)NOTNULL`name`varchar(255)default`age`int(11)defaultNULL,PRIMARYKEY(` )ENGINE=InnoDBAUTO_INCREMENT=3DEFAULT<hibernate-<hibernate-><classn1tab"<id <generator<property<property<!--使用join元素显式确 表--<join<!--映射关联<key<key<many-to-one</hibernate- <hibernate-<hibernate-><classname="com.lavasoft.dx._n_1_tab.Addressn1tab"<id <generator<property</hibernate- publicpublicclassTest_n1tabpublicstaticvoidmain(String[] n1tabp1=new n1tabp2=newAddressn1tabadd=newAddressn1tab();Transactiontx=session.beginTransaction();}Hibernate:insertintoADDRESS_n1tab(addressdetail)values(?)Hibernate:insertinto_n1tab(name,age)values(?,?)Hibernate:insertintojoin_n1tab(address11fk, id)values(?,?)Hibernate:insertinto_n1tab(name,age)values(?,?)Hibernate:insertintojoin_n1tab(address11fk, id)values(?,?):正常保Hibernate:insertinto_n1tab(name,age)values(?,?)Hibernate:insertintojoin_n1tab(address11fk, id)values(?,?)Hibernate:insertinto_n1tab(name,age)values(?,?)Hibernate:insertintojoin_n1tab(address11fk, id)values(?,?)Hibernate多对多单向关联(见二、实体(省略getter、setter方法)publicpublic {privateint privateStringname;privateintage;privateSetaddresses=newpublicpublicclass{privateintaddre privateStringaddressdetail;++|++|+++++|Null|Key|Default|+++++|+mysql>||||+id|| |PRI||auto_increment|varchar(255)||+|+||+||+||+||++|+||+|+||++|++++++|Null|Key|Default|Extra+++|int(11)||int(11)||PRI|PRI+++++||++||++|+|||++|+id|++++|Null|Key|Default|+|++++|+|PRI||auto_increment|varchar(255)||+|+||+||+||+||+mysql>/*Formatted/*Formattedon2007/08/2111:13(QP5v5.50)*/CREATETABLE`address_nn`( `int(11)NOTNULL`addressdetail`varchar(255)defaultNULL,PRIMARYKEY(`addre )ENGINE=InnoDBAUTO_INCREMENT=4DEFAULT/*Formattedon2007/08/2111:14(QP5v5.50)CREATETABLE`join_nn` id`int(11)NOT `int(11)NOTNULL,PRIMARYKEY(` KEY`FKAAB98CF5E008E752`KEY`FKAAB98CF5239F6A16`CONSTRAINT`FKAAB98CF5239F6A16`FOREIGNKEY `)REFERENCES CONSTRAINT`FKAAB98CF5E008E752`FOREIGNKEYid`)REFERENCES)ENGINE=InnoDBDEFAULT/*Formattedon2007/08/2111:14(QP5v5.50)CREATETABLE _nn```id`int(11)NOTNULL`name`varchar(255)default`age`int(11)defaultNULL,PRIMARYKEY(` )ENGINE=InnoDBAUTO_INCREMENT=3DEFAULT <classnn"<id <generator<property<property><key<!-- <id <generator<property publicpublicclassTest_nnpublicstaticvoidmain(String[]args){Addressnnadd1=newAddressnn();AddressnnAddressnn();Addressnnnnnnnnadd3.setAddressdetail("安路");Sessionsession=}}Hibernate:insertintoADDRESS_nn(addressdetail)values(?)Hibernate:insertintoADDRESS_nn(addressdetail)values(?)Hibernate:insertintoADDRESS_nn(addressdetail)values(?)Hibernate:insertinto_nn(name,age)values(?,?)Hibernate:insertinto_nn(name,age)values(?,?)Hibernate:insertintojoin_nn(id,addre)values(?,?)Hibernate:insertintojoin_nn(id,addre)values(?,?)Hibernate:insertintojoin_nn(id,addre)values(?,?)Hibernate:insertintojoin_nn(id,addre)values(?, /*DBMS MySQL /*Created 2008-12-9 droptableifexistsaddress;droptableifexists /*Table: createtableaddress bigintnotnullauto_incrementcommentID', varchar(120)notnullcomment'详细地址', bigintcomment人的ID',primarykey(id))type=altertableaddresscomment'地址/* createtable bigintnotnullauto_incrementcomment'ID', varchar(24)notnullcomment' primaryprimarykey)type=alter comment'人altertableaddressaddconstraintFK_Reference_4foreignkey (id)ondeleterestrictonupdatepublicpublic implements{privateLongprivateStringname;privateAddressaddress;publicclassAddressimplements{privateLongid; privateString<?xml<?xmlversion="1.0"encoding="utf-0//EN"PUBLIC"-//Hibernate/HibernateDTD><class "<idname="id"<columnname="id"<generatorclass="identity"<propertyname="name"<columnname="name"length="24"not- <one-to-onename="address"cascade="all" <?xmlversion="1.0"encoding="utf-0//EN"PUBLIC"-//Hibernate/HibernateDTD><classname="entity.Address"table="address"<idname="id"<<columnname="id"<generatorclass="identity"<propertyname="detail"<columnname="detail"length="120"not-<many-to-onename=" "class="entity. fetch="select"unique="true"><column <?xml<?xmlversion='1.0'encoding='UTF-<!DOCTYPEhibernate-configuration"-//Hibernate/HibernateConfigurationDTD3.0//EN"<!--GeneratedbyMyEclipseHibernate<property<propertyname="connection.url"><propertyname="dialect"><property<propertyname="connection.driver_class"><property<property.hbm.xml"importimportorg.hibernate.Transaction;importentity.Address;import importpublicclassTestpublicstaticvoidmain(String[]{ }publicstaticvoid{=Addressaddress=newAddress("XXX号 Sessionsession=HibernateSessionFactory.getSession();Transactiontx=session.beginTransaction(); } (?,Hibernate /*DBMS MySQL /*Created 2008-12-8 droptableifexistsaddress;droptableifexists /*Table: createtableaddress bigintnotnullcomment varchar(120notnullcomment'详细地址',primarykey(id))type=altertableaddresscomment'地址/*/*create(bigintnotnullauto_incrementcommentvarchar(24)notnullcommentprimarykey)type=altercomment'人altertableaddressaddconstraintFK_Reference_2foreignkey (id)ondeleterestrictonupdatepublicpublic implements{privateLongid;privateStringname;privateAddressaddress;publicclassAddressimplements{privateLongid; privateString<?<?xmlversion="1.0"encoding="utf-0//EN"PUBLIC"-//Hibernate/HibernateDTD><class "<idname="id"<columnname="id"<generatorclass="identity"<propertyname="name"<columnname="name"length="24"not- <!--cascade="all":在保存 对象关联的address对象 <one-to-onename="address"cascade="all"><?<?xmlversion="1.0"encoding="utf-<!DOCTYPEhibernate-map"PUBLIC"-//Hibernate/HibernateDTD><classname="entity.Address"table="address"<idname="id"<columnname="id"<generator<param <propertyname="detail"<columnname="detail"length="120"not- <one-to-one "constrained="true" <?<?xmlversion='1.0'encoding='UTF-<!DOCTYPEhibernate-configuration"-//Hibernate/HibernateConfigurationDTD3.0//EN"<!--GeneratedbyMyEclipseHibernate<property<propertyname="connection.url"><propertyname="dialect"><<property<propertyname="connection.driver_class"><property<property.hbm.xml" 一对一连接表双向关联(见public {privateint privateStringname;privateintage;privateAddress11tab_sxAddpublic {privateint privateStringname;privateintage;privateAddress11tab_sxpublicpublicclass{privateintaddre privateStringaddressdetail; ++|+|||++|+id|++++|Null|Key|Default|+|++++|+|PRI||auto_increment|varchar(255)||+|+||+||+||+||+mysql>+|+|+|+||++|++++++|Null|Key|Default|Extra+++|int(11)|id|int(11)||UNI|PRI+++++||++||++|+|+|+|+|+|++++|Null|Key|Default|+|++++|+|PRI||auto_increment|addressdetail|varchar(255)|YES | /*Formatted/*Formattedon2007/08/2217:35(QP5v5.50)*/CREATETABLE` _11tab_sx`( id`int(11)NOTNULL`name`varchar(255)default`age`int(11)defaultNULL,PRIMARYKEY(` )ENGINE=InnoDBDEFAULT/*Formattedon2007/08/2217:34(QP5v5.50)*/CREATETABLE`address_11tab_sx`( `int(11)NOTNULL`addressdetail`varchar(255)defaultNULL,PRIMARYKEY(`addre )ENGINE=InnoDBDEFAULT/*Formattedon2007/08/2218:35(QP5v5.50)CREATETABLE`join_11tab_sx` `int(11)NOT id`int(11)NOTNULL,PRIMARYKEY(` UNIQUEKEY `UNIQUEKEY id`KEY`FKF4AA80E44327AAB6`KEY`FKF4AA80E460C0C9F0` `address_11tab_sx` _11tab_sx` ))ENGINE=InnoDBDEFAULT <class<id <generator<property<property<keycolumn=" <many-to-one <id <generator<property<keycolumn="addre <many-to-onename=" 11tab_sx"column=" not-null="true"unique="true"/> publicpublicclassTest_11tab_sxpublicstaticvoidmain(String[]args){Address11tab_sxadd=new 11tab_sxp=Sessionsession=Transactiontx=session.beginTransaction();}}1)Hibernate:insertinto_11tab_sx(name,age)values(?,?)Hibernate:insertintoADDRESS_11tab_sx(addressdetail)values(?)Hibernate:insertintojoin_11tab_sx(addre,id)values(?,?) (ddress二、实体(省略getter、setter方法)publicpublic 1nfk_sximplements{privateint privateStringname;privateintage;privateSetaddresses=newpublicpublicclassAddress1nfk_sximplements{privateintaddre privateStringaddressdetail;++|+|||++|+id|++++|Null|Key|Default|+|++++|+|PRI||auto_increment|varchar(255)||+|+||+||+||+||+mysql>+|+|+|+|+|+|++++|Null|Key|Default|+|++++|+|PRI||auto_increment|addressdetail|varchar(255)|YES | | |+|MUL++||+||+/*/*Formattedon2007/08/2217:42(QP5v5.50)CREATETABLE`address_1nfk` `int(11)NOTNULL`addressdetail`varchar(255)default id`int(11)defaultNULL,PRIMARYKEY(`addre KEY`FK9B93456DA6D6C1F5` )ENGINE=InnoDBDEFAULT/*Formattedon2007/08/2217:42(QP5v5.50)*/CREATETABLE` _1nfk`( id`int(11)NOTNULL`name`varchar(255)default`age`int(11)defaultNULL,PRIMARYKEY(` )ENGINE=InnoDBDEFAULT <class1nfk_sx"<id<id<key id"not- <id <column id"not- publicpublicclassTest_1nfk_sxpublicstaticvoidmain(String[]args){Address1nfk_sxadd1=newAddress1nfk_sx();Address1nfk_sxadd2=new 1nfk_sxp=new Sessionsession=HibernateUtil.getCurrentSession();Transactiontx=session.beginTransaction();}}1)正常保存Hibernate:insertintoADDRESS_1nfk_sx(addressdetail,id)values(?,?)Hibernate:insertintoADDRESS_1nfk_sx(addressdetail,id)values(?,?) 一对多连接表双向关联(见public {privateint privateStringname;privateintage;privateSetaddresses=newApublic {privateint privateStringname;privateintage;privateSetaddresses=newpublicpublicclass{privateintaddre privateStringaddressdetail; ++|+|||++|+id|++++|Null|Key|Default|+|++++|+|PRI||auto_increment|varchar(255)||+|+||+||+||+||+mysql>+|+|+|+|+|+|++++|Null|Key|Default|+|++++|+|PRI||auto_increment|addressdetail|varchar(255)|YES | +|+|+|+||++|++++++|Null|Key|Default|Extra+++|int(11)|id|int(11)||PRI|PRI+++++||++||+/*Formatted/*Formattedon2007/08/2217:52(QP5v5.50)*/CREATETABLE`address_1ntab_sx`( `int(11)NOTNULL`addressdetail`varchar(255)defaultNULL,PRIMARYKEY(`addre )ENGINE=InnoDBDEFAULT/*Formattedon2007/08/2217:52(QP5v5.50)*/CREATETABLE` _1ntab_sx`( id`int(11)NOTNULL`name`varchar(255)default`age`int(11)defaultNULL,PRIMARYKEY(` )ENGINE=InnoDBDEFAULT/*Formattedon2007/08/2217:52(QP5v5.50)CREATETABLE`join_1ntab_sx` `int(11)NOT id`int(11)NOTPRIMARYKEY(` KEY`FK8F869F61F93DDD6`(` KEY`FK8F869F61FC0F682A`(`addre `address_1ntab_sx` )ENGINE=InnoDBDEFAULT <class<id <generator<prop
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年度别墅装修售后服务合同
- 运载工具发电机灯项目评价分析报告
- 使用电池的荧光棒市场发展现状调查及供需格局分析预测报告
- 运动马甲市场需求与消费特点分析
- 淋浴器市场发展预测和趋势分析
- 土方机械市场发展现状调查及供需格局分析预测报告
- 2024年度林地改耕地生态修复工程合同
- 红宝石市场发展现状调查及供需格局分析预测报告
- 2024年度版权代理合同标的的代理区域与代理期限
- 2024年度数字货币支付平台建设与运营合同
- 《孤独的小螃蟹》读书交流会
- 法理学-6法律解释、法律推理
- 99S203消防水泵接合器安装图集
- 肉鸡饲养管理课件
- 职业暴露个案登记表(全表)
- 《雁门太守行》《从军行(其五)》阅读练习及答案
- 清洁设施设备用具消毒记录表
- 化学方程式的配平-完整版PPT
- 人教版小学数学六年级下册 比例尺-全国优质课一等奖
- 三氯化铁蚀刻液
- 六三制新青岛版五年级科学上册第六单元第23课《生物的启示》课件
评论
0/150
提交评论