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 閱讀(537) 評論(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.接下來我們將探討一章全新的內容。

          主站蜘蛛池模板: 多伦县| 商水县| 鄢陵县| 南丹县| 铜鼓县| 静宁县| 卓资县| 永年县| 彰化县| 星子县| 龙游县| 涟源市| 武汉市| 泸定县| 佛山市| 南京市| 闸北区| 南郑县| 蓝山县| 如东县| 阿图什市| 比如县| 祁东县| 邳州市| 那曲县| 宁波市| 米林县| 广平县| 张家界市| 蒲城县| 兴和县| 辽阳县| 封开县| 天峨县| 威信县| 威远县| 和静县| 阆中市| 定安县| 沈丘县| 石狮市|