Version 2

    JBoss EJB 3.0 and Extensions

     

    &12456;&12531;&12486;&12451;&12486;&12451;Bean&12398;&12521;&12452;&12501;&12469;&12452;&12463;&12523; (Entity Bean Life Cycle)

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

     

    So far, you have learned how to manipulate the entity beans via the EntityManager. Using annotated life cycle callback methods in entity beans, you can further automate the bean behavior and reduce the amount of application code. For instance, in the investment calculator application, the container could automatically invoke a callback method to update the TimedRecord bean's timestamp before the bean is saved into the database. That eliminates the related setter method calls in the application.

     

    &12371;&12371;&12414;&12391;&12289;EntityManager&12434;&20351;&12387;&12383;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12398;&25805;&20316;&26041;&27861;&12395;&12388;&12356;&12390;&23398;&12435;&12391;&12365;&12414;&12375;&12383;&12290;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12391;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12395;&12424;&12387;&12390;&25351;&23450;&12373;&12428;&12427;&12521;&12452;&12501;&12469;&12452;&12463;&12523;&12467;&12540;&12523;&12496;&12483;&12463;&12513;&12477;&12483;&12489;&12434;&20351;&12358;&12392;&12289;Bean&12398;&25391;&12427;&33310;&12356;&12434;&12424;&12426;&33258;&21205;&21270;&12375;&12289;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12467;&12540;&12489;&12398;&37327;&12434;&12373;&12425;&12395;&28187;&12425;&12377;&12371;&12392;&12364;&21487;&33021;&12395;&12394;&12426;&12414;&12377;&12290;&20363;&12360;&12400;&12289;&25237;&36039;&35336;&31639;&27231;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12391;&12399;&12289;&12467;&12531;&12486;&12490;&12399;&33258;&21205;&30340;&12395;&12467;&12540;&12523;&12496;&12483;&12463;&12513;&12477;&12483;&12489;&12434;&21628;&12403;&20986;&12375;&12289;TimedRecord Bean&12398;&12479;&12452;&12512;&12473;&12479;&12531;&12503;&12434;&26356;&26032;&12375;&12289;&12381;&12398;&24460;&12289;&12381;&12398;Bean&12399;&12487;&12540;&12479;&12505;&12540;&12473;&12395;&20445;&23384;&12373;&12428;&12414;&12377;&12290;&12371;&12428;&12399;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12391;&12398;&38306;&36899;&12377;&12427;&12475;&12483;&12479;&12540;&12513;&12477;&12483;&12489;&21628;&12403;&20986;&12375;&12434;&21462;&12426;&38500;&12365;&12414;&12377;&12290;

     

    In this trail, you will learn how the entity bean life cycle callback methods work. They are very similar to the session bean callback methods discussed in an earlier trail.

     

    &12371;&12398;&23567;&36947;&12391;&12399;&12289;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12398;&12521;&12452;&12501;&12469;&12452;&12463;&12523;&12467;&12540;&12523;&12496;&12483;&12463;&12513;&12477;&12483;&12489;&12398;&21205;&20316;&12434;&23398;&12403;&12414;&12377;&12290;&12381;&12428;&12425;&12399;&21021;&26399;&12398;&23567;&36947;&12391;&35696;&35542;&12375;&12383;&12475;&12483;&12471;&12519;&12531;Bean&12398;&12467;&12540;&12523;&12496;&12483;&12463;&12513;&12477;&12483;&12489;&12392;&22823;&22793;&33391;&12367;&20284;&12390;&12356;&12414;&12377;&12290;

     

    &12521;&12452;&12501;&12469;&12452;&12463;&12523;&12513;&12477;&12483;&12489;&12398;&12450;&12494;&12486;&12540;&12471;&12519;&12531; (Life cycle method annotations)

     

    You can annotate any method in the entity bean class with the following annotations. The container would automatically invoke the annotated methods at the appropriate stages of the bean life cycle.

     

    &12456;&12531;&12486;&12451;&12486;&12451;Bean&12463;&12521;&12473;&12398;&20219;&24847;&12398;&12513;&12477;&12483;&12489;&12395;&27425;&12398;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12434;&25351;&23450;&12377;&12427;&12371;&12392;&12364;&12391;&12365;&12414;&12377;&12290;&12467;&12531;&12486;&12490;&12399;&12289;&12381;&12398;Bean&12398;&12521;&12452;&12501;&12469;&12452;&12463;&12523;&12398;&36969;&20999;&12394;&27573;&38542;&12391;&33258;&21205;&30340;&12395;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&20184;&12365;&12398;&12513;&12477;&12483;&12489;&12434;&21628;&12403;&20986;&12375;&12414;&12377;&12290;

     

    • @PrePersist: &12487;&12540;&12479;&12505;&12540;&12473;&12391;&12456;&12531;&12486;&12451;&12486;&12451;&12364;&20316;&25104;&12373;&12428;&12427;&30452;&21069;&12395;&12371;&12398;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12364;&20184;&12356;&12383;&12513;&12477;&12483;&12489;&12364;&21628;&12403;&20986;&12373;&12428;&12427;&12290;(The annotated method is invoked right before the entity is created in the database.)

    • @PostPersist: &12487;&12540;&12479;&12505;&12540;&12473;&12391;&12456;&12531;&12486;&12451;&12486;&12451;&12364;&20316;&25104;&12373;&12428;&12427;&30452;&24460;&12395;&12371;&12398;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12364;&20184;&12356;&12383;&12513;&12477;&12483;&12489;&12364;&21628;&12403;&20986;&12373;&12428;&12427;&12290;(The annotated method is invoked right after the entity is created in the database.)

    • @PreRemove: &12487;&12540;&12479;&12505;&12540;&12473;&12391;&12456;&12531;&12486;&12451;&12486;&12451;&12364;&21066;&38500;&12373;&12428;&12427;&30452;&21069;&12395;&12371;&12398;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12364;&20184;&12356;&12383;&12513;&12477;&12483;&12489;&12364;&21628;&12403;&20986;&12373;&12428;&12427;&12290;(The annotated method is invoked right before the entity is deleted in the database.)

    • @PostRemove: &12487;&12540;&12479;&12505;&12540;&12473;&12391;&12456;&12531;&12486;&12451;&12486;&12451;&12364;&21066;&38500;&12373;&12428;&12427;&30452;&24460;&12395;&12371;&12398;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12364;&20184;&12356;&12383;&12513;&12477;&12483;&12489;&12364;&21628;&12403;&20986;&12373;&12428;&12427;&12290;(The annotated method is invoked right after the entity is deleted in the database.)

    • @PreUpdate: &12487;&12540;&12479;&12505;&12540;&12473;&12364;&26356;&26032;&12373;&12428;&12427;&30452;&21069;&12395;&12371;&12398;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12364;&20184;&12356;&12383;&12513;&12477;&12483;&12489;&12364;&21628;&12403;&20986;&12373;&12428;&12427;&12290;(The annotated method is invoked right before the database is updated.)

    • @PostUpdate: &12487;&12540;&12479;&12505;&12540;&12473;&12364;&26356;&26032;&12373;&12428;&12427;&30452;&24460;&12395;&12371;&12398;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12364;&20184;&12356;&12383;&12513;&12477;&12483;&12489;&12364;&21628;&12403;&20986;&12373;&12428;&12427;&12290;(The annotated method is invoked immediately after the database has been updated.)

    • @PostLoad: &12487;&12540;&12479;&12505;&12540;&12473;&12363;&12425;&12487;&12540;&12479;&12364;&12525;&12540;&12489;&12373;&12428;&12390;&12289;&12456;&12531;&12486;&12451;&12486;&12451;&12395;&38306;&36899;&20184;&12369;&12428;&12383;&30452;&24460;&12395;&12371;&12398;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12364;&20184;&12356;&12383;&12513;&12477;&12483;&12489;&12364;&21628;&12403;&20986;&12373;&12428;&12427;&12290;(The annotated method is invoked right after data has been loaded from the database and associated with the entity.)

     

    Another life cycle method annotation for entity bean is the @Remove tag. It is not a callback method since the application, not the container, calls the @Remove method on the bean object to remove the bean instance from the EntityManager managed context. The bean instance becomes detached and you cannot use it again. The changes you made in this bean instance, if not committed, will be lost.

     

    &12456;&12531;&12486;&12451;&12486;&12451;Bean&12398;&12383;&12417;&12398;&12418;&12358;&19968;&12388;&12398;&12521;&12452;&12501;&12469;&12452;&12463;&12523;&12513;&12477;&12483;&12489;&29992;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12399;@Remove&12479;&12464;&12391;&12377;&12290;&12381;&12428;&12399;&12467;&12540;&12523;&12496;&12483;&12463;&12513;&12477;&12483;&12489;&12391;&12399;&12354;&12426;&12414;&12379;&12435;&12290;&12394;&12380;&12394;&12425;&12289;&65288;&12467;&12531;&12486;&12490;&12391;&12394;&12367;&65289;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12364;EntityManager&31649;&29702;&12467;&12531;&12486;&12461;&12473;&12488;&12363;&12425;&25351;&23450;&12373;&12428;&12383;Bean&12452;&12531;&12473;&12479;&12531;&12473;&12434;&21066;&38500;&12377;&12427;&12383;&12417;&12395;@Remove&12513;&12477;&12483;&12489;&12434;&21628;&12403;&20986;&12377;&12363;&12425;&12391;&12377;&12290;Bean&12452;&12531;&12473;&12479;&12531;&12473;&12399;&20998;&38626;&29366;&24907;(detached)&12395;&12394;&12426;&12289;&12381;&12428;&12434;&20877;&12403;&20351;&29992;&12377;&12427;&12371;&12392;&12399;&12391;&12365;&12414;&12379;&12435;&12290;&12371;&12398;Bean&12452;&12531;&12473;&12479;&12531;&12473;&12395;&23550;&12377;&12427;&22793;&26356;&12399;&12289;&12467;&12511;&12483;&12488;&12375;&12390;&12356;&12394;&12369;&12428;&12400;&22833;&12431;&12428;&12427;&12371;&12392;&12395;&12394;&12426;&12414;&12377;&12290;

     

    The changes made in those annotated life cycle methods will cascade to all entities to which this operation is cascaded.

     

    &12381;&12428;&12425;&12398;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&20184;&12365;&12521;&12452;&12501;&12469;&12452;&12463;&12523;&12513;&12477;&12483;&12489;&20869;&12391;&12398;&20462;&27491;&12399;&12289;&12371;&12398;&25805;&20316;&12364;&12459;&12473;&12465;&12540;&12489;&12373;&12428;&12383;&12377;&12409;&12390;&12398;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12395;&12459;&12473;&12465;&12540;&12489;&12375;&12414;&12377;&12290;

     

    &21029;&12463;&12521;&12473;&12408;&20998;&12369;&12425;&12428;&12383;&12467;&12540;&12523;&12496;&12483;&12463;&12513;&12477;&12483;&12489; (Separate callback methods into another class)

     

    Similar to the session beans, you can also place all the annotated life cycle callback methods in a separate class and specify that class as the "callback listener" class for the bean using the @EntityListener annotation.

     

    &12475;&12483;&12471;&12519;&12531;Bean&12392;&21516;&27096;&12395;&12289;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&20184;&12365;&12521;&12452;&12501;&12469;&12452;&12463;&12523;&12467;&12540;&12523;&12496;&12483;&12463;&12513;&12477;&12483;&12489;&12418;&21029;&12398;&12463;&12521;&12473;&12395;&37197;&32622;&12377;&12427;&12371;&12392;&12364;&21487;&33021;&12391;&12289;@EntityListener&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12434;&20351;&12387;&12390;&12381;&12398;&12463;&12521;&12473;&12434;&25351;&23450;&12373;&12428;&12383;Bean&12398;&12383;&12417;&12398;&12300;&12467;&12540;&12523;&12496;&12483;&12463;&12522;&12473;&12490;&12540;&12301;&12463;&12521;&12473;&12392;&12375;&12390;&25351;&23450;&12377;&12427;&12371;&12392;&12364;&12391;&12365;&12414;&12377;&12290;

     @Entity
     @EntityListener(TimedRecordEntityListener.class)
     public class TimedRecord extends Record {
        // ... ...
     }
    

    The callback methods in the TimedRecordEntityListener take a TimedRecord object as argument. The container passes in the TimedRecord object that causes this callback event at runtime.

     

    TimedRecordEntityListener &20869;&12398;&12467;&12540;&12523;&12496;&12483;&12463;&12513;&12477;&12483;&12489;&12399;TimedRecord&12458;&12502;&12472;&12455;&12463;&12488;&12434;&24341;&25968;&12392;&12375;&12390;&21462;&12426;&12414;&12377;&12290;&12467;&12531;&12486;&12490;&12399;&12289;&23455;&34892;&26178;&12395;&12371;&12398;&12467;&12540;&12523;&12496;&12483;&12463;&12452;&12505;&12531;&12488;&12398;&21407;&22240;&12392;&12394;&12427;TimedRecord&12458;&12502;&12472;&12455;&12463;&12488;&12434;&28193;&12375;&12414;&12377;&12290;

     public class TimedRecordEntityListener {
     
        @PrePersist
        public prepareTimestamp (TimedRecord rec) {
            // ... ...
        }
        
        @PreUpdate
        public updateTimestamp (TimedRecord rec) {
            // ... ...
        }
     
     }
    

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

     

    In trail, we discussed the life cycle callback methods for entity beans. As an exercise, you can refactor the investment calculator application to use callback methods to update the record timestamps automatically.

     

    &12371;&12398;&23567;&36947;&12391;&12399;&12289;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12398;&12521;&12452;&12501;&12469;&12452;&12463;&12523;&12467;&12540;&12523;&12496;&12483;&12463;&12513;&12477;&12483;&12489;&12395;&12388;&12356;&12390;&35696;&35542;&12375;&12414;&12375;&12383;&12290;&32244;&32722;&12392;&12375;&12390;&12289;&25237;&36039;&35336;&31639;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12434;&12522;&12501;&12449;&12463;&12479;&12522;&12531;&12464;&12375;&12390;&35352;&37682;&12398;&12479;&12452;&12512;&12473;&12479;&12531;&12503;&12434;&33258;&21205;&30340;&12395;&26356;&26032;&12377;&12427;&12424;&12358;&12395;&12467;&12540;&12523;&12496;&12483;&12463;&12513;&12477;&12483;&12489;&12434;&20351;&12358;&12371;&12392;&12418;&21487;&33021;&12391;&12377;&12290;

     

    In the next trail, we will discuss how to configure entity beans for databases other than the default embedded HSQLDB and the concept of multiple persistence context.

     

    &27425;&12398;&23567;&36947;&12391;&12399;&12289;&12487;&12501;&12457;&12523;&12488;&12398;&32068;&12415;&36796;&12415;HSQLDB&20197;&22806;&12398;&12487;&12540;&12479;&12505;&12540;&12473;&12398;&12383;&12417;&12398;&12456;&12531;&12486;&12451;&12486;&12451;Bean&12398;&27083;&25104;&26041;&27861;&12392;&35079;&25968;&12398;&27704;&32154;&12467;&12531;&12486;&12461;&12473;&12488;(persistence context)&12398;&27010;&24565;&12395;&12388;&12356;&12390;&35696;&35542;&12375;&12414;&12377;&12290;

     

     

    � 2005 JBoss, Inc. All Rights Reserved