Version 4

    JBoss EJB 3.0 and Extensions

     

    EntityManager&12398;&22522;&26412; (EntityManager Basics)

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

     

    Since the EJB 3 entity beans are just POJOs, you can instantiate them using the new keyword. But to save and retrieve the object into and from the database table, you need an API known as the EJB 3.0 EntityManager. In this trail, we will cover how to get an EntityManager and its basic operations.

    The sample application

     

    EJB 3&12456;&12531;&12486;&12451;&12486;&12451;Bean&12399;&21336;&12394;&12427;POJO&12394;&12398;&12391;&12289;new&12461;&12540;&12527;&12540;&12489;&12434;&20351;&12387;&12390;&12381;&12428;&12425;&12434;&29983;&25104;&12377;&12427;&12371;&12392;&12364;&12391;&12365;&12414;&12377;&12290;&12375;&12363;&12375;&12487;&12540;&12479;&12505;&12540;&12473;&12398;&12486;&12540;&12502;&12523;&12408;&12458;&12502;&12472;&12455;&12463;&12488;&12434;&20445;&23384;&12375;&12383;&12426;&12289;&12486;&12540;&12502;&12523;&12363;&12425;&21462;&12426;&20986;&12375;&12383;&12426;&12377;&12427;&12395;&12399;&12289;EJB 3.0 EntityManager&12392;&12375;&12390;&30693;&12425;&12428;&12427;API&12364;&24517;&35201;&12391;&12377;&12290;&12371;&12398;&23567;&36947;&12391;&12399;&12289;EntityManager&12434;&21462;&24471;&12377;&12427;&26041;&27861;&12392;&12381;&12398;&22522;&26412;&30340;&12394;&25805;&20316;&12434;&23398;&12403;&12414;&12377;&12290;

     

    In this trail, we implement the database-driven investment calculator application using the data model described in the last trail. Please click on the following buttons to add a few funds and investors now.

     

    &12371;&12398;&23567;&36947;&12391;&12399;&12289;&21069;&22238;&12398;&23567;&36947;&12391;&35500;&26126;&12373;&12428;&12383;&12487;&12540;&12479;&12514;&12487;&12523;&12434;&20351;&12387;&12390;&12487;&12540;&12479;&12505;&12540;&12473;&39366;&21205;&12398;&25237;&36039;&35336;&31639;&27231;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12434;&23455;&35013;&12375;&12414;&12377;&12290;&27425;&12398;&12508;&12479;&12531;&12434;&20170;&12463;&12522;&12483;&12463;&12375;&12390;&12289;&12356;&12367;&12388;&12363;&12398;&25237;&36039;&20250;&31038;&12392;&25237;&36039;&23478;&12434;&36861;&21152;&12375;&12390;&12415;&12414;&12375;&12423;&12358;&12290;

     

    Click on the button below to launch the investment calculator program. The drop down lists are pre-populated with the funds and investors. Once you click on the "calculate" button, application retrieves the fund and investor details (e.g., fund growth rate, and the investor's start and end age), and uses the information to perform the calculation.

     

    &20197;&19979;&12398;&12508;&12479;&12531;&12434;&12463;&12522;&12483;&12463;&12377;&12427;&12392;&25237;&36039;&35336;&31639;&27231;&12503;&12525;&12464;&12521;&12512;&12434;&36215;&21205;&12375;&12414;&12377;&12290;&12489;&12525;&12483;&12503;&12480;&12454;&12531;&12522;&12473;&12488;&12399;&12377;&12391;&12395;&30331;&37682;&28168;&12415;&12398;&25237;&36039;&20250;&31038;&12392;&25237;&36039;&23478;&12391;&12377;&12290;"&35336;&31639;(calculate)"&12508;&12479;&12531;&12434;&12463;&12522;&12483;&12463;&12377;&12428;&12400;&12289;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12399;&25237;&36039;&20250;&31038;&12392;&25237;&36039;&23478;&12398;&35443;&32048;&24773;&22577;&65288;&20363;&12360;&12400;&12289;&25237;&36039;&25104;&38263;&29575;&12289;&25237;&36039;&23478;&12398;&38283;&22987;&24180;&40802;&12392;&32066;&20102;&24180;&40802;&65289;&12434;&21462;&12426;&20986;&12375;&12289;&12381;&12398;&24773;&22577;&12434;&20351;&12387;&12390;&35336;&31639;&12434;&23455;&34892;&12375;&12414;&12377;&12290;

     

    EntityManager&12398;&21462;&24471; (Obtain the EntityManager)

     

    As in earlier iterations of the calculator application, the CalculatorBean session bean handles all the business logic and provides an interface to the client (i.e., the servlet). So, all the entity bean manipulations are done inside the CalculatorBean class. The EntityManager object can be injected into the CalculatorBean via a @PersistenceContext annotation. That means you do not need to explicitly create or lookup the EntityManager object. Its value is automatically assigned by the EJB 3.0 container when you use it.

     

    &35336;&31639;&27231;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12398;&21021;&26399;&12398;&27573;&38542;(iteration)&12391;&12399;&12289;CalculatorBean&12475;&12483;&12471;&12519;&12531;Bean&12399;&12377;&12409;&12390;&12398;&12499;&12472;&12493;&12473;&12525;&12472;&12483;&12463;&12434;&25201;&12356;&12289;&12463;&12521;&12452;&12450;&12531;&12488;&65288;&12377;&12394;&12431;&12385;&12289;&12469;&12540;&12502;&12524;&12483;&12488;&65289;&12395;&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12434;&25552;&20379;&12375;&12414;&12377;&12290;&12381;&12371;&12391;&12289;&12377;&12409;&12390;&12398;&12456;&12531;&12486;&12451;&12486;&12451;Bean&25805;&20316;&12399;CalculatorBean&12463;&12521;&12473;&20869;&37096;&12391;&12362;&12371;&12394;&12431;&12428;&12414;&12377;&12290;EntityManager&12458;&12502;&12472;&12455;&12463;&12488;&12399;@PersistenceContext&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12434;&20351;&12387;&12390;CalculatorBean&12408;&27880;&20837;(inject)&12377;&12427;&12371;&12392;&12364;&21487;&33021;&12391;&12377;&12290;&12371;&12428;&12399;&26126;&31034;&30340;&12394;EntityManager&12458;&12502;&12472;&12455;&12463;&12488;&12398;&29983;&25104;&12420;&65288;&35379;&27880;:JNDI&12363;&12425;&65289;&12523;&12483;&12463;&12450;&12483;&12503;&12364;&24517;&35201;&12364;&12394;&12356;&12371;&12392;&12434;&24847;&21619;&12375;&12414;&12377;&12290;&12381;&12398;&20516;&12399;&12381;&12428;&12434;&20351;&12358;&12392;&12365;&12395;&12394;&12387;&12390;EJB 3.0&12467;&12531;&12486;&12490;&12395;&12424;&12387;&12390;&33258;&21205;&30340;&12395;&21106;&12426;&20184;&12369;&12425;&12428;&12427;&12398;&12391;&12377;&12290;

     @Stateless
     public class CalculatorBean implements Calculator {
     
      @PersistenceContext // (unitName="ejb3trail")
      protected EntityManager em;
      
      // ... ...
     }
    

    Note: If the application has multiple EntityManager configurations, each with a different database configuration, the unitName attribute in the @PersistenceContext annotation can be used to tell the container which EntityManager object to inject. For more on this topic, please refer to the "Configure Persistence Context" trail later in this hike.

     

    &27880;&24847;: &12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12364;&35079;&25968;&12398;EntityManager&12434;&25345;&12385;&12289;&12381;&12428;&12382;&12428;&12364;&30064;&12394;&12427;&12487;&12540;&12479;&12505;&12540;&12473;&35373;&23450;&12434;&25345;&12388;&22580;&21512;&12395;&12289;@PersistenceContext&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12398;unitName&23646;&24615;&12399;&12467;&12531;&12486;&12490;&12395;&12393;&12398;EntityManager&12458;&12502;&12472;&12455;&12463;&12488;&12364;&27880;&20837;&12373;&12428;&12427;&12398;&12363;&12434;&31034;&12375;&12414;&12377;&12290;&12371;&12398;&12488;&12500;&12483;&12463;&12395;&38306;&12377;&12427;&35443;&32048;&12399;&12289;&12495;&12452;&12461;&12531;&12464;&12398;&24460;&21322;&12395;&30331;&22580;&12377;&12427;"Persitence Context&12398;&27083;&25104; (Configure Persistence Context)"&12398;&23567;&36947;&12434;&21442;&29031;&12375;&12390;&12367;&12384;&12373;&12356;&12290;

     

    &12456;&12531;&12486;&12451;&12486;&12451;Bean&12458;&12502;&12472;&12455;&12463;&12488;&12398;&27704;&32154;&21270; (Persist the entity bean object)

     

    The EntityManager.persist() method saves any entity bean instance to the database table as a new row. The code below shows how to create an entity bean, set its attributes, and then save it to the database.

     

    EntityManager.persist()&12513;&12477;&12483;&12489;&12399;&26032;&12375;&12356;&34892;&12392;&12375;&12390;&12487;&12540;&12479;&12505;&12540;&12473;&12398;&12486;&12540;&12502;&12523;&12395;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12452;&12531;&12473;&12479;&12531;&12473;&12434;&20445;&23384;&12375;&12414;&12377;&12290;&20197;&19979;&12398;&12467;&12540;&12489;&12399;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12398;&20316;&25104;&12289;&23646;&24615;&12398;&35373;&23450;&12289;&12381;&12375;&12390;&27425;&12395;&12381;&12428;&12434;&12487;&12540;&12479;&12505;&12540;&12473;&12395;&20445;&23384;&12377;&12427;&26041;&27861;&12434;&31034;&12375;&12414;&12377;&12290;

     @Stateless
     public class EntityCalculator implements Calculator {
     
      @PersistenceContext
      protected EntityManager em;
     
      public void addFund (String name, double growthrate) {
        Fund fund = new Fund (name, growthrate);
        em.persist (fund);
      }
     
      public void addInvestor (String name, int start, int end) {
        Investor investor = new Investor (name, start, end);
        em.persist (investor);
      }
     
      // ... ...
     }
    

     

    &12456;&12531;&12486;&12451;&12486;&12451;Bean&12398;&21462;&24471; (Retrieve an entity bean)

     

    Saving the entity bean POJOs to database is only one side of the persistence operation. When the application needs to use those data, you would need to retrieve them from the database. The EntityManager.find() method retrieves an entity bean instance from the database using the entity bean class name (i.e., the database table name) and the entity ID (i.e., the database primary key). In our O/R mapping schema, the entity ID is the Id attribute of the entity bean instance. The following code segment in CalculatorBean shows how to use EntityManager.find().

     

    &12456;&12531;&12486;&12451;&12486;&12451;Bean POJO&12434;&12487;&12540;&12479;&12505;&12540;&12473;&12395;&20445;&23384;&12377;&12427;&12392;&12356;&12358;&12371;&12392;&12399;&12289;&27704;&32154;&21270;&20966;&29702;&12398;&21322;&20998;&12398;&25805;&20316;&12395;&36942;&12366;&12414;&12379;&12435;&12290;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12364;&12487;&12540;&12479;&12434;&20351;&12358;&24517;&35201;&12364;&12354;&12427;&12392;&12365;&12395;&12399;&12289;&12487;&12540;&12479;&12505;&12540;&12473;&12363;&12425;&12381;&12428;&12434;&21462;&24471;&12377;&12427;&24517;&35201;&12364;&12354;&12427;&12391;&12375;&12423;&12358;&12290;EntityManager.find()&12513;&12477;&12483;&12489;&12399;&12289;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12463;&12521;&12473;&21517;&65288;&12377;&12394;&12431;&12385;&12289;&12487;&12540;&12479;&12505;&12540;&12473;&12398;&12486;&12540;&12502;&12523;&21517;&65289;&12392;&12456;&12531;&12486;&12451;&12486;&12451;ID&65288;&12377;&12394;&12431;&12385;&12289;&12487;&12540;&12479;&12505;&12540;&12473;&12398;&20027;&12461;&12540;&65289;&12434;&20351;&12387;&12390;&12487;&12540;&12479;&12505;&12540;&12473;&12363;&12425;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12452;&12531;&12473;&12479;&12531;&12473;&12434;&21462;&24471;&12375;&12414;&12377;&12290;O/R&12510;&12483;&12500;&12531;&12464;&12398;&12473;&12461;&12540;&12510;&12391;&12399;&12289;&12456;&12531;&12486;&12451;&12486;&12451;ID&12399;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12452;&12531;&12473;&12479;&12531;&12473;&12398;Id&23646;&24615;&12391;&12377;&12290;&27425;&12398;CalculatorBean&12398;&12467;&12540;&12489;&29255;&12399;EntityManager.find()&12398;&20351;&12356;&26041;&12434;&31034;&12375;&12414;&12377;&12290;

     @Stateless
     public class EntityCalculator implements Calculator {
     
      @PersistenceContext
      protected EntityManager em;
      
      // ... ...
      
      public double calculate (int fundId, int investorId, double saving) {
     
        Investor investor = 
            em.find(Investor.class, 
                    Integer.valueOf(investorId));
        Fund fund = 
            em.find(Fund.class, 
                    Integer.valueOf(fundId));
     
        int start = investor.getStartAge();
        int end = investor.getEndAge();
        double growthrate = fund.getGrowthrate();
     
        // ... ...
     
        TimedRecord rec = 
            new TimedRecord (fund, investor, saving, result, ts);
        em.persist (rec);
     
        return result;
      }
     
     }
    

    Finding and retrieving entity beans based on the primary ID is easy. But the problem is that you do not always know the primary ID of the data row you are looking for. In this trail, we have so far ignored how we obtained a list of Fund and Investor entity beans from the database with their primary IDs and names. We will cover that in the next trail.

     

    &20027;&12461;&12540;ID&12395;&22522;&12389;&12356;&12383;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12398;&26908;&32034;&12392;&21462;&24471;&12399;&31777;&21336;&12391;&12377;&12290;&12375;&12363;&12375;&12289;&25506;&12375;&12390;&12356;&12427;&12487;&12540;&12479;&12398;&20027;&12461;&12540;ID&12434;&30693;&12387;&12390;&12356;&12427;&12392;&12399;&38480;&12425;&12394;&12356;&12398;&12364;&21839;&38988;&12391;&12377;&12290;&12371;&12398;&23567;&36947;&12391;&12399;&12289;&12375;&12400;&12425;&12367;&12398;&38291;&12399;&12289;&20027;&12461;&12540;&12420;&21517;&21069;&12434;&20351;&12387;&12390;&12487;&12540;&12479;&12505;&12540;&12473;&12363;&12425;Fund&12420;Investor&12456;&12531;&12486;&12451;&12486;&12451;Bean&12398;&12522;&12473;&12488;&12434;&21462;&24471;&12377;&12427;&26041;&27861;&12399;&28961;&35222;&12375;&12390;&12365;&12414;&12375;&12383;&12290;&12371;&12428;&12399;&27425;&12398;&23567;&36947;&12391;&25201;&12356;&12414;&12375;&12423;&12358;&12290;

     

    &23436;&20840;&12394;&12477;&12540;&12473;&12467;&12540;&12489;&21442;&29031; (The complete source code reference)

     

    Session bean that persists and retrieves Fund and Investor beans to/from the database.

     

    &12475;&12483;&12471;&12519;&12531;Bean&12399;&12487;&12540;&12479;&12505;&12540;&12473;&12363;&12425;Fund&12420;Investor Bean&12398;&27704;&32154;&21270;&12420;&21462;&24471;&12434;&12375;&12414;&12377;&12290;

     

    • Calculator.java: &12473;&12486;&12540;&12488;&12524;&12473;&12475;&12483;&12471;&12519;&12531;Bean&12452;&12531;&12479;&12501;&12455;&12540;&12473; (Stateless session bean interface)

    • EntityCalculatorBean.java: EntityManager&12398;&20351;&29992;&20363;&12434;&31034;&12377;&12475;&12483;&12471;&12519;&12531;Bean&23455;&35013; (Session bean implementation showing the use of EntityManager)

     

    You can ignore the getFunds() and getInvestors() methods in the CalculatorBean class for now. They will be discussed in a later trail covering the EJB query language.

     

    CalculatorBean&12463;&12521;&12473;&12398;getFunds()&12420;getInvestors()&12399;&20170;&12398;&12392;&12371;&12429;&27671;&12395;&12375;&12394;&12356;&12391;&12367;&12384;&12373;&12356;&12290;&12381;&12428;&12425;&12395;&12388;&12356;&12390;&12399;EJB&21839;&21512;&12379;&35328;&35486;(EJB query language)&12434;&35519;&12409;&12427;&23567;&36947;&12398;&24460;&21322;&12391;&35696;&35542;&12375;&12414;&12377;&12290;

     

     

    The JSP user interface

     

    JSP&12518;&12540;&12470;&12452;&12531;&12479;&12501;&12455;&12540;&12473;

     

    • addfund.jsp

    • addinvestor.jsp

    • calculator.jsp

     

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

     

    In this trail, you learned how to create, save, and retrieve an entity bean. A key feature of the database is that it allows you to search and retrieve the data using a query language, including to retrieve all the rows of a specific table (i.e., the Fund or Investor tables in our application). The EntityManager supports advanced database queries. We will look into that in the next trail.

     

    &12371;&12398;&23567;&36947;&12391;&12399;&12289;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12434;&20316;&25104;&12289;&20445;&23384;&12289;&21462;&24471;&12377;&12427;&26041;&27861;&12395;&12388;&12356;&12390;&23398;&12403;&12414;&12375;&12383;&12290;&12487;&12540;&12479;&12505;&12540;&12473;&12398;&12461;&12540;&12392;&12394;&12427;&27231;&33021;&12399;&21839;&21512;&12379;&35328;&35486;&12434;&20351;&12387;&12390;&12487;&12540;&12479;&12505;&12540;&12473;&12434;&26908;&32034;&12289;&21462;&24471;&21487;&33021;&12394;&12371;&12392;&12391;&12289;&12371;&12428;&12395;&12424;&12387;&12390;&29305;&23450;&12398;&12486;&12540;&12502;&12523;&65288;&12377;&12394;&12431;&12385;&12289;&31169;&12383;&12385;&12398;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12391;&12398;Fund&12420;Investor&12486;&12540;&12502;&12523;&65289;&12398;&12377;&12409;&12390;&12398;&34892;&12434;&21462;&24471;&12391;&12365;&12427;&12424;&12358;&12395;&12394;&12426;&12414;&12377;&12290;EntityManager&12399;&20808;&36914;&30340;&12394;&12487;&12540;&12479;&12505;&12540;&12473;&21839;&21512;&12379;&12434;&12469;&12509;&12540;&12488;&12375;&12414;&12377;&12290;&27425;&12398;&23567;&36947;&12391;&12371;&12428;&12395;&12388;&12356;&12390;&35519;&12409;&12390;&12356;&12365;&12414;&12375;&12423;&12358;&12290;

     

    c 2005 JBoss, Inc. All Rights Reserved