JPA配置詳解之jpaProperties用法_第1頁
JPA配置詳解之jpaProperties用法_第2頁
JPA配置詳解之jpaProperties用法_第3頁
JPA配置詳解之jpaProperties用法_第4頁
JPA配置詳解之jpaProperties用法_第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

第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配置關(guān)聯(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í)體類對應(yīng)數(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關(guān)聯(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事務(wù)配置--

beanid="transactionManager"

propertyname="entityManagerFactory"ref="entityManagerFactory"/

/bean

!--使用annotation定義事務(wù)--

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

這是一個有用的設(shè)置

其實(shí)這個hibernate.hbm2ddl.auto參數(shù)的作用主要用于:自動創(chuàng)建|更新|驗(yàn)證數(shù)據(jù)庫表結(jié)構(gòu)。如果不是此方面的需求建議setvalue="none"。

create:每次加載hibernate時都會刪除上一次的生成的表,然后根據(jù)你的model類再重新來生成新表,哪怕兩次沒有任何改變也要這樣執(zhí)行,這就是導(dǎo)致數(shù)據(jù)庫表數(shù)據(jù)丟失的一個重要原因。

create-drop:每次加載hibernate時根據(jù)model類生成表,但是sessionFactory一關(guān)閉,表就自動刪除。

update:最常用的屬性,第一次加載hibernate時根據(jù)model類會自動建立起表的結(jié)構(gòu)(前提是先建立好數(shù)據(jù)庫),以后加載hibernate時根據(jù)model類自動更新表結(jié)構(gòu),即使表結(jié)構(gòu)改變了但表中的行仍然存在不會刪除以前的行。要注意的是當(dāng)部署到服務(wù)器后,表結(jié)構(gòu)是不會被馬上建立起來的,是要等應(yīng)用第一次運(yùn)行起來后才會。

validate:每次加載hibernate時,驗(yàn)證創(chuàng)建數(shù)據(jù)庫表結(jié)構(gòu),只會和數(shù)據(jù)庫中的表進(jìn)行比較,不會創(chuàng)建新表,但是會插入新值。

SpingDataJpa配置問題

perties.hibernate.hbm2ddl.auto=update

在配置springdatajpa時,如果perties.hibernate.hbm2ddl.auto設(shè)置為update,會自動更新數(shù)據(jù)表結(jié)構(gòu),比如Entity中增加成員變量,數(shù)據(jù)表中也會增加相應(yīng)的字段,但是需要注意的是,如果刪除一個成員變量,這時數(shù)據(jù)表中不會自動刪除對應(yīng)的字段,如果刪除的那個成員變量在數(shù)據(jù)表中被設(shè)置為notnull,當(dāng)再次運(yùn)行時就會報錯,如下面的例子

新建一個實(shí)體類

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)系上傳者。文件的所有權(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論