




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、hibernate反向生成數(shù)據(jù)庫表第一步:添加 Hibernate jar 包,hibernate.cfg.xml 寫好數(shù)據(jù)源.hibernate.cfg.xml:VDOCTYPE hibernate-co nfiguration PUBLIC"-/Hibernate/Hibernate Con figuratio n DTD 3.0/EN""http:/hibernate.sourceforge. net/hibernate-c on figuratio n-3.0.dtd">< hiber nate-c on figuratio n>
2、;<sessi on-factory><property n ame="hiber nate.c onn ecti on. url">jdbc:mysql:/localhost/test</property><property n ame="hiber nate.c onn ecti on. driver_class">com.mysql.jdbc.Driver</property><property n ame="hiber nate.c onn ecti on. user n
3、ame">root</property><property n ame="hiber nate.c onn ecti on .password">root</property><property n ame="hiber nate.dialect">org.hiber nate.dialect.MySQLDialect</property><property n ame="hiber nate.show_sql">true</property
4、><mapp ing resource="com/bc/bea n/HomeBusi ness.hbm.xml"/></sessi on-factory>< /hiber nate-c on figurati on>第二步:編寫實(shí)體類;package com.bc.bea n;import java.util.Date;public class Busin ess private In teger uld;private String uName;private Date hirDate;private int zStoreNum;
5、private int zBra nchNum;private int zTradNum;private int sStoreNum;private int sBra nchNum;private int sTradNum;private int bStoreNum;private int bBra nchNum;private int bTradNum;private int totalNum;private String home;public Busin ess() super();public In teger getuld() return uId;public void setuI
6、d(I nteger uId) this.uId = uId;public String getuName() return uName;public void setuName(Stri ng uName) this.uName = uName;public Date getHirDate() return hirDate;public void setHirDate(Date hirDate) this.hirDate = hirDate;public int getzStoreNum() return zStoreNum;public void setzStoreNum(i nt zSt
7、oreNum) this.zStoreNum = zStoreNum;public int getzBra nchNum() retur n zBra nchNum;public void setzBra nchNum(i nt zBra nchNum) this.zBra nchNum = zBra nchNum;public int getzTradNum() retur n zTradNum;public void setzTradNum(i nt zTradNum) this.zTradNum = zTradNum;public int getsStoreNum() return sS
8、toreNum;public void setsStoreNum(i nt sStoreNum) this.sStoreNum = sStoreNum;public int getsBra nchNum() retur n sBra nchNum;public void setsBra nchNum(i nt sBra nchNum) this.sBra nchNum = sBra nchNum;public int getsTradNum() return sTradNum;public void setsTradNum(i nt sTradNum) this.sTradNum = sTra
9、dNum;public int getbStoreNum() return bStoreNum;public void setbStoreNum(i nt bStoreNum) this.bStoreNum = bStoreNum;public int getbBra nchNum() retur n bBra nchNum;public void setbBra nchNum(i nt bBra nchNum) this.bBra nchNum = bBra nchNum;public int getbTradNum() return bTradNum;public void setbTra
10、dNum(i nt bTradNum) this.bTradNum = bTradNum;public in t getTotalNum() return totalNum;public void setTotalNum(i nt totalNum) this.totalNum = totalNum;public String getHome() return home;public void setHome(Stri ng home) this.home = home;第三步:編寫實(shí)體類映射文件HomeBus in ess.hbm.xml<?xml versio n="1.0
11、"?>< !DOCTYPE hibernate-mappi ng PUBLIC"-/Hibernate/Hibernate Mappi ng DTD 3.0/EN""http:/hiber nate.sourceforge .n et/hiber nate-mapp in g-3.0.dtd">< hiber nate-mapp ing package="com.bc.bea n"><class n ame="HomeBus in ess" table="t_
12、HomeBus in ess"><id n ame="id" type="java .lang.ln teger"><colu mn n ame="id" precisi on="6" scale="0" /><ge nerator class="n ative"></ge nerator</id><property n ame="n ame" colu mn="n am
13、e"/><property n ame="one" colu mn="on e"/><property n ame="two" colu mn="two"/><property n ame="three" colu mn="three"/<property n ame="four" colu mn="four"/<property n ame="five" c
14、olu mn="five"/<property n ame="totalNum" colu mn="totalNum"/><property n ame="home" colu mn="home"/<property n ame="team" colu mn="team"/</class>< /hibernate-mappi ng>第四步:Main執(zhí)行創(chuàng)建ExportDB.javapackage com.te
15、st.bea n.test;import org.hibernate.cfg.C on figuratio n;import org.hibernate.tool.hbm2ddl.SchemaExport;public class ExportDB public static void main(String args) 讀取 hibernate.cfg.xml 文件Con figurati on cfg = new Con figurati on().con figure()SchemaExport export = new SchemaExport(cfg); export.create(
16、true, true);按照以上步驟,即可反向生成!使用Hibernate生成或還原數(shù)據(jù)庫表利用MyEclipse 可以很容易的根據(jù)數(shù)據(jù)庫表生成Hibernate mapping file和實(shí)體類。同時反過來,利用生成的Hibernate mappi ng file和實(shí)體類也可以生成數(shù)據(jù)庫表,這樣就可以免去部署項(xiàng)目時建立數(shù)據(jù)庫表的過程,也可以利用這種方法從一種數(shù)據(jù)庫導(dǎo)入到另一種數(shù)據(jù)庫。在使用前一定要確保Hibernate在應(yīng)用中可以正常訪問數(shù)據(jù)庫。自動生成只適用于數(shù)據(jù)庫的表,數(shù)據(jù)庫沒有的話,需要自已手工建立。方法有兩種:I. hibernate.cfg.xml配置文件中添加屬性:vproper
17、tyn ame="hbm2ddl.auto">create</property>這種方式可以在啟動web服務(wù)后首次調(diào)用Hibernate時自動建立數(shù)據(jù)庫表,建立表后再把這個屬性去掉,防止以后自動重新建立表,丟失數(shù)據(jù)。2. 以代碼的形式實(shí)現(xiàn):Configuration cfg =new Configuration().configure();SchemaExport schemaExport=new SchemaExport(cfg);schemaExport.create(false , true );我在測試不同數(shù)據(jù)庫之間導(dǎo)入數(shù)據(jù)時岀現(xiàn)錯誤,從SQLSe
18、rver 建立的映射無法還原到MySQL數(shù)據(jù)庫,對比了一下,發(fā)現(xiàn) SQLServer 在Hibernate mapping file里多了一個schema="dbo", 把每個mapping file 里的schema="dbo" 去掉,就可以成功還原了。使用Hibernate 在多種數(shù)據(jù)庫之間切換時,也會岀現(xiàn)這種問題,解決辦法就是在mapping file里不要指定schema,如果像SQLServer 需要指定schema 的話,可以在 Hibernate 配置文 件 hibernate.cfg.xml里指定默認(rèn) schema : vpropertyn ame="default_schema">dbo</property>
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 診所引流現(xiàn)場管理制度
- 診療技術(shù)授權(quán)管理制度
- 調(diào)解中心監(jiān)督管理制度
- 財政特設(shè)專戶管理制度
- 貨代公司各類管理制度
- 貨物裝卸安全管理制度
- 貨船安全生產(chǎn)管理制度
- 2025年中國感應(yīng)式皂液器行業(yè)市場全景分析及前景機(jī)遇研判報告
- 2025年中國動作感應(yīng)手柄行業(yè)市場全景分析及前景機(jī)遇研判報告
- 液壓工具質(zhì)保協(xié)議書范本
- 2025至2030中國材料疲勞試驗(yàn)機(jī)行業(yè)項(xiàng)目調(diào)研及市場前景預(yù)測評估報告
- 2025年陜西、山西、寧夏、青海四省(陜晉寧青)高考 生物真題試卷 附答案
- 2024年西昌市教育和體育局考核聘用公立幼兒園教師真題
- 2025設(shè)備租賃合同版本范文
- 2025年浙江杭州錢塘區(qū)和達(dá)能源有限公司招聘筆試沖刺題(帶答案解析)
- 2025年衣物清潔多元化發(fā)展趨勢白皮書-天貓家清第一財經(jīng)商業(yè)數(shù)據(jù)中心
- 冷鏈物流園建設(shè)項(xiàng)目投融資與財務(wù)方案
- 保險業(yè)務(wù)員分級考試試題及答案
- 2024統(tǒng)編版七年級道德與法治下冊期末測試卷(含答案)
- 酒店經(jīng)銷商合同協(xié)議書
- 轉(zhuǎn)讓釣場合同協(xié)議書
評論
0/150
提交評論