Let's go inside

          this blog is deprecated as a result of laziness.
          posts - 59, comments - 2, trackbacks - 0, articles - 0

          TrailBlazer第12天--Configure Persistence Context

          Posted on 2006-07-28 04:03 Earth 閱讀(535) 評論(0)  編輯  收藏 所屬分類: JavaEE5/EJB3

          這是TrailBlazer Hike 3最后一節,? 全文翻譯

          要在應用中發布entity beans,你僅需要將這些類文件打包成JAR. EJB 3.0 規范要求你在JAR包的META-INF目錄下提供一個persistence.xml。 在persistence.xml定義你將使用哪個數據庫,定義EntityManager的默認行為。你既可以把JAR發布到server的deploy目錄下,也可以把JAR放在EAR包中。
          ?
          persistence.xml文件
          ???
          persistence.xml包含一個或多個persistence-unit元素. 每一個persistence-unit定義了一個persistence context名字, data source設置, 和vendor specific properties. 以下是一個典型的persistence.xml文件.

          < persistence >
          ??
          < persistence-unit? name ="ejb3trail" >
          ????
          < jta-data-source > java:/DefaultDS </ jta-data-source >
          ????
          < properties >
          ??????
          < property? name ="hibernate.hbm2ddl.auto"
          ????????????????value
          ="create-drop" />
          ????
          </ properties >
          ??
          </ persistence-unit >
          </ persistence >

          它定義了以 ejb3trail命名的EntityManager 是用 java:/DefaultDS 作為數據源來持久化它所管理的entity beans的. 如果你想使用一個不同的數據庫來映射當前persistence context中的entity beans,你可以改變數據源的JNDI名字以替換java:/DefaultDS(你需要提供相應的*-ds.xml文件). 另外,還指定了JBoss服務器應該在應用deploy之時給這些entity beans創建相應的表, 在應用un-deploy之時刪除這些表。注意應用的deploy和un-deploy是在server startup或shutdown時進行的。

          persistence.xml中的properties是application server specific的. 在JBoss AS中, 它們是簡單的Hibernate properties因為Hibernate是JBoss EJB 3.0的內在實現框架.你可以在[server_installation]/server/deploy/ejb3.deployer/META-INF/persistence.properties 中找到一系列可用的properties 和它們的默認值.
          ?
          從一個特定的context中注入EntityManager
          ???? 正如我們所看到的,JAR包中的persistence.xml可以用不同的名字定義多個persistence contexts.為了從一個有名字的persistence context中得到EntityManager,? 你只需要指定@PersistenceContext標注的unitName屬性. 下面的例子就是把ejb3trail persistence context注入到EntityManager.

          @PersistenceContext(unitName = " ejb3trail " )
          EntityManager?em;

          如果滿足下面的條件,你可以省去unitName:

          1,僅在persistence.xml文件中定義了一個persistence context.
          2,在應用中使用了scoped classloader.
          ?
          第2條可能需要更多的解釋. 如果在EAR包中包含WEB應用, 意味著你需要在EAR包中的META-INF目錄下提供一個 jboss-app.xml文件. 在這個文件中你可以給classloader指定任意一個名字,這里是一個例子:

          < jboss-app >
          ??
          < loader-repository >
          ????trailblazer:app=ejb3
          ??
          </ loader-repository >
          </ jboss-app >

          如果你在應用中未定義scoped classloader, 所有該應用中定義的persistence contexts將在整個服務器中全局可用, 相應地@PersistenceContext injection將在global context查找EntityManager. 這樣你可以使用在該應用之外定義的persistence context. 但是此時你不得不通過unitName顯示地指定persistence context的名字。
          ?
          小結?
          ??? 在這一節中,我們討論了怎樣給應用配置多個persistence contexts.接下來我們將探討一章全新的內容。

          主站蜘蛛池模板: 喀喇| 荣昌县| 廊坊市| 巍山| 邹平县| 成都市| 九寨沟县| 德州市| 濮阳县| 扬中市| 邯郸县| 田林县| 密山市| 宕昌县| 株洲县| 黄大仙区| 靖西县| 墨竹工卡县| 密山市| 昆明市| 怀柔区| 萍乡市| 青神县| 乌海市| 新建县| 文山县| 临潭县| 黄大仙区| 泗阳县| 泰和县| 金华市| 黑山县| 梨树县| 临江市| 垣曲县| 盐边县| 铅山县| 松原市| 称多县| 隆安县| 天长市|