版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
第JPA配置詳解之jpaProperties用法目錄JPA配置之jpaPropertiesSpingDataJpa配置問題perties.hibernate.hbm2ddl.auto=update
JPA配置之jpaProperties
xmlversion="1.0"encoding="UTF-8"
beansxmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:context="/schema/context"
xmlns:aop="/schema/aop"
xmlns:jpa="/schema/data/jpa"
xmlns:tx="/schema/tx"
xsi:schemaLocation="/schema/aop/schema/aop/spring-aop-4.1.xsd
/schema/beans/schema/beans/spring-beans.xsd
/schema/data/jpa/schema/data/jpa/spring-jpa-1.3.xsd
/schema/tx/schema/tx/spring-tx-3.2.xsd
/schema/context/schema/context/spring-context-4.1.xsd"
!--spring自動讀取指定位置的配置為簡到spring中--
context:property-placeholderlocation="classpath*:/perties"/
context:component-scanbase-package="com.shiroweb"
!--掃描com.shiroweb包下除去@Controller以外注解的類--
context:exclude-filtertype="annotation"expression="org.springframework.stereotype.Controller"/
context:exclude-filtertype="annotation"expression="org.springframework.web.bind.annotation.ControllerAdvice"/
/context:component-scan
!--c3p0數(shù)據(jù)源配置--
beanid="dataSource"
propertyname="driverClass"value="${jdbc.driver}"/
propertyname="jdbcUrl"value="${jdbc.url}"/
propertyname="user"value="${jdbc.username}"/
propertyname="password"value="${jdbc.password}"/
/bean
!--JpaEntityManager配置關聯(lián)hibernateJpaVendorAdapter--
beanid="entityManagerFactory"
propertyname="dataSource"ref="dataSource"/
propertyname="jpaVendorAdapter"ref="hibernateJpaVendorAdapter"/
propertyname="packagesToScan"value="com.shiroweb"/
!--propertyname="jpaProperties"
props
命名規(guī)則My_NAME-MyName
propkey="hibernate.ejb.naming_strategy"org.hibernate.cfg.ImprovedNamingStrategy/prop
實體類對應數(shù)據(jù)庫沒有表就生成一個表
propkey="hibernate.hbm2ddl.auto"update/prop
/props
/property--
!--指定JPA屬性;如Hibernate中指定是否顯示SQL的是否顯示、方言等--
propertyname="jpaProperties"
props
!--propkey="hibernate.dialect"org.hibernate.dialect.Oracle10gDialect/prop--
propkey="hibernate.ejb.naming_strategy"org.hibernate.cfg.ImprovedNamingStrategy/prop
!--propkey="vider_class"org.hibernate.cache.NoCacheProvider/prop--
propkey="hibernate.show_sql"true/prop
propkey="hibernate.format_sql"true/prop
!--propkey="hibernate.hbm2ddl.auto"validate/prop--
propkey="hibernate.hbm2ddl.auto"update/prop
/props
/property
/bean
!--配置hibernateJpaVendorAdapter關聯(lián)數(shù)據(jù)源--
beanid="hibernateJpaVendorAdapter"
propertyname="database"value="MYSQL"/
propertyname="showSql"value="true"/
/bean
!--SpringDataJpa配置--
jpa:repositoriesbase-package="com.shiroweb"transaction-manager-ref="transactionManager"entity-manager-factory-ref="entityManagerFactory"/
!--Jpa事務配置--
beanid="transactionManager"
propertyname="entityManagerFactory"ref="entityManagerFactory"/
/bean
!--使用annotation定義事務--
tx:annotation-driventransaction-manager="transactionManager"proxy-target-/
/beans
其中jpaProperties是這是jpa的一些屬性的
!--指定JPA屬性;如Hibernate中指定是否顯示SQL的是否顯示、方言等--
propertyname="jpaProperties"
props
!--propkey="hibernate.dialect"org.hibernate.dialect.Oracle10gDialect/prop--
propkey="hibernate.ejb.naming_strategy"org.hibernate.cfg.ImprovedNamingStrategy/prop
!--propkey="vider_class"org.hibernate.cache.NoCacheProvider/prop--
propkey="hibernate.show_sql"true/prop
propkey="hibernate.format_sql"true/prop
!--propkey="hibernate.hbm2ddl.auto"validate/prop--
propkey="hibernate.hbm2ddl.auto"update/prop
/props
/property
這里有個屬性為
propkey="hibernate.hbm2ddl.auto"update/prop
這是一個有用的設置
其實這個hibernate.hbm2ddl.auto參數(shù)的作用主要用于:自動創(chuàng)建|更新|驗證數(shù)據(jù)庫表結構。如果不是此方面的需求建議setvalue="none"。
create:每次加載hibernate時都會刪除上一次的生成的表,然后根據(jù)你的model類再重新來生成新表,哪怕兩次沒有任何改變也要這樣執(zhí)行,這就是導致數(shù)據(jù)庫表數(shù)據(jù)丟失的一個重要原因。
create-drop:每次加載hibernate時根據(jù)model類生成表,但是sessionFactory一關閉,表就自動刪除。
update:最常用的屬性,第一次加載hibernate時根據(jù)model類會自動建立起表的結構(前提是先建立好數(shù)據(jù)庫),以后加載hibernate時根據(jù)model類自動更新表結構,即使表結構改變了但表中的行仍然存在不會刪除以前的行。要注意的是當部署到服務器后,表結構是不會被馬上建立起來的,是要等應用第一次運行起來后才會。
validate:每次加載hibernate時,驗證創(chuàng)建數(shù)據(jù)庫表結構,只會和數(shù)據(jù)庫中的表進行比較,不會創(chuàng)建新表,但是會插入新值。
SpingDataJpa配置問題
perties.hibernate.hbm2ddl.auto=update
在配置springdatajpa時,如果perties.hibernate.hbm2ddl.auto設置為update,會自動更新數(shù)據(jù)表結構,比如Entity中增加成員變量,數(shù)據(jù)表中也會增加相應的字段,但是需要注意的是,如果刪除一個成員變量,這時數(shù)據(jù)表中不會自動刪除對應的字段,如果刪除的那個成員變量在數(shù)據(jù)表中被設置為notnull,當再次運行時就會報錯,如下面的例子
新建一個實體類
importlombok.Data;
importjavax.persistence.*;
@Entity
@Data
publicclassCar{
@Column(name="id",nullable=false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
privateLongid;
@Column(nul
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 大秦醫(yī)院面試題及答案
- C語言基礎選擇測試題含多知識點考察及答案
- 感控護士院感防控知識試題及答案
- 新疆成人考試真題及答案
- 成都三基試題題庫附答案
- 市事業(yè)單位招聘考試公共基礎知識試題題庫附答案詳解
- 輸血三基考試試題及答案
- 三級醫(yī)院護士招聘面試題含答案
- 嵌入式開發(fā)面試題及答案
- 河南專升本試題及答案
- 起重設備安全使用指導方案
- 江蘇省揚州市區(qū)2025-2026學年五年級上學期數(shù)學期末試題一(有答案)
- 干部履歷表(中共中央組織部2015年制)
- GB/T 5657-2013離心泵技術條件(Ⅲ類)
- GB/T 3518-2008鱗片石墨
- GB/T 17622-2008帶電作業(yè)用絕緣手套
- GB/T 1041-2008塑料壓縮性能的測定
- 400份食物頻率調(diào)查問卷F表
- 滑坡地質(zhì)災害治理施工
- 實驗動物從業(yè)人員上崗證考試題庫(含近年真題、典型題)
- 可口可樂-供應鏈管理
評論
0/150
提交評論