Version 4

    JBoss EJB 3.0 and Extensions

     

    EJB&12463;&12456;&12522;&12540;&12395;&12424;&12427;&12487;&12540;&12479;&12505;&12540;&12473;&26908;&32034; (Search Database via EJB Queries)

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

     

    The EJB 3.0 EntityManager interface supports an SQL-like query language, known as the EJB Query Language (EJB QL), to search the database and the return the results as entity bean instances. Compared with traditional SQL, the EJB QL is portable across all supported databases and it operates well within an object oriented environment.

     

    EJB 3.0 EntityManager&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12399;&12289;EJB&12463;&12456;&12522;&12540;&35328;&35486; (EJB QL: EJB Query Language)&12392;&12375;&12390;&30693;&12425;&12428;&12427;SQL&12521;&12452;&12463;&12394;&12463;&12456;&12522;&12540;&35328;&35486;&12434;&12469;&12509;&12540;&12488;&12375;&12414;&12377;&12290;&12381;&12428;&12399;&12487;&12540;&12479;&12505;&12540;&12473;&12398;&26908;&32034;&12392;&12381;&12398;&32080;&26524;&12434;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12452;&12531;&12473;&12479;&12531;&12473;&12392;&12375;&12390;&36820;&12377;&12371;&12392;&12434;&30446;&30340;&12392;&12375;&12414;&12377;&12290;&20253;&32113;&30340;&12394;SQL&12392;&27604;&36611;&12377;&12427;&12392;&12289;EJB QL&12399;&12469;&12509;&12540;&12488;&12373;&12428;&12427;&12377;&12409;&12390;&12398;&12487;&12540;&12479;&12505;&12540;&12473;&38291;&12391;&12509;&12540;&12479;&12502;&12523;&12391;&12289;&12363;&12388;&12458;&12502;&12472;&12455;&12463;&12488;&25351;&21521;&29872;&22659;&12391;&36969;&20999;&12395;&21205;&20316;&12375;&12414;&12377;&12290;

     

    &12486;&12540;&12502;&12523;&12363;&12425;&20840;&12487;&12540;&12479;&12434;&21462;&24471; (Retrieve all data from a table)

     

    In the previous trail, we mentioned that the drop down lists in the investment calculator application are populated with available funds and investors in the database, including their names and primary IDs. At the bottom of the calculator program, there is also a list of past calculation records from the database. We did not discuss how we get those data from the database.

     

    &20197;&21069;&12398;&23567;&36947;&12391;&12399;&12289;&25237;&36039;&35336;&31639;&27231;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12391;&12487;&12540;&12479;&12505;&12540;&12473;&20869;&12391;&21033;&29992;&21487;&33021;&12394;&25237;&36039;&20250;&31038;(fund)&12392;&25237;&36039;&23478;(investor)&12364;&12289;&21517;&21069;&12392;&20027;&12461;&12540;&12434;&21547;&12416;&24418;&12391;&12489;&12525;&12483;&12503;&12480;&12454;&12531;&12522;&12473;&12488;&12395;&34920;&31034;&12373;&12428;&12427;&12371;&12392;&12434;&36848;&12409;&12414;&12375;&12383;&12290;&12381;&12398;&35336;&31639;&27231;&12398;&26368;&19979;&37096;&12395;&12399;&12487;&12540;&12479;&12505;&12540;&12473;&12363;&12425;&36942;&21435;&12398;&35336;&31639;&35352;&37682;&12398;&12522;&12473;&12488;&12418;&12354;&12426;&12414;&12377;&12290;(&12375;&12363;&12375;)&12289;&12487;&12540;&12479;&12505;&12540;&12473;&12363;&12425;&12381;&12428;&12425;&12398;&12487;&12540;&12479;&12434;&21462;&24471;&12377;&12427;&26041;&27861;&12395;&12388;&12356;&12390;&12399;&35696;&35542;&12375;&12414;&12379;&12435;&12391;&12375;&12383;&12290;

     

    In fact, retrieving all the rows in a table is very easy to do using the EJB QL. The following code snippet shows how to retrieve all funds from the Fund table. It is very easy to understand if you know SQL. The EntityManager.createQuery() creates a query with an EJB QL statement. The listResults() method returns a collection of entity beans that are matched by the query.

     

    &23455;&38555;&12398;&12392;&12371;&12429;&12289;EJB QL&12434;&20351;&12387;&12390;&12289;&12354;&12427;&12486;&12540;&12502;&12523;&12363;&12425;&12377;&12409;&12390;&12398;&34892;&12434;&21462;&24471;&12377;&12427;&12371;&12392;&12399;&12392;&12390;&12418;&31777;&21336;&12391;&12377;&12290;&27425;&12398;&12467;&12540;&12489;&29255;&12399;Fund&12486;&12540;&12502;&12523;&12363;&12425;&12377;&12409;&12390;&12398;&25237;&36039;&20250;&31038;(fund)&12434;&21462;&24471;&12377;&12427;&26041;&27861;&12434;&31034;&12375;&12414;&12377;&12290;&12381;&12428;&12399;SQL&12434;&12372;&23384;&30693;&12398;&26041;&12395;&12399;&12392;&12390;&12418;&31777;&21336;&12395;&29702;&35299;&12391;&12365;&12427;&12418;&12398;&12391;&12377;&12290;EntityManager.createQuery()&12399;EJB QL&25991;&12434;&20351;&12387;&12390;&12463;&12456;&12522;&12540;&12434;&29983;&25104;&12375;&12414;&12377;&12290;listResults()&12513;&12477;&12483;&12489;&12399;&12381;&12398;&12463;&12456;&12522;&12540;&12395;&12510;&12483;&12481;&12377;&12427;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12398;&12467;&12524;&12463;&12471;&12519;&12531;&12434;&36820;&12375;&12414;&12377;&12290;

     @Stateless
     public class QueryCalculator implements Calculator {
     
      @PersistenceContext
      protected EntityManager em;
     
      public Collection <Fund> getFunds () {
        return em.createQuery("from Fund f").getResultList();
      }
      
      // ... ...
     }
    

    The following snippet shows how to retrieve a complete list of rows in the TimedRecord table. The results are returned with the latest records at the beginning of the list.

     

    &27425;&12398;&12467;&12540;&12489;&12399;TimedRecord&12486;&12540;&12502;&12523;&12398;&34892;&12398;&23436;&20840;&12394;&12522;&12473;&12488;&12434;&21462;&24471;&12377;&12427;&26041;&27861;&12434;&31034;&12375;&12414;&12377;&12290;&32080;&26524;&12399;&12522;&12473;&12488;&12398;&26368;&21021;&12395;&12418;&12387;&12392;&12418;&26368;&36817;&12398;&35352;&37682;&12364;&36820;&12373;&12428;&12414;&12377;&12290;

     @Stateless
     public class QueryCalculator implements Calculator {
     
      @PersistenceContext
      protected EntityManager em;
     
      // ... ...
     
      public Collection <TimedRecord> getRecords () {
        return em.createQuery(
          "from TimedRecord r order by r.ts desc").getResultList();
      }
      
      // ... ...
     }
    

    To see the above code in action, click on the button below to try out the sample application from the last trail.

     

    &19978;&12398;&12467;&12540;&12489;&12364;&21205;&12367;&27096;&23376;&12434;&35211;&12427;&12383;&12417;&12395;&12399;&12289;&20197;&19979;&12398;&12508;&12479;&12531;&12434;&21481;&12356;&12390;&21069;&22238;&12398;&23567;&36947;&12398;&12469;&12531;&12503;&12523;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12434;&35430;&12375;&12390;&12415;&12390;&19979;&12373;&12356;&12290;

     

    &12487;&12540;&12479;&12505;&12540;&12473;&26908;&32034; (Search the database)

     

    Of course, you can also search the database using the EJB QL. In the following example, we search all TimedRecord beans that have a specific range for the result attribute value. Please note that the EJB QL statement can be parameterized like a JDBC prepared statement. The parameters in the query are dynamically set by the application.

     

    &12418;&12385;&12429;&12435;&12289;EJB QL&12434;&20351;&12387;&12390;&12487;&12540;&12479;&12505;&12540;&12473;&12434;&26908;&32034;&12377;&12427;&12371;&12392;&12418;&21487;&33021;&12391;&12377;&12290;&27425;&12398;&20363;&12391;&12399;&12289;result&23646;&24615;&12398;&20516;&12364;&29305;&23450;&12398;&31684;&22258;&12395;&21454;&12414;&12427;&12424;&12358;&12394;&12377;&12409;&12390;&12398;TimedRecord Bean&12434;&26908;&32034;&12375;&12414;&12377;&12290;EJB QL&25991;&12399;JDBC prepared statement&12398;&12424;&12358;&12395;&12497;&12521;&12513;&12479;&21270;&12391;&12365;&12427;&12371;&12392;&12395;&27880;&24847;&12375;&12390;&12367;&12384;&12373;&12356;&12290;&12463;&12456;&12522;&12540;&20013;&12398;&12497;&12521;&12513;&12479;&12399;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12395;&12424;&12387;&12390;&21205;&30340;&12395;&35373;&23450;&12373;&12428;&12414;&12377;&12290;

     @Stateless
     public class QueryCalculator implements Calculator {
     
      @PersistenceContext
      protected EntityManager em;
     
      // ... ...
      
      public Collection <TimedRecord> filterRecords (double low, double high) {
        return em.createQuery(
            "from TimedRecord r where r.result > :low AND r.result < :high")
            .setParameter ("low", new Double (low))
            .setParameter ("high", new Double (high))
            .getResultList();
      }
     
     }
    

    Click on the following button to try the search application.

     

    &27425;&12398;&12508;&12479;&12531;&12434;&12463;&12522;&12483;&12463;&12375;&12390;&26908;&32034;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12434;&35430;&12375;&12390;&12367;&12384;&12373;&12356;&12290;

     

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

     

    Session bean of the search application

     

    &26908;&32034;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12398;&12475;&12483;&12471;&12519;&12531;Bean

     

    • Calculator.java is the stateless session bean interface

    • Calculator.java&12399;&12473;&12486;&12540;&12488;&12524;&12473;&12475;&12483;&12471;&12519;&12531;Bean&12452;&12531;&12479;&12501;&12455;&12540;&12473;&12391;&12377;&12290;

    • QueryCalculatorBean.java is the session bean implement that performs the database query

    • QueryCalculatorBean.java&12399;&12487;&12540;&12479;&12505;&12540;&12473;&26908;&32034;&12434;&23455;&34892;&12377;&12427;&12475;&12483;&12471;&12519;&12531;Bean&23455;&35013;&12391;&12377;&12290;

     

    The JSP user interface of the search application

     

    &26908;&32034;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12398;JSP&12518;&12540;&12470;&12452;&12531;&12479;&12501;&12455;&12540;&12473;

     

    • filter.jsp

     

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

     

    In this trail, you learned how to query and retrieve entity beans stored in databases. In the next trail, we will cover another database operation: to update the data and synchronize the object model with the database.

     

    &12371;&12398;&23567;&36947;&12391;&12399;&12289;&12487;&12540;&12479;&12505;&12540;&12473;&12395;&26684;&32013;&12373;&12428;&12383;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12398;&12463;&12456;&12522;&12540;&12392;&21462;&24471;&12398;&26041;&27861;&12434;&23398;&12403;&12414;&12375;&12383;&12290;&27425;&12398;&23567;&36947;&12391;&12399;&12289;&21029;&12398;&12487;&12540;&12479;&12505;&12540;&12473;&25805;&20316;&12391;&12354;&12427;&12289;&12487;&12540;&12479;&12398;&26356;&26032;&12392;&12458;&12502;&12472;&12455;&12463;&12488;&12514;&12487;&12523;&12434;&12487;&12540;&12479;&12505;&12540;&12473;&12392;&21516;&26399;&12373;&12379;&12427;&12371;&12392;&12434;&25201;&12356;&12414;&12377;&12290;(&35379;&27880;: cover&12434;&19978;&25163;&12367;&35379;&12379;&12390;&12356;&12394;&12356;)

     

    c 2005 JBoss, Inc. All Rights Reserved