Version 7

    JBoss EJB 3.0&12392;&25313;&24373;&27231;&33021;

     

    &12456;&12531;&12486;&12451;&12486;&12451;Bean&12398;O/R&12510;&12483;&12500;&12531;&12464; (Object / Relational Mapping with Entity Bean)

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

     

    To build database driven applications, Java developers typically need to manage and model data in two different structures.

     

    &12487;&12540;&12479;&12505;&12540;&12473;&39366;&21205;&12398;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12434;&20316;&12427;&12383;&12417;&12395;&12399;&12289;Java&38283;&30330;&32773;&12399;&36890;&24120;&65298;&12388;&12398;&30064;&12394;&12427;&27083;&36896;&12391;&12487;&12540;&12479;&12434;&31649;&29702;&12539;&12514;&12487;&12523;&21270;&12375;&12414;&12377;&12290;

     

    • Inside the application, the data is modeled by Java objects in the heap memory. The entire Java application is architected around those data objects. The Java &12288;language and APIs are designed to handle objects efficiently. And Java developers are productive with object models.

     

    • &12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&20869;&37096;&12391;&12399;&12289;&12381;&12398;&12487;&12540;&12479;&12399;&12498;&12540;&12503;&12513;&12514;&12522;&19978;&12391;Java&12458;&12502;&12472;&12455;&12463;&12488;&12395;&12424;&12387;&12390;&12514;&12487;&12523;&21270;&12373;&12428;&12414;&12377;&12290;Java&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&20840;&20307;&12399;&12289;&12371;&12428;&12425;&12398;&12487;&12540;&12479;&12458;&12502;&12472;&12455;&12463;&12488;&12398;&38598;&12414;&12426;&12395;&12424;&12387;&12390;&24418;&25104;&12373;&12428;&12414;&12377;&12290;Java&35328;&35486;&12392;API&12399;&12458;&12502;&12472;&12455;&12463;&12488;&12434;&21177;&29575;&30340;&12395;&25201;&12360;&12427;&12424;&12358;&35373;&35336;&12373;&12428;&12390;&12356;&12414;&12377;&12290;&12381;&12375;&12390;Java&38283;&30330;&32773;&12399;&12458;&12502;&12472;&12455;&12463;&12488;&12514;&12487;&12523;&12395;&12424;&12387;&12390;&29983;&29987;&24615;&12434;&21521;&19978;&12373;&12379;&12427;&12371;&12392;&12364;&12391;&12365;&12414;&12377;&12290;

     

    • When the data is stored in the relational database, it has to be modeled in database tables. The relational database is a fast and reliable persist storage technology. It works in distributed environments and enables efficient search in large data sets.

     

    • &12381;&12398;&12487;&12540;&12479;&12364;&12522;&12524;&12540;&12471;&12519;&12490;&12523;&12487;&12540;&12479;&12505;&12540;&12473;&19978;&12391;&26684;&32013;&12373;&12428;&12427;&12392;&12365;&12289;&12381;&12428;&12399;&12487;&12540;&12479;&12505;&12540;&12473;&12398;&12486;&12540;&12502;&12523;&19978;&12391;&12514;&12487;&12523;&21270;&12373;&12428;&12414;&12377;&12290;&12522;&12524;&12540;&12471;&12519;&12490;&12523;&12487;&12540;&12479;&12505;&12540;&12473;&12399;&39640;&36895;&12391;&20449;&38972;&24615;&12398;&39640;&12356;&27704;&32154;&12473;&12488;&12524;&12540;&12472;&25216;&34899;&12391;&12377;&12290;&12381;&12428;&12399;&20998;&25955;&29872;&22659;&12391;&21205;&20316;&12375;&12414;&12377;&12375;&12289;&22823;&37327;&12487;&12540;&12479;&12475;&12483;&12488;&12391;&12418;&21177;&29575;&30340;&12394;&26908;&32034;&12364;&21487;&33021;&12391;&12377;&12290;

     

    Manipulating Java objects and relational tables requires different language syntax and APIs. So, developers have to model the same data twice and switch between the two systems throughout the application. It is inefficient and error-prone.

     

    Java&12458;&12502;&12472;&12455;&12463;&12488;&12392;&12522;&12524;&12540;&12471;&12519;&12490;&12523;&12486;&12540;&12502;&12523;&12434;&25805;&20316;&12377;&12427;&12395;&12399;&30064;&12394;&12427;&12471;&12531;&12479;&12463;&12473;&12420;API&12364;&24517;&35201;&12395;&12394;&12426;&12414;&12377;&12290;&12381;&12428;&12422;&12360;&12289;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12398;&20840;&20307;&12434;&36890;&12375;&12390;&12289;&38283;&30330;&32773;&12399;&21516;&12376;&12487;&12540;&12479;&12395;&23550;&12375;&12390;2&22238;&12398;&12514;&12487;&12523;&21270;&12434;&12375;&12390;&12289;&12371;&12428;&12425;2&12388;&12398;&12471;&12473;&12486;&12512;&38291;&12391;&12487;&12540;&12479;&12434;&20999;&12426;&26367;&12360;&12427;&24517;&35201;&12364;&12354;&12427;&12398;&12391;&12377;&12290;&12371;&12428;&12399;&21177;&29575;&12364;&24746;&12367;&12289;&38291;&36949;&12356;&12420;&12377;&12356;&12420;&12426;&26041;&12391;&12377;&12290;

     

    The entity beans are designed to bridge the gap between the objects representation and relational representation of application data. From the Java developer's view, EJB 3.0 entity beans are just plain old Java objects (POJOs) with annotations that specify how the object should be stored in the database. The mapping from the objects to relational database tables is done automatically and transparently by the EJB 3.0 container. The Java developer no longer needs to worry about the details of the database table schema, database connection management, and specific database access APIs.

     

    &12456;&12531;&12486;&12451;&12486;&12451;Bean&12399;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12487;&12540;&12479;&12398;&12458;&12502;&12472;&12455;&12463;&12488;&34920;&29694;&12392;&12522;&12524;&12540;&12471;&12519;&12490;&12523;&34920;&29694;&38291;&12398;&12371;&12398;&12462;&12515;&12483;&12503;&12434;&22475;&12417;&12427;&12383;&12417;&12395;&35373;&35336;&12373;&12428;&12414;&12375;&12383;&12290;Java&38283;&30330;&32773;&12398;&35222;&28857;&12363;&12425;&35211;&12427;&12392;&12289;EJB 3.0&12456;&12531;&12486;&12451;&12486;&12451;Bean&12392;&12399;&12289;&12458;&12502;&12472;&12455;&12463;&12488;&12364;&12393;&12398;&12424;&12358;&12395;&12487;&12540;&12479;&12505;&12540;&12473;&12395;&26684;&32013;&12373;&12428;&12427;&12398;&12363;&12434;&31034;&12375;&12383;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12398;&20184;&12356;&12383;&21336;&12394;&12427;POJO(plain old Java object)&12395;&36942;&12366;&12414;&12379;&12435;&12290;&12458;&12502;&12472;&12455;&12463;&12488;&12363;&12425;&12522;&12524;&12540;&12471;&12519;&12490;&12523;&12487;&12540;&12479;&12505;&12540;&12473;&12398;&12486;&12540;&12502;&12523;&12408;&12398;&12510;&12483;&12500;&12531;&12464;&12399;&12289;EJB 3.0&12395;&12424;&12387;&12390;&33258;&21205;&30340;&12363;&12388;&36879;&26126;&12395;&34892;&12431;&12428;&12414;&12377;&12290;Java&38283;&30330;&32773;&12399;&12418;&12399;&12420;&12487;&12540;&12479;&12505;&12540;&12473;&12398;&12486;&12540;&12502;&12523;&12473;&12461;&12540;&12510;&12289;&12487;&12540;&12479;&12505;&12540;&12473;&12398;&12467;&12493;&12463;&12471;&12519;&12531;&31649;&29702;&12289;&29305;&23450;&12398;&12487;&12540;&12479;&12505;&12540;&12473;&12450;&12463;&12475;&12473;API&12398;&35443;&32048;&12395;&12388;&12356;&12390;&30693;&12427;&24517;&35201;&12399;&12394;&12356;&12398;&12391;&12377;&12290;

     

    &12469;&12531;&12503;&12523;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&20869;&12398;&12458;&12502;&12472;&12455;&12463;&12488;&27083;&36896;(Object structure in the sample application)

     

    In the next several trails, we develop a new investment calculator application to demonstrate how to use EJB 3.0 entity beans. The new investment calculator allows you to save a list of fund company profiles and a list of individual investor profiles in a relational database. For each investment calculation, you can choose a company and an individual from the database, and then enter a monthly saving amount to calculate the final investment return. Each calculation record is also saved in the database with a time stamp. We can later search and update the calculation records in the database.

     

    &12371;&12428;&12363;&12425;&12398;&23567;&36947;&12391;&12399;&12289;EJB 3.0&12456;&12531;&12486;&12451;&12486;&12451;Bean&12398;&20351;&12356;&26041;&12434;&12487;&12514;&12377;&12427;&12383;&12417;&12395;&26032;&12375;&12356;&25237;&36039;&35336;&31639;&27231;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12434;&38283;&30330;&12375;&12414;&12377;&12290;&12371;&12398;&26032;&12375;&12356;&25237;&36039;&35336;&31639;&27231;&12395;&12399;&25237;&36039;&20250;&31038;(fund company)&12398;&12503;&12525;&12501;&12449;&12452;&12523;&12398;&12522;&12473;&12488;&12420;&20491;&20154;&25237;&36039;&23478;(indivisual investor)&12398;&12503;&12525;&12501;&12449;&12452;&12523;&12398;&12522;&12473;&12488;&12434;&12522;&12524;&12540;&12471;&12519;&12490;&12523;&12487;&12540;&12479;&12505;&12540;&12473;&12395;&26684;&32013;&12391;&12365;&12414;&12377;&12290;&12381;&12428;&12382;&12428;&12398;&25237;&36039;&35336;&31639;&12391;&12399;&12289;&12487;&12540;&12479;&12505;&12540;&12473;&12363;&12425;&25237;&36039;&20250;&31038;&12392;&25237;&36039;&23478;&12434;&36984;&25246;&12375;&12289;&27425;&12395;&26376;&25499;&36015;&37329;(monthly saving amount)&12434;&20837;&21147;&12377;&12427;&12371;&12392;&12391;&12289;&26368;&32066;&30340;&12394;&25237;&36039;&21454;&30410;(investment return)&12434;&35336;&31639;&12375;&12377;&12427;&12371;&12392;&12364;&12391;&12365;&12414;&12377;&12290;&21508;&35336;&31639;&35352;&37682;&12418;&12289;&12479;&12452;&12512;&12473;&12479;&12531;&12503;&20184;&12365;&12391;&12487;&12540;&12479;&12505;&12540;&12473;&12395;&20445;&23384;&12373;&12428;&12414;&12377;&12290;&12487;&12540;&12479;&12505;&12540;&12473;&19978;&12391;&12399;&24460;&12363;&12425;&35336;&31639;&35352;&37682;&12398;&26908;&32034;&12392;&26356;&26032;&12364;&21487;&33021;&12391;&12377;&12290;

     

    We construct the following object class hierarchy to model the data in the above investment calculator application. All classes conform to simple JavaBeans convention (i.e., standard getter and setter methods for data attributes).

     

    &19978;&35352;&12398;&25237;&36039;&35336;&31639;&27231;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12395;&12362;&12369;&12427;&12487;&12540;&12479;&12434;&12514;&12487;&12523;&21270;&12377;&12427;&12383;&12417;&27425;&12398;&12424;&12358;&12394;&12458;&12502;&12472;&12455;&12463;&12488;&12463;&12521;&12473;&38542;&23652;&12434;&27083;&31689;&12375;&12414;&12377;&12290;&12377;&12409;&12390;&12398;&12463;&12521;&12473;&12399;&21336;&32020;&12394;JavaBeans&12467;&12531;&12505;&12531;&12471;&12519;&12531;(&12388;&12414;&12426;&12289;&12487;&12540;&12479;&23646;&24615;&12395;&12450;&12463;&12475;&12473;&12377;&12427;&12383;&12417;&27161;&28310;&30340;&12394;&12398;getter&12289;setter&12513;&12477;&12483;&12489;)&12395;&24467;&12356;&12414;&12377;&12290;

    ()&35379;&27880;&65306;&12371;&12371;&12391;&12398;convention&12399;&12300;&24931;&32722;&12301;&12392;&35379;&12373;&12428;&12427;&12371;&12392;&12364;&22810;&12356;&12391;&12377;&12364;&12289;&12371;&12428;&12399;&24931;&32722;&12392;&12356;&12358;&12424;&12426;&12300;&32004;&26463;&20107;&12301;&12420;&12300;&35215;&32004;&12301;&12398;&12371;&12392;&12391;&12377;&12290;&12371;&12371;&12391;&12399;&12354;&12360;&12390;&12467;&12531;&12505;&12531;&12471;&12519;&12531;&12392;&35379;&12375;&12414;&12377;&12290;

     

    • The Fund class represents the profiles of available investment funds. It has a name attribute and an average annual growth rate attribute.

     

    • Fund&12463;&12521;&12473;&12399;&21033;&29992;&21487;&33021;&12394;&25237;&36039;&12501;&12449;&12531;&12489;&12398;&12503;&12525;&12501;&12449;&12452;&12523;&12434;&34920;&29694;&12375;&12414;&12377;&12290;&12381;&12428;&12399;&21517;&21069;&23646;&24615;&12392;&24179;&22343;&24180;&25104;&38263;&29575;&23646;&24615;&12434;&25345;&12385;&12414;&12377;&12290;

     

    • The Investor class represents the profiles of individual investors. It has the following data attributes: an investor name, a start age, and an end age of investment.

     

    • Investor&12463;&12521;&12473;&12399;&20491;&20154;&25237;&36039;&23478;&12398;&12503;&12525;&12501;&12449;&12452;&12523;&12434;&34920;&29694;&12375;&12414;&12377;&12290;&12381;&12428;&12399;&12487;&12540;&12479;&23646;&24615;&12392;&12375;&12390;&25237;&36039;&23478;&12398;&21517;&21069;&12289;&25237;&36039;&12398;&38283;&22987;&24180;&40802;&12392;&32066;&20102;&24180;&40802;&12434;&21547;&12415;&12414;&12377;&12290;

     

    • The Record class represents an investment calculation. It is composed of the following data attributes: a Fund object, an Investor object, a monthly saving amount, and a final result.

     

    • Record&12463;&12521;&12473;&12399;&25237;&36039;&35336;&31639;&12434;&34920;&29694;&12375;&12414;&12377;&12290;&12381;&12428;&12399;&12487;&12540;&12479;&23646;&24615;&12392;&12375;&12390;Fund&12458;&12502;&12472;&12455;&12463;&12488;&12289;Investor&12458;&12502;&12472;&12455;&12463;&12488;&12289;&26376;&25499;&36015;&37329;(monthly saving amount)&12289;&25237;&36039;&21454;&30410;&12434;&21547;&12415;&12414;&12377;&12290;

     

    • The TimedRecord class represents a time stamped calculation record. It inherits from the Record class with an additional timestamp data attribute.

     

    • TimeRecord&12463;&12521;&12473;&12399;&26178;&21051;&24773;&22577;&20184;&12365;&35336;&31639;&35352;&37682;&12434;&34920;&29694;&12375;&12414;&12377;&12290;&12381;&12428;&12399;Record&12463;&12521;&12473;&12434;&32153;&25215;&12375;&12289;&12479;&12452;&12512;&12473;&12479;&12531;&12503;&12398;&12487;&12540;&12479;&23646;&24615;&12364;&36861;&21152;&12373;&12428;&12390;&12356;&12414;&12377;&12290;

     

    The relationships between those classes are illustrated in the following figure.

    &12371;&12428;&12425;&12463;&12521;&12473;&38291;&12398;&38306;&20418;&12399;&27425;&22259;&12391;&34920;&29694;&12373;&12428;&12414;&12377;&12290;

     

    Fund&12456;&12531;&12486;&12451;&12486;&12451;Bean (The Fund entity bean)

     

    Tagged with the @Entity annotation, the Fund POJO class becomes an entity bean. Typically, the EJB 3.0 container maps each entity bean class to a single table in the database. The @Table annotation tells the container the name of the table. Each instance of the entity bean represents a row of data in the table. Each column in the table corresponds to a data attribute in the entity bean. Below is a snippet from the Fund entity bean class.

     

    @Entity&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12398;&12479;&12464;&12434;&12388;&12369;&12427;&12392;&12289;Fund POJO&12463;&12521;&12473;&12399;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12395;&12394;&12426;&12414;&12377;&12290;&36890;&24120;&12399;&12289;EJB 3.0&12467;&12531;&12486;&12490;&12399;&21508;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12463;&12521;&12473;&12434;&12487;&12540;&12479;&12505;&12540;&12473;&20869;&12398;&21336;&19968;&12486;&12540;&12502;&12523;&12395;&12510;&12483;&12503;&12375;&12414;&12377;&12290;@Table&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12399;&12467;&12531;&12486;&12490;&12395;&23550;&12375;&12390;&12486;&12540;&12502;&12523;&21517;&12434;&25945;&12360;&12414;&12377;&12290;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12398;&21508;&12452;&12531;&12473;&12479;&12531;&12473;&12399;&12289;&12381;&12398;&12486;&12540;&12502;&12523;&12395;&12362;&12369;&12427;&12487;&12540;&12479;&12398;&34892;&12434;&34920;&29694;&12375;&12414;&12377;&12290;&12381;&12398;&12486;&12540;&12502;&12523;&12398;&21508;&12459;&12521;&12512;&12399;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12398;&12487;&12540;&12479;&23646;&24615;&12395;&30456;&24403;&12375;&12414;&12377;&12290;&20197;&19979;&12399;Fund&12456;&12531;&12486;&12451;&12486;&12451;Bean&12463;&12521;&12473;&12363;&12425;&25244;&31883;&12375;&12383;&12418;&12398;&12391;&12377;&12290;

     @Entity
     @Table(name = "fund")
     public class Fund implements Serializable {
      private int id;
      private String name;
      private double growthrate;
     
      public Fund () { }
     
      public Fund (String name, double growthrate) {
        this.name = name;
        this.growthrate = growthrate;
      }
     
      @Id
      @GeneratedValue
      public int getId () {
        return id;
      }
     
      public void setId (int id) {
        this.id = id;
      }
     
      public String getName () {
        return name;
      }
     
      public void setName (String name) {
        this.name = name;
      }
     
      // Other getter and setter methods ...
     }
    

    The @Id annotation specifies that the Id attribute is the primary key of the table. The @GeneratedValue annotation indicates that the primary key value is automatically generated by the server. The EJB 3.0 container sets the id for each Fund bean instance it saves to the database. You can also optionally specify the column name for each data attribute in Fund. In this class, the EJB 3.0 container uses the Java Bean attribute name as the default column name. The schema for the mapped database table is shown as follows.

     

    @Id&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12399;&25351;&23450;&12375;&12383;Id&23646;&24615;&12364;&12381;&12398;&12486;&12540;&12502;&12523;&12398;&20027;&12461;&12540;&12391;&12354;&12427;&12371;&12392;&12434;&31034;&12375;&12414;&12377;&12290;@GeneratedValue&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12399;&12381;&12398;&20027;&12461;&12540;&12398;&20516;&12364;&12469;&12540;&12496;&12395;&12424;&12387;&12390;&33258;&21205;&29983;&25104;&12373;&12428;&12427;&12371;&12392;&12434;&34920;&12375;&12414;&12377;&12290;EJB 3.0&12467;&12531;&12486;&12490;&12399;&12289;&12487;&12540;&12479;&12505;&12540;&12473;&12395;&26684;&32013;&12391;&12365;&12427;&12424;&12358;&12395;&21508;Fund Bean&12452;&12531;&12473;&12479;&12531;&12473;&12395;&23550;&12375;&12390;&12381;&12398;id&12434;&35373;&23450;&12375;&12414;&12377;&12290;&12371;&12398;&12463;&12521;&12473;&12391;&12399;&12289;EJB 3.0&12467;&12531;&12486;&12490;&12399;&12487;&12501;&12457;&12523;&12488;&12392;&12394;&12427;&12459;&12521;&12512;&21517;&12392;&12375;&12390;Java Bean&23646;&24615;&21517;&12434;&20351;&29992;&12375;&12414;&12377;&12290;&12510;&12483;&12503;&12373;&12428;&12427;&12487;&12540;&12479;&12505;&12540;&12473;&12486;&12540;&12502;&12523;&12398;&12473;&12461;&12540;&12510;&12399;&27425;&12398;&12424;&12358;&12395;&12394;&12426;&12414;&12377;&12290;

     

    Investor&12456;&12531;&12486;&12451;&12486;&12451;Bean (The Investor entity bean)

     

    Similar to the Fund bean, the Investor class is another simple entity bean that can be directly mapped to a database table. Below is a snippet from the Investor class.

     

    Fund Bean&12392;&21516;&27096;&12395;&12289;Investor&12463;&12521;&12473;&12399;&12289;&12487;&12540;&12479;&12505;&12540;&12473;&12486;&12540;&12502;&12523;&12395;&30452;&25509;&12510;&12483;&12503;&21487;&33021;&12394;&12418;&12358;&19968;&12388;&12398;&31777;&21336;&12394;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12391;&12377;&12290;&20197;&19979;&12399;Investor&12463;&12521;&12473;&12363;&12425;&12398;&25244;&31883;&12391;&12377;&12290;

     @Entity
     @Table(name = "investor")
     public class Investor implements Serializable {
      private int id;
      private String name;
      private int startAge;
      private int endAge;
     
      public Investor () { }
     
      public Investor (String name, int startAge, int endAge) {
        this.name = name;
        this.start = startAge;
        this.end = endAge;
      }
     
      @Id
      @GeneratedValue
      public int getId () {
        return id;
      }
     
      public void setId (int id) {
        this.id = id;
      }
     
      public int getStartAge () {
        return startAge;
      }
     
      public void setStartAge (int startAge) {
        this.startAge = startAge;
      }
     
      // Other getter and setter methods ...
     }
    

    The following figure shows the database schema for the mapped investor table.

     

    &27425;&22259;&12399;&12510;&12483;&12503;&12373;&12428;&12383;&25237;&36039;&23478;&12486;&12540;&12502;&12523;&12398;&12487;&12540;&12479;&12505;&12540;&12473;&12473;&12461;&12540;&12510;&12434;&31034;&12375;&12414;&12377;&12290;

     

    Record&12456;&12531;&12486;&12451;&12486;&12451;Bean (The Record entity bean)

     

    Mapping the Record entity bean is more complex than mapping Fund and Investor beans. In addition to simple data attributes, the Record contains attributes that are themselves entity beans, such as the Fund and Investor beans. To map these relationships, the table for the Record bean has foreign ID columns. The foreign ID columns in each Record row contain the primary IDs of the associated data rows in the Fund and Investor tables. You can specify the name of foreign ID column by annotating the related attributes with the @JoinColumn tag. If you omit this annotation, the EJB 3.0 server would choose a default column name for you.

     

    Record&12456;&12531;&12486;&12451;&12486;&12451;Bean&12398;&12510;&12483;&12500;&12531;&12464;&12399;Fund&12420;Investor Bean&12398;&12510;&12483;&12500;&12531;&12464;&12424;&12426;&12418;&12373;&12425;&12395;&35079;&38609;&12391;&12377;&12290;Record&12399;Fund&12420;Investor Bean&12398;&12424;&12358;&12394;&12289;&12381;&12428;&33258;&36523;&12364;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12391;&12354;&12427;&12424;&12358;&12394;&23646;&24615;&12434;&21547;&12415;&12414;&12377;&12290;&12371;&12428;&12425;&12398;&38306;&20418;&12434;&12510;&12483;&12503;&12377;&12427;&12395;&12399;&12289;Record Bean&12398;&12486;&12540;&12502;&12523;&12399;&22806;&37096;ID&12459;&12521;&12512;&12434;&25345;&12385;&12414;&12377;&12290;&21508;Record&34892;&12398;&22806;&37096;ID&12459;&12521;&12512;&12399;&12289;Fund&12420;Investor&12486;&12540;&12502;&12523;&20869;&12398;&38306;&36899;&12377;&12427;&12487;&12540;&12479;&34892;&12398;&12381;&12428;&12382;&12428;&12398;&20027;&12461;&12540;&12434;&21547;&12415;&12414;&12377;&12290;&38306;&20418;&12377;&12427;&23646;&24615;&12395;@JoinColumn&12479;&12464;&12398;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12434;&20184;&21152;&12377;&12427;&12371;&12392;&12395;&12424;&12387;&12390;&22806;&37096;ID&12459;&12521;&12512;&21517;&12434;&25351;&23450;&12377;&12427;&12371;&12392;&12364;&21487;&33021;&12391;&12377;&12290;&12418;&12375;&12289;&12371;&12398;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12434;&30465;&30053;&12377;&12428;&12400;&12289;EJB 3.0&12469;&12540;&12496;&12399;&12518;&12540;&12470;&12398;&12383;&12417;&12395;&12487;&12501;&12457;&12523;&12488;&12398;&12459;&12521;&12512;&21517;&12434;&36984;&25246;&12375;&12390;&12367;&12428;&12414;&12377;&12290;

     

    Annotations can also be used to specify the constraints of foreign keys. For instance, the @ManyToOne specifies that each Record bean is associated with only one Fund bean and one Investor bean. In the meanwhile, each Fund or Investor bean can be associated with many Record beans. The EJB 3.0 container automatically enforces these constraints in the database. The entity relationship annotations in EJB 3.0 include the following.

     

    &12450;&12494;&12486;&12540;&12471;&12519;&12531;&12399;&22806;&37096;&12461;&12540;&12398;&21046;&32004;&12434;&25351;&23450;&12377;&12427;&12383;&12417;&12395;&12418;&20351;&29992;&12377;&12427;&12371;&12392;&12364;&12391;&12365;&12414;&12377;&12290;&20363;&12360;&12400;&12289;@ManyToOne&12399;&21508;Record Bean&12364;&19968;&12388;&12398;Fund Bean&12392;&19968;&12388;&12398;Investor Bean&12384;&12369;&12395;&38306;&36899;&20184;&12369;&12425;&12428;&12427;&12371;&12392;&12434;&25351;&23450;&12375;&12414;&12377;&12290;&19968;&26041;&12289;&21508;Fund&12420;Investor Bean&12399;&12289;&35079;&25968;&12398;Record Bean&12392;&38306;&36899;&20184;&12369;&12427;&12371;&12392;&12364;&21487;&33021;&12391;&12377;&12290;EJB 3.0&12467;&12531;&12486;&12490;&12399;&33258;&21205;&30340;&12395;&12371;&12428;&12425;&12398;&21046;&32004;&12434;&12487;&12540;&12479;&12505;&12540;&12473;&19978;&12395;&24375;&21046;&12375;&12414;&12377;&12290;EJB 3.0&12395;&12362;&12369;&12427;&12456;&12531;&12486;&12451;&12486;&12451;&12398;&38306;&20418;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12399;&27425;&12398;&12418;&12398;&12434;&21547;&12415;&12414;&12377;&12290;

     

    • @ManyToOne: For each bean instance (e.g., a Record), the annotated attribute is associated with one external entity bean object (e.g., a Fund). But multiple bean instances could be associated with the same external object.

     

    • @ManyToOne: &12381;&12428;&12382;&12428;&12398;Bean&12452;&12531;&12473;&12479;&12531;&12473;&65288;&20363;&12360;&12400;Record&65289;&12391;&12289;&12371;&12398;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12398;&20184;&12356;&12383;&23646;&24615;&12399;&19968;&12388;&12398;&22806;&37096;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12458;&12502;&12472;&12455;&12463;&12488;&65288;&20363;&12360;&12400;Fund&65289;&12395;&38306;&36899;&20184;&12369;&12425;&12428;&12414;&12377;&12290;&12375;&12363;&12375;&12289;&35079;&25968;Bean&12452;&12531;&12473;&12479;&12531;&12473;&12399;&21516;&19968;&12398;&22806;&37096;&12458;&12502;&12472;&12455;&12463;&12488;&12395;&38306;&36899;&12389;&12369;&12427;&12371;&12392;&12418;&12391;&12365;&12414;&12377;&12290;

     

    • @OneToMany: For each bean instance (e.g., a book), the annotated attribute is a Collection associated with many external entity bean object (e.g., the book's authors).

     

    • @OneToMany: &12381;&12428;&12382;&12428;&12398;Bean&12452;&12531;&12473;&12479;&12531;&12473;&65288;&20363;&12360;&12400;&12300;&26412;&12301;&65289;&12391;&12289;&12371;&12398;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12398;&20184;&12356;&12383;&23646;&24615;&12399;&22810;&12367;&12398;&22806;&37096;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12458;&12502;&12472;&12455;&12463;&12488;&65288;&20363;&12360;&12400;&12300;&26412;&12398;&33879;&32773;&12301;&65289;&12395;&38306;&36899;&20184;&12369;&12425;&12428;&12383;Collection&12395;&12394;&12426;&12414;&12377;&12290;

     

    • @OneToOne: For each bean instance (e.g., a person), the annotated attribute is associated with one external entity bean object (e.g., a social security number). No two bean instances can be associated with the same external object.

     

    • @OneToOne: &12381;&12428;&12382;&12428;&12398;Bean&12452;&12531;&12473;&12479;&12531;&12473;&65288;&20363;&12360;&12400;&12300;&20154;&12301;&65289;&12391;&12289;&12371;&12398;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12398;&20184;&12356;&12383;&23646;&24615;&12399;&19968;&12388;&12398;&22806;&37096;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12458;&12502;&12472;&12455;&12463;&12488;&65288;&20363;&12360;&12400;&12300;&31038;&20250;&20445;&38556;&30058;&21495;&12301;&65289;&12395;&38306;&36899;&20184;&12369;&12425;&12428;&12414;&12377;&12290;&20108;&12388;&12398;Bean&12452;&12531;&12473;&12479;&12531;&12473;&12434;&21516;&12376;&22806;&37096;&12458;&12502;&12472;&12455;&12463;&12488;&12395;&38306;&36899;&20184;&12369;&12427;&12371;&12392;&12399;&12391;&12365;&12414;&12379;&12435;&12290;

     

    • @ManyToMany: For each bean instance (e.g., an employee), the annotated attribute is associated with multiple external entity bean objects (e.g., an array of job titles). Multiple bean instances could be associated with the same external object.

     

    • @ManyToMany: &12381;&12428;&12382;&12428;&12398;Bean&12452;&12531;&12473;&12479;&12531;&12473;&65288;&20363;&12360;&12400;&12300;&39015;&23458;&12301;&65289;&12391;&12289;&12371;&12398;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12398;&20184;&12356;&12383;&23646;&24615;&12399;&35079;&25968;&12398;&22806;&37096;&12456;&12531;&12486;&12451;&12486;&12451;&12458;&12502;&12472;&12455;&12463;&12488;&65288;&20363;&12360;&12400;&12300;&32937;&26360;&12365;&12301;&12398;&37197;&21015;&65289;&12395;&38306;&36899;&20184;&12369;&12425;&12428;&12414;&12377;&12290;&35079;&25968;&12398;Bean&12452;&12531;&12473;&12479;&12531;&12473;&12399;&21516;&19968;&12398;&22806;&37096;&12458;&12502;&12472;&12455;&12463;&12488;&12395;&38306;&36899;&20184;&12369;&12427;&12371;&12392;&12364;&12391;&12365;&12414;&12377;&12290;

     

    Below is the relevant snippet from the Record class.

     

    &20197;&19979;&12399;Record&12463;&12521;&12473;&38306;&36899;&12398;&25244;&31883;&12391;&12377;&12290;

     @Entity
     @Table(name = "record")
     // ... ...
     public class Record implements Serializable {
      protected int id;
      protected Fund fund;
      protected Investor investor;
      protected double saving;
      protected double result;
     
      // ... ...
     
      @ManyToOne(optional=false)
      @JoinColumn(name="my_fundid")
      public Fund getFund () {
        return fund;
      }
     
      @ManyToOne(optional=false)
      // Use the system-specified join column
      public Investor getInvestor () {
        return investor;
      }
     
      // Other getter and setter methods ...
     }
    

    The following figure shows the schema of the table that maps the Record entity bean. The my_fundid and investor_id columns are foreign key columns for the fund and investor attributes.

     

    &27425;&22259;&12399;Record&12456;&12531;&12486;&12451;&12486;&12451;Bean&12434;&12510;&12483;&12503;&12377;&12427;&12486;&12540;&12502;&12523;&12398;&12473;&12461;&12540;&12510;&12434;&31034;&12375;&12414;&12377;&12290;my_fundid&12392;investor_id&12459;&12521;&12512;&12399;&25237;&36039;(fund)&12392;&25237;&36039;&23478;(investor)&23646;&24615;&12398;&12383;&12417;&12398;&22806;&37096;&12461;&12540;&12459;&12521;&12512;&12391;&12377;&12290;

     

    Additional columns in the Record table are discussed in the next section.

     

    Record&12486;&12540;&12502;&12523;&12398;&27531;&12426;&12398;&12459;&12521;&12512;&12395;&12388;&12356;&12390;&12399;&27425;&12398;&12475;&12463;&12471;&12519;&12531;&12391;&35696;&35542;&12375;&12414;&12377;&12290;

     

    TimedRecord&12456;&12531;&12486;&12451;&12486;&12451;Bean (The TimedRecord entity bean)

     

    The TimedRecord class inherits from the Record class. There are several different ways to map the inheritance relationship. For instance, you can use a separate table for each class in the hierarchy and use foreign keys to constraint the relationship. In this trail, we demonstrate the use of a different mapping strategy -- the single table mapping strategy.

     

    TimedRecord&12463;&12521;&12473;&12399;Record&12463;&12521;&12473;&12434;&32153;&25215;&12375;&12414;&12377;&12290;&12452;&12531;&12504;&12522;&12479;&12531;&12473;&38306;&20418;&12434;&12510;&12483;&12503;&12377;&12427;&12395;&12399;&30064;&12394;&12427;&12356;&12367;&12388;&12363;&12398;&26041;&27861;&12364;&23384;&22312;&12375;&12414;&12377;&12290;&20363;&12360;&12400;&12289;&38542;&23652;&19978;&12398;&21508;&12463;&12521;&12473;&12395;&23550;&12375;&12390;&21029;&12293;&12398;&12486;&12540;&12502;&12523;&12434;&20351;&12356;&12289;&12381;&12398;&38306;&20418;&12395;&21046;&32004;&12434;&35373;&23450;&12377;&12427;&12383;&12417;&12395;&22806;&37096;&12461;&12540;&12434;&20351;&12358;&12371;&12392;&12364;&21487;&33021;&12391;&12377;&12290;&12371;&12398;&23567;&36947;&12391;&12399;&12289;&21336;&19968;&12486;&12540;&12502;&12523;&12510;&12483;&12500;&12531;&12464;&25126;&30053;&65288;sigle table mapping strategy&65289;&12392;&12356;&12358;&12510;&12483;&12500;&12531;&12464;&25126;&30053;&12434;&35500;&26126;&12375;&12414;&12377;&12290;

     

    The single table mapping strategy is the default inheritance mapping strategy for entity beans in EJB 3.0. It maps both the base Record class and the TimedRecord class to a single table. The table has columns to hold all attributes for all class in the hierarchy. Special columns in the table are used to differentiate which sub-class a particular row is an instance of.

     

    The mapping strategy and the differentiator column must be specified in the base class of the entire inheritance hierarchy. We tag the Record class with the @DiscriminatorColumn and @DiscriminatorValue annotations. In this example, the record_type column in the Record table is the class differentiator. If it has value B, the current row represents an instance of the Record class (the base class).

     

    &21336;&19968;&12486;&12540;&12502;&12523;&12510;&12483;&12500;&12531;&12464;&25126;&30053;&12399;EJB 3.0&12391;&12398;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12398;&12487;&12501;&12457;&12523;&12488;&12392;&12394;&12427;&12510;&12483;&12500;&12531;&12464;&25126;&30053;&12391;&12377;&12290;&12381;&12428;&12399;&22522;&24213;&12392;&12394;&12427;Record&12463;&12521;&12473;&12392;TimedRecord&12463;&12521;&12473;&12398;&20001;&26041;&12434;&12434;&21336;&19968;&12486;&12540;&12502;&12523;&12408;&12510;&12483;&12503;&12375;&12414;&12377;&12290;&12371;&12398;&12486;&12540;&12502;&12523;&12399;&12381;&12398;&12463;&12521;&12473;&38542;&23652;&19978;&12398;&12377;&12409;&12390;&12398;&12463;&12521;&12473;&12398;&12377;&12409;&12390;&12398;&23646;&24615;&12434;&20445;&25345;&12377;&12427;&12383;&12417;&12398;&12459;&12521;&12512;&12434;&25345;&12385;&12414;&12377;&12290;&12371;&12398;&12486;&12540;&12502;&12523;&12398;&29305;&27530;&12394;&12459;&12521;&12512;&12364;&12289;&29305;&23450;&12398;&34892;&12364;&12393;&12398;&12469;&12502;&12463;&12521;&12473;&12398;&12418;&12398;&12363;&12434;&35672;&21029;&12377;&12427;&12383;&12417;&12395;&20351;&12431;&12428;&12414;&12377;&12290;&12510;&12483;&12500;&12531;&12464;&25126;&30053;&12392;&35672;&21029;&12459;&12521;&12512;&65288;differentiator column&65289;&12399;&32153;&25215;&38542;&23652;&20840;&20307;&12398;&22522;&24213;&12463;&12521;&12473;&12391;&25351;&23450;&12373;&12428;&12394;&12369;&12428;&12400;&12394;&12426;&12414;&12379;&12435;&12290;Record&12463;&12521;&12473;&12395;@DiscriminatorColumn&12392;@DiscriminatorValue&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12398;&12479;&12464;&12434;&25351;&23450;&12375;&12390;&12356;&12414;&12377;&12290;&12371;&12398;&20363;&12391;&12399;&12289;Record&12486;&12540;&12502;&12523;&12398;record_type&12459;&12521;&12512;&12364;&12463;&12521;&12473;&35672;&21029;&23376;(class differentiator)&12391;&12377;&12290;&12418;&12375;&12381;&12428;&12364;&20516;B&12434;&25345;&12390;&12400;&12289;&29694;&22312;&34892;&12399;&65288;&22522;&24213;&12463;&12521;&12473;&12391;&12354;&12427;&65289;Record&12463;&12521;&12473;&12398;&12452;&12531;&12473;&12479;&12531;&12473;&12434;&34920;&29694;&12375;&12414;&12377;&12290;

     @Entity
     @Table(name = "record")
     @DiscriminatorColumn(name="record_type")
     @DiscriminatorValue(value="B")
     public class Record {
        // ... ...
     }
    

    The TimedRecord class simply inherits from the Record class. Its @DiscriminatorValue annotation specifies that all rows representing TimedRecord objects have value T in their differentiator column record_type (which is specified by the @DiscriminatorColumn annotation on the base class Record).

     

    TimedRecord&12463;&12521;&12473;&12399;Record&12463;&12521;&12473;&12434;&21336;&12395;&32153;&25215;&12375;&12414;&12377;&12290;&12381;&12398;@DiscriminatorValue&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12399;&12289;TimedRecord&12458;&12502;&12472;&12455;&12463;&12488;&12434;&34920;&29694;&12377;&12427;&12377;&12409;&12390;&12398;&34892;&12364;&20516;T&12434;&35672;&21029;&12459;&12521;&12512;record_type&12434;&25345;&12388;&12371;&12392;&12434;&25351;&23450;&12375;&12414;&12377;&65288;&12371;&12398;&12459;&12521;&12512;&12399;&22522;&24213;&12463;&12521;&12473;Record&12398;@DiscriminatorColumn&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12395;&12424;&12387;&12390;&25351;&23450;&12373;&12428;&12414;&12377;&65289;&12290;

     @Entity
     @DiscriminatorValue (value="T")
     public class TimedRecord extends Record {
     
      private Timestamp ts;
     
      // ... ...
     
      public Timestamp getTs () {
        return ts;
      }
     
      public void setTs (Timestamp ts) {
        this.ts = ts;
      }
     
     }
    

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

     

    The entity beans

     

    &12456;&12531;&12486;&12451;&12486;&12451;Bean

     

    • Fund.java

    • Investor.java

    • Record.java

    • TimedRecord.java

     

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

     

    Object / Relational mapping is a key concept behind entity beans. EJB 3.0 enables you to map Java POJOs to database tables automatically and transparently. In the next several trails, you will learn how to use those entity bean objects in applications.

     

    O/R&12510;&12483;&12500;&12531;&12464;&12399;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12398;&32972;&24460;&12395;&12354;&12427;&12461;&12540;&12392;&12394;&12427;&27010;&24565;&12391;&12377;&12290;EJB 3.0&12399;Java POJO&12434;&12487;&12540;&12479;&12505;&12540;&12473;&12486;&12540;&12502;&12523;&12408;&33258;&21205;&30340;&12363;&12388;&36879;&26126;&12395;&12510;&12483;&12500;&12531;&12464;&12375;&12414;&12377;&12290;&27425;&12363;&12425;&12398;&23567;&36947;&12391;&12399;&12289;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12391;&12398;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12458;&12502;&12472;&12455;&12463;&12488;&12398;&20351;&29992;&26041;&27861;&12434;&23398;&12403;&12414;&12377;&12290;

     

    c 2005 JBoss, Inc. All Rights Reserved