Version 3

    JBoss EJB 3.0 and Extensions

     

    &27704;&32154;&12467;&12531;&12486;&12461;&12473;&12488;&12398;&35373;&23450; (Configure Persistence Context)

    &12399;&12376;&12417;&12395; (Introduction)

     

    To deploy the entity beans in an application, you can just package their class files in a JAR file. The EJB 3.0 specification also requires a persistence.xml file in the META-INF directory of the JAR file. The persistence.xml file defines which database is used with those entity beans and specifies the default behavior of the EntityManager. You can then deploy the JAR to the server's deploy directory or bundled in an EAR file.

     

    &12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12398;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12434;&12487;&12503;&12525;&12452;&12377;&12427;&12395;&12399;&12289;&12381;&12428;&12425;&12398;&12463;&12521;&12473;&12501;&12449;&12452;&12523;&12434;JAR&12501;&12449;&12452;&12523;&12395;&12497;&12483;&12465;&12540;&12472;&12377;&12427;&12384;&12369;&12391;&12391;&12365;&12414;&12377;&12290;EJB 3.0&20181;&27096;&12399;&12289;&12381;&12398;JAR&12501;&12449;&12452;&12523;&12398;META-INF&12487;&12451;&12524;&12463;&12488;&12522;&12395;persistence.xml&12418;&35201;&27714;&12375;&12390;&12356;&12414;&12377;&12290;&12381;&12398;persistence.xml&12501;&12449;&12452;&12523;&12399;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12391;&20351;&29992;&12377;&12427;&12487;&12540;&12479;&12505;&12540;&12473;&12398;&31278;&39006;&12392;EntityManager&12398;&12487;&12501;&12457;&12523;&12488;&12398;&25391;&12427;&33310;&12356;&12434;&35215;&23450;&12375;&12414;&12377;&12290;&12371;&12358;&12375;&12390;&12289;&12381;&12398;JAR&12434;&30452;&25509;&12395;&12469;&12540;&12496;&12398;&12487;&12503;&12525;&12452;&12487;&12451;&12524;&12463;&12488;&12522;&12395;&12487;&12503;&12525;&12452;&12377;&12427;&12371;&12392;&12364;&12391;&12365;&12414;&12377;&12375;&12289;&12354;&12427;&12356;&12399;EAR&12501;&12449;&12452;&12523;&12395;&12496;&12531;&12489;&12523;&12373;&12428;&12383;&12418;&12398;&12391;&12418;&21487;&33021;&12391;&12377;&12290;

     

    persistence.xml&12501;&12449;&12452;&12523; (The persistence.xml file)

     

    The persistence.xml file contains one or several persistence-unit element. Each persistence-unit defines the persistence context name, data source settings, and vendor specific properties. Below is a more typical persistence.xml file.

     

    persistence.xml&12501;&12449;&12452;&12523;&12399;&19968;&12388;&20197;&19978;&12398;persistence-unit&35201;&32032;&12434;&21547;&12415;&12414;&12377;&12290;&21508;persistence-unit&12399;&27704;&32154;&12467;&12531;&12486;&12461;&12473;&12488;&21517;&12289;&12487;&12540;&12479;&12477;&12540;&12473;&35373;&23450;&12289;&12381;&12375;&12390;&12505;&12531;&12480;&12540;&22266;&26377;&12398;&12503;&12525;&12497;&12486;&12451;&12434;&23450;&32681;&12375;&12414;&12377;&12290;&20197;&19979;&12399;&12289;&20856;&22411;&30340;&12394;persistence.xml&12501;&12449;&12452;&12523;&12391;&12377;&12290;

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

    It specifies that the ejb3trail named EntityManager (see next section) should use the java:/DefaultDS datasource (i.e., the embedded HSQL database in JBoss AS) to persist the entity beans it manages. If you need to use a different database to map the entity beans in this persistence context, you can change the JNDI name of the data source to replace the java:/DefaultDS value (you have to define the alternative datasource in a -ds.xml file, see the application server guide for more details). In addition, JBoss AS should create database tables for those entity beans when the application is deployed, and delete those tables when the application is un-deployed. Note that the application is deployed and un-deployed automatically at the server startup or shutdown.

     

    &12381;&12428;&12399;&12289;ejb3trail&12392;&21517;&12389;&12369;&12425;&12428;&12383;EntityManager&65288;&27425;&12398;&12475;&12463;&12471;&12519;&12531;&12434;&21442;&29031;&65289;&12364;&31649;&29702;&12377;&12427;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12434;&27704;&32154;&21270;&12377;&12427;&12398;&12395;java:/DefaultDS&12487;&12540;&12479;&12477;&12540;&12473;&65288;&12388;&12414;&12426;&12289;JBoss AS&20869;&12398;&32068;&12415;&36796;&12415;HSQL&12487;&12540;&12479;&12505;&12540;&12473;&65289;&12434;&20351;&12358;&12289;&12392;&25351;&23450;&12375;&12390;&12356;&12414;&12377;&12290;&12371;&12398;&27704;&32154;&12467;&12531;&12486;&12461;&12473;&12488;&12391;&35079;&25968;&12398;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12434;&12510;&12483;&12503;&12377;&12427;&12383;&12417;&12395;&30064;&12394;&12427;&12487;&12540;&12479;&12505;&12540;&12473;&12434;&20351;&12358;&24517;&35201;&12364;&12354;&12427;&12394;&12425;&12289;&12487;&12540;&12479;&12477;&12540;&12473;&12398;JNDI&21517;&12434;&22793;&26356;&12375;&12390;&12289;java:/DefaultDS&12398;&20516;&12434;&32622;&12365;&25563;&12360;&12427;&12371;&12392;&12364;&12391;&12365;&12414;&12377;&65288;&12381;&12398;&22580;&21512;&12289;-ds.xml&12501;&12449;&12452;&12523;&12391;&21029;&12398;&12487;&12540;&12479;&12477;&12540;&12473;&21517;&12434;&23450;&32681;&12377;&12427;&24517;&35201;&12364;&12354;&12426;&12414;&12377;&12290;&35443;&32048;&12399;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12469;&12540;&12496;&12460;&12452;&12489;&12434;&21442;&29031;&12398;&12371;&12392;&65289;&12290;&12373;&12425;&12395;&12289;JBoss AS&12399;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12364;&12487;&12503;&12525;&12452;&12373;&12428;&12427;&12392;&12365;&12289;&12381;&12428;&12425;&12398;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12398;&12383;&12417;&12398;&12487;&12540;&12479;&12505;&12540;&12473;&12486;&12540;&12502;&12523;&12434;&29983;&25104;&12377;&12427;&12409;&12365;&12391;&12377;&12375;&12289;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12364;&12450;&12531;&12487;&12503;&12525;&12452;&12373;&12428;&12427;&12392;&12365;&12395;&12399;&12371;&12428;&12425;&12398;&12486;&12540;&12502;&12523;&12434;&21066;&38500;&12377;&12427;&12409;&12365;&12391;&12377;&12290;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12399;&12469;&12540;&12496;&36215;&21205;&12539;&20572;&27490;&26178;&12395;&33258;&21205;&30340;&12395;&12487;&12503;&12525;&12452;&12420;&12450;&12531;&12487;&12503;&12525;&12452;&12373;&12428;&12427;&12371;&12392;&12395;&27880;&24847;&12375;&12390;&12367;&12384;&12373;&12356;&12290;

     

    The properties in the persistence.xml are application server specific. In JBoss AS, they are simply Hibernate properties since Hibernate is the underlying implementation framework for JBoss EJB 3.0. You can see a list of available properties and their default values in the Server_installation/server/deploy/ejb3.deployer/META-INF/persistence.properties file.

     

    persistence.xml&20869;&12398;&12503;&12525;&12497;&12486;&12451;&12399;&12289;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12469;&12540;&12496;&22266;&26377;&12391;&12377;&12290;JBoss AS&12391;&12399;&12381;&12428;&12425;&12399;Hibernate&12503;&12525;&12497;&12486;&12451;&12391;&12377;&12290;&12394;&12380;&12394;&12425;&12289;Hibernate&12364;JBoss EJB 3.0&12398;&12383;&12417;&12398;&19979;&20301;&12395;&23384;&22312;&12377;&12427;&23455;&35013;&12501;&12524;&12540;&12512;&12527;&12540;&12463;&12384;&12363;&12425;&12391;&12377;&12290;&12381;&12428;&12425;&12398;&21033;&29992;&21487;&33021;&12394;&12503;&12525;&12497;&12486;&12451;&12398;&12522;&12473;&12488;&12392;&12487;&12501;&12457;&12523;&12488;&12503;&12525;&12497;&12486;&12451;&12399;&12289;server_installation]/server/deploy/ejb3.deployer/META-INF/persistence.properties &12501;&12449;&12452;&12523;&12434;&12372;&35239;&12367;&12384;&12373;&12356;&12290;

     

    &29305;&23450;&12398;&12467;&12531;&12486;&12461;&12473;&12488;&12363;&12425;&12398;EntityManager&12398;&27880;&20837; (Injecting an EntityManager from a specific context)

     

    As we can see, the persistence.xml file in the JAR file can define multiple persistence contexts with different names. To obtain an EntityManager from a named persistence context, you just need to specify the name in the unitName attribute of the @PersistenceContext annotation. The following example injects the ejb3trail persistence context to the EntityManager.

     

    &20170;&12414;&12391;&35211;&12390;&12365;&12383;&12424;&12358;&12395;&12289;JAR&12501;&12449;&12452;&12523;&20869;&12398;persistence.xml&12501;&12449;&12452;&12523;&12399;&30064;&12394;&12427;&21517;&21069;&12434;&25345;&12388;&35079;&25968;&12398;&27704;&32154;&12467;&12531;&12486;&12461;&12473;&12488;&12434;&23450;&32681;&12377;&12427;&12371;&12392;&12364;&12391;&12365;&12414;&12377;&12290;&21517;&21069;&12398;&20184;&12356;&12383;&27704;&32154;&12467;&12531;&12486;&12461;&12473;&12488;&12363;&12425;EntityManager&12434;&21462;&24471;&12377;&12427;&12395;&12399;&12289;@PersistenceContext&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12398;unitName&23646;&24615;&12395;&21517;&21069;&12434;&25351;&23450;&12377;&12427;&24517;&35201;&12364;&12354;&12426;&12414;&12377;&12290;&27425;&12398;&20363;&12399;ejb3trail&27704;&32154;&12467;&12531;&12486;&12461;&12473;&12488;&12434;EntityManager&12408;&27880;&20837;&12375;&12414;&12377;&12290;

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

    You can omit the unitName attribute if the following conditions are met:

     

    &27425;&12398;&26465;&20214;&12434;&28288;&12383;&12379;&12400;&12289;unitName&23646;&24615;&12434;&30465;&30053;&12377;&12427;&12371;&12392;&12364;&21487;&33021;&12391;&12377;:

     

         &12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&20840;&20307;&12391;persistence.xml&12501;&12449;&12452;&12523;&12434;&19968;&12388;&12384;&12369;&25345;&12388;&12290;(You only have one persistence context defined in the persistence.xml file in the entire application.)

         &12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12391;&12473;&12467;&12540;&12503;&20184;&12365;&12463;&12521;&12473;&12525;&12540;&12480;&12434;&20351;&29992;&20013;&12391;&12354;&12427;&12290;(You are using a scoped classloader for the application.)

     

    The second item probably needs more explanation. In a web application deployed in an EAR file, that means you need a jboss-app.xml file in the EAR file's META-INF directory. The file can specify an arbitrary name for the classloader. Here is an example:

     

    &20108;&30058;&30446;&12398;&38917;&30446;&12399;&12362;&12381;&12425;&12367;&12418;&12387;&12392;&35500;&26126;&12364;&24517;&35201;&12391;&12375;&12423;&12358;&12290;EAR&12501;&12449;&12452;&12523;&12395;&12487;&12503;&12525;&12452;&12373;&12428;&12383;Web&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12391;&12399;&12289;&12381;&12428;&12399;EAR&12501;&12449;&12452;&12523;&12398;META-INF&12487;&12451;&12524;&12463;&12488;&12522;&12391;jboss-app.xml&12501;&12449;&12452;&12523;&12364;&24517;&35201;&12391;&12354;&12427;&12392;&12356;&12358;&12371;&12392;&12434;&24847;&21619;&12375;&12414;&12377;&12290;&12381;&12398;&12501;&12449;&12452;&12523;&12399;&12463;&12521;&12473;&12525;&12540;&12480;&29992;&12398;&20219;&24847;&12398;&21517;&21069;&12434;&25351;&23450;&12377;&12427;&12371;&12392;&12364;&21487;&33021;&12391;&12377;&12290;&12371;&12428;&12399;&12381;&12398;&20363;&12391;&12377;&65306;

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

    If you do not have a scoped classloader for the application, all the persistence contexts defined in the applications would be globally available in the entire server, and the @PersistenceContext injection look for an EntityManager in the global context as well. That allows you to use persistence context defined outside of your own application. But you also have to explicitly specify the persistence context name via the unitName attribute.

     

    &12418;&12375;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12364;&12473;&12467;&12540;&12503;&20184;&12365;&12463;&12521;&12473;&12525;&12540;&12480;&12434;&25345;&12383;&12394;&12369;&12428;&12400;&12289;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12391;&23450;&32681;&12373;&12428;&12383;&12377;&12409;&12390;&12398;&27704;&32154;&12467;&12531;&12486;&12461;&12473;&12488;&12399;&12469;&12540;&12496;&20840;&20307;&12391;&21033;&29992;&21487;&33021;&12391;&12289;@PersistenceContext&27880;&20837;&12399;&21516;&27096;&12395;&12464;&12525;&12540;&12496;&12523;&12467;&12531;&12486;&12461;&12473;&12488;&12363;&12425;EntityManager&12434;&25506;&12375;&12414;&12377;&12290;&12381;&12428;&12399;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&33258;&36523;&12398;&22806;&37096;&12391;&23450;&32681;&12373;&12428;&12383;&27704;&32154;&12467;&12531;&12486;&12461;&12473;&12488;&12398;&20351;&29992;&12434;&21487;&33021;&12395;&12375;&12414;&12377;&12290;&12375;&12363;&12375;&12289;&19968;&26041;&12391;&12289;unitName&23646;&24615;&12434;&20171;&12375;&12390;&27704;&32154;&12467;&12531;&12486;&12461;&12473;&12488;&21517;&12434;&26126;&31034;&30340;&12395;&25351;&23450;&12375;&12394;&12369;&12428;&12400;&12394;&12426;&12414;&12379;&12435;&12290;

     

    &12414;&12392;&12417; (Summary)

     

    In this trail, we discussed how to configure multiple persistence contexts for an application.

     

    &12371;&12398;&23567;&36947;&12391;&12399;&12289;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12398;&12383;&12417;&12395;&35079;&25968;&12398;&27704;&32154;&12467;&12531;&12486;&12461;&12473;&12488;&12434;&35373;&23450;&12377;&12427;&26041;&27861;&12395;&12388;&12356;&12390;&35696;&35542;&12375;&12414;&12375;&12383;&12290;

     

     

    � 2005 JBoss, Inc. All Rights Reserved