版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
Hibernate實體關(guān)聯(lián)關(guān)系---總結(jié)收集整理:Volunteer20091116版權(quán)聲明:作品,允許,時請務必以超形式標明文章原始出處、作者信息和本。否則將 。/62575/39398 看了這些是不是后悔浪費時間學習映射關(guān)系了?呵呵,Hibernate的ORMap是HibernHibernate在實際項目開發(fā)中,hbm.xml包括數(shù)據(jù)庫都是通過Xdoclet生成的,在此不采 關(guān)聯(lián)關(guān)系映射實例速查HibernateHibernateHibernate關(guān)聯(lián)│││││││││└─多對多雙向關(guān)聯(lián)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的實質(zhì)是相同的,1-1是N-1的特例,單向1-1與N-1的映射配的配置將與無連接表N-1關(guān)聯(lián)的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:發(fā)生異常,不能保存Hibernate:insertinto_11fk(name,age,addre )values(?,?,?)Exceptioninthread"main"org.hibernate.TransientObjectException:com.lavasofHibernate一對一主鍵單向關(guān)聯(lián)(見1-1的關(guān)聯(lián)可以基于主鍵關(guān)聯(lián),但基于主鍵關(guān)聯(lián)的持久化類不能擁有自己的主鍵生成策略,one-to-oneone-to-one元素增加constrained="true"屬性,表明該類主鍵由關(guān)聯(lián)類生成。二、實體(省略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一對多外鍵單向關(guān)聯(lián)(見將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一對多連接表單向關(guān)聯(lián)(見(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多對一外鍵單向關(guān)聯(lián)(見多個人()對應一個地址(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<!--用來映射關(guān)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多對一連接表單向關(guān)聯(lián)(見多個人()對應一個地址(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<!--映射關(guān)聯(lián)<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多對多單向關(guān)聯(lián)(見二、實體(省略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":在保存 對象關(guān)聯(lián)的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" 一對一連接表雙向關(guān)聯(lián)(見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(?,?) 一對多連接表雙向關(guān)聯(lián)(見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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 冷凍海水產(chǎn)品購銷協(xié)議
- 測量不確定度
- 八年級英語上冊 Unit 9 Can you come to my party Section B(2a-2e)教案 (新版)人教新目標版
- 安徽省長豐縣2024-2025學年高中政治 第四課 第二框 認識運動 把握規(guī)律教案 新人教版必修4
- 2024年春九年級化學下冊 9 溶液 課題2 溶解度教案 (新版)新人教版
- 2024-2025學年高中數(shù)學上學期第10周 3.1.1方程的根與函數(shù)的零點教學設(shè)計
- 2023七年級英語下冊 Unit 3 How do you get to school Section A 第1課時(1a-2e)教案 (新版)人教新目標版
- 2024-2025年新教材高中生物 第6章 第3節(jié) 細胞的衰老和死亡教案 新人教版必修1
- 預制房屋采購合同范本(2篇)
- 美味冰淇淋課件
- 網(wǎng)絡安全教育課件PPT(30張)
- 政協(xié)委員提案案例3篇
- 《傳播學概論》考試復習題庫大全(含答案)
- 關(guān)愛青少年心理健康 課件(共18張ppt) 心理健康
- 放射科技能考核評分表
- isae3402如何做--dmla訪問控制
- 部編版三年級語文(上冊)標點符號專項訓練題(含答案)
- 《勞動關(guān)系協(xié)調(diào)員》教學計劃及教學大綱
- 中國歷史文化名城-南京課件
- 城市橋梁安全性評估規(guī)程DB50-T 273-2021
- 《人物的千姿百態(tài)》初中美術(shù)教學課件
評論
0/150
提交評論