5. Ŭ¶óÀ̾ðÆ®ÀÇ °üÁ¡¿¡¼­ º»  entity Beans

    business methods ´Â enterprise Bean ÀÚ½ÅÀÌ Á¦°øÇØ¾ß ÇÑ´Ù.

    5.1 Overview

        ¿£Æ¼Æ¼ºóÀº Áö¼Ó¼º ÀúÀå¸Åü(µ¥ÀÌŸº£À̽º, ÆÄÀϽýºÅÛ...)¿¡ ÀúÀåµÇ´Â Áö¼Ó¼º °´Ã¼ÀÌ´Ù.
        Ŭ¶óÀÌ¾ðÆ®´Â ¿£Æ¼Æ¼ºóÀÇ È¨ÀÎÅÍÆäÀ̽º¸¦ ÅëÇØ Á¢±ÙÇÑ´Ù.
        ¸®¸ðÆ® ÀÎÅÍÆäÀ̽º¸¦ ±¸ÇöÇÏ´Â °´Ã¼¸¦ EJB object ¶ó°í ÇÑ´Ù.
        EJB object ´Â ÄÁÅÍ¿¡³Ê ³»¿¡¼­ »ýȰÇϸç Ŭ¶óÀÌ¾ðÆ®¿¡ Åõ¸íÇÏ´Ù.
        ÄÁÅ×À̳ʴ EJB object ¸¦ À§ÇØ  security, concurrency, transactions, persistence...
        µîÀÇ ¼­ºñ½º¸¦ Á¦°øÇÑ´Ù.
        ÄÁÅ×ÀÌ³Ê ¶ÇÇÑ Å¬¶óÀÌ¾ðÆ®¿¡ Åõ¸íÇϸç Ŭ¶óÀÌ¾ðÆ®°¡ ÄÁÅ×À̳ʸ¦ ´Ù·ê¼ö ÀÖ´Â
        API Á¸ÀçÇÏÁö ¾Ê´Â´Ù.

        Multiple clients ´Â ÇÑ ¿£Æ¼Æ¼ °´Ã¼¿¡ µ¿½Ã¿¡ Á¢±ÙÇÒ ¼ö ÀÖ´Ù.

        ÀϹÝÀûÀ¸·Î °¢°¢ÀÇ ¿£Æ¼Æ¼ °´Ã¼´Â ½Äº°ÀÚ¸¦ °¡Áö¸ç ÄÁÅ×À̳ʰ¡ Àç½ÃÀÛÇϰųª
        ÀÛµ¿À» ¸ØÃ߾ °è¼Ó »ýÁ¸ÇÑ´Ù.
        °´Ã¼ ½Äº°ÀÚ´Â ÄÁÅ×À̳ʿ¡ ÀÇÇØ ±¸ÇöµÈ´Ù.

        enterprise Bean's home interface Ŭ¶óÀÌ¾ðÆ®°¡ EJB Object ¸¦ create, look up,
        remove Çϴ°ÍÀ» Çã¶ôÇÑ´Ù.
        Ŭ¶óÀÌ¾ðÆ®´Â JNDI ¸¦ ÅëÇØ enterprise Bean's home interface ¸¦ ãÀ»¼ö ÀÖ´Ù.
        ÄÁÅ×À̳ʴ JNDI À̸§°ø°£¿¡¼­ enterprise Bean's home interface °¡
        »ç¿ë°¡´ÉÇϵµ·Ï ÇÒ Ã¥ÀÓÀÌ ÀÖ´Ù.

    5.2 EJB container

        ÄÁÅ×À̳ʴ enterprise Bean object °¡ »ýÁ¸ÇÏ´Â °ø°£ÀÌ´Ù.

        5.2.1 Locating enterprise Bean's home interface

            // Account enterprise Bean À» À§ÇÑ È¨ÀÎÅÍÆäÀ̽º¸¦ ã´Â¹æ¹ý

            Context initialContext = new InitialContext();
            AccountHome accountHome = javax.rmi.PortableRemoteObject.narrow(
           initialContext.lookup("applications/bank/accounts", AccountHome.class);

        5.2.2 What a container provides
 
 
 
 


 
 
 
 
 
 
 
 

    5.3 Enterprise Bean's home interface

        ÄÁÅ×À̳ʴ °¢°¢ÀÇ enterprise Bean ÀÇ È¨ÀÎÅÍÆäÀ̽º ±¸ÇöÀ» Á¦°øÇÑ´Ù.
        ±×¸®°í ¸ðµç Ŭ¶óÀÌ¾ðÆ®°¡ JNDI ¸¦ ÅëÇØ ȨÀÎÅÍÆäÀ̽ºÁ¦ Á¢±ÙÇÒ¼ö ÀÖµµ·Ï ÇÑ´Ù.
        ȨÀÎÅÍÆäÀ̽ºÀÇ ±¸ÇöŬ·¡½º¸¦ EJB home À̶ó°í ÇÑ´Ù.

        ¿£Æ¼Æ¼ºóÀÇ È¨ÀÎÅÍÆäÀ̽º´Â ¾Æ·¡»çÇ×À» Ŭ¶óÀÌ¾ðÆ®¿¡°Ô Çã¶ôÇÑ´Ù.

            -  Create new EJB objects.

            - Look up existing EJB objects.

            - Remove an EJB object.

            - Get the javax.ejb.EJBMetaData interface for the enterprise Bean.

        Ȩ ÀÎÅÍÆäÀ̽º´Â javax.ejb.EJBHome interface ¸¦ »ó¼ÓÇØ¾ß ÇÑ´Ù.

        5.3.1 create() method

            Entity Beans ÀÇ Home interface ´Â 0°³ ÀÌ»óÀÇ create() ¸Þ¼Òµå¸¦ Á¤ÀÇÇÒ ¼ö ÀÖ´Ù.
            (ÇÑ EJB °´Ã¼¸¦ »ý¼ºÇϱâÀ§ÇÑ °¢°¢ÀÇ ¹æ¹ýÀ» »ç¿ëÇÒ¼ö ÀÖ´Ù´Â °ÍÀÌ´Ù.)

            ÀϹÝÀûÀ¸·Î create() ¸Þ¼ÒµåÀÇ Àü´ÞÀÎÀÚ´Â »ý¼ºµÈ °´Ã¼¸¦ »óŸ¦ ÃʱâÈ­ Çϱâ À§ÇØ
            »ç¿ëµÈ´Ù.

            create() ¸Þ¼ÒµåÀÇ ¸®ÅϰªÀº enterprise Bean's remote interface ÀÌ´Ù.

            ¶ÇÇÑ create() ¸Þ¼Òµå´Â java.rmi.RemoteException, javax.ejb.CreateException À»
            throws ÇØ¾ßÇϸç Ãß°¡ÀûÀ¸·Î ¾ÖÇø®ÄÉÀÌ¼Ç ·¹º§ÀÇ ¿¹¿Ü¸¦ ¹ß»ý½Ãų¼ö ÀÖ´Ù.

            2°³ÀÇ create() ¸Þ¼Òµå¸¦ ±¸ÇöÇϴ Ȩ ÀÎÅÍÆäÀ̽º ¿¹Á¦..

                public interface AccountHome extends javax.ejb.EJBHome {

                   public Account create(String firstName, String lastName, double initialBalance)
                                                                throws RemoteException, CreateException;

                   public Account create(String accountNumber, double initialBalance)
                          throws RemoteException, CreateException, LowInitialBalanceException;

                   ...
                }

            Ŭ¶óÀÌ¾ðÆ®°¡ »õ·Î¿î EJB °´Ã¼¸¦ »ý¼ºÇÏ´Â ¹æ¹ý.

                AccountHome accountHome = ...;
                Account account = accountHome.create("john", "smith", 500.00);
 
 

        5.3.2 find~() methods

            Entity Beans ÀÇ Home interface ´Â ÇϳªÀÌ»óÀÇ find~() ¸Þ¼Òµå¸¦ Á¤ÀÇÇÒ ¼ö ÀÖ´Ù.
            (ÇÑ EJB °´Ã¼¸¦ ã±âÀ§ÇØ ¶Ç´Â Ưº°ÇÑ Å¸ÀÔÀÇ EJB °´Ã¼¸¦ ¼öÁýÇϱâ À§ÇØ
            °¢°¢ÀÇ ¹æ¹ýÀ» »ç¿ëÇÒ¼ö ÀÖ´Ù´Â °ÍÀÌ´Ù.)
           (¸ðµç ¿£Æ¼Æ¼ºó¿¡ À־ findByPrimaryKey(primaryKey) ¸Þ¼Òµå´Â ¹Ýµå½Ã Á¤ÀÇÇØ¾ßÇÔ)

            ¸Þ¼Òµå ¸íĪÀº findLargeAccounts(...)¿Í °°ÀÌ find ·Î ½ÃÀÛÇØ¾ß ÇÑ´Ù.

            ¸®ÅÏŸÀÔÀº enterprise Bean's remote interface À̰ųª EJB °´Ã¼ÀÇ ¸ðÀÓÀ» ¹¦»çÇϴ ŸÀÔÀÌ´Ù.

            ¸ðµç find ¸Þ¼Òµå´Â java.rmi.RemoteException À» ¹ß»ýÇØ¾ß¸¸ Çϰí, ÀϹÝÀûÀ¸·Î
            javax.ejb.FinderException À» ¹ß»ýÇÒ¼ö ÀÖ´Ù.

            Entity Bean ÀÇ È¨ ÀÎÅÍÆäÀ̽º´Â Primary Key ¸¦ °¡Áö´Â Entity Bean ÀÇ ¸®¸ðÆ® ÀÎÅÍÆäÀ̽º¸¦
            ¾ò±âÀ§ÇØ findByPrimaryKey(primaryKey) ¸Þ¼Òµå¸¦ Æ÷ÇÔÇÑ´Ù.

            public interface AccountHome extends javax.ejb.EJBHome {
               ...
               public Account findByPrimaryKey(String AccountNumber) throws RemoteException,
                                                                                                                FinderException;
            }

            AccountHome = ...;
            Account account = accountHome.findByPrimaryKey("100-3450-3333");

        5.3.3 remove() methods

            javax.ejb.EJBHome ÀÎÅÍÆäÀ̽º´Â Ŭ¶óÀÌ¾ðÆ®°¡ EJB °´Ã¼¸¦ Á¦°ÅÇÒ¼ö ÀÖ°Ô ÇϱâÀ§ÇØ
            remove() ¸Þ¼Òµå¸¦ Á¤ÀÇÇÑ´Ù.

            public interface EJBHome extends Remote {

                void remove(Handle handle) throws RemoteException, RemoveException;

                void remove(Object primaryKey) throws RemoteException, RemoveException;
           }

    5.4 Entity EJB object life cycle
 
 
 
 


 
 
 
 
 
 
 
 

        EJB object ´Â »ý¼º(create())µÇ±â Àü±îÁö Á¸ÀçÇÏÁö ¾ÊÀ¸¸ç ½Äº°ÀÚµµ °¡ÁöÁö ¾Ê´Â´Ù.
        Ŭ¶óÀÌ¾ðÆ®´Â ÄÁÅ×À̳ʿ¡ ÀÇÇØ ½ÇÇàµÇ´Â enterprise Bean's home interface  ¸¦ ÀÌ¿ëÇØ
        EJB °´Ã¼¸¦ »ý¼ºÇÑ´Ù.

        entity EJB ´Â create() ¸Þ¼Òµå¸¦ ÀÌ¿ëÇÏ´Â°Í ¿Ü¿¡ ´Ù¸¥ ¸ÞÄ¿´ÏÁò
        (µ¥ÀÌŸº£À̽º ·¹Äڵ忡 Á÷Á¢ »ðÀÔÇÏ´Â ¹æ¹ý µî)À» »ç¿ëÇÏ¿© °´Ã¼¸¦ »ý¼ºÇÒ ¼ö ÀÖ´Ù.
        À̰æ¿ì¿¡ Ŭ¶óÀÌ¾ðÆ®´Â find~() method ¸¸À» ÀÌ¿ëÇÒ ¼ö ÀÖ´Ù.

        ¶ÇÇÑ remove() ¸¦ »ç¿ëÇÏÁö ¾Ê°í µ¥ÀÌŸº£À̽ºÀÇ ·¹Äڵ带 Á÷Á¢ »èÁ¦ÇÔÀ¸·Î½á
        Á¦°ÅµÉ¼öµµ ÀÖ´Ù.

        Ŭ¶óÀÌ¾ðÆ®°¡ ÀÌ¹Ì Á¸ÀçÇÏ´Â EJB °´Ã¼ÀÇ ÂüÁ¶¸¦ ȹµæÇÏ´Â 3°¡Áö ¹æ¹ý

            - Receive a reference as a parameter in a method call (input parameter or result).

            - Look up the EJB object using a finder method of the enterprise Bean's home interface.

            - Obtain the reference from a Bean's handle

        °´Ã¼ÂüÁ¶¸¦ ¾òÀºÈÄ Å¬¶óÀÌ¾ðÆ®°¡ ÇÒ¼ö ÀÖ´Â ÀÏ.

            - Invoke business methods on the object through the EJB object's remote interface.

            - Obtain a reference to the enterprise Bean's home interface.

            - Pass the reference as a parameter or return value.

            - Obtain the EJB object's primary key.

            - Obtain the EJB object's handle.

            - Remove the EJB object.

        Á¸ÀçÇÏÁö ¾Ê´Â °´Ã¼¿¡ ´ëÇÑ ÂüÁ¶¸¦ °¡Áö°í È£ÃâÇÒ °æ¿ì java.rmi.NoSuchObjectException À» ¹ß»ýÇÑ´Ù.

        ¸ðµç ¿£Æ¼Æ¼ºóÀº Áö¼Ó¼º °´Ã¼ÀÌ´Ù.µû¶ó¼­ »ý¸íÁÖ±â´Â ÀÚ¹Ù°¡»ó¸Ó½Å, ÄÁÅ×À̳ʵéÀÇ »óÅ¿¡
        ¿µÇâ¹ÞÁö¾Ê´Â´Ù.

        Multiple clients ´Â °°Àº EJB °´Ã¼¿¡ µ¿½Ã¿¡ Á¢±ÙÇÒ ¼ö ÀÖÀ¸¸ç Æ®·£Àè¼ÇÀº °¢°¢À¸·ÎºÎÅÍ
        Ŭ¶óÀ̾ðÆ®ÀÇ ¿äûÀ» ºÐ¸®Çϱâ À§ÇØ »ç¿ëµÈ´Ù.

    5.5 Primary key and object identity

        ¸ðµç ¿£Æ¼Æ¼°´Ã¼¸¦ ±×°ÍÀÇ È¨ ³»¿¡¼­ À¯ÀÏÇÑ ½Äº°ÀÚ¸¦ °¡Áø´Ù.
        ÄÁÅ×À̳ʳ»ÀÇ °´Ã¼ ½Äº°ÀÚ´Â EJB object's home ±×¸®°í primary key¿¡ ÀÇÇØ °áÁ¤µÈ´Ù.
        ¸¸¾à µÎ °´Ã¼°¡ °°Àº home °ú primary key ¸¦ °¡Áø´Ù¸é µÎ °´Ã¼´Â µ¿ÀÏÇÏ´Ù.
        ±×¸®°í primary key object Á÷·ÄÈ­ °¡´ÉÇØ¾ßÇϸç primary key class´Â
        enterprise Bean class ¿¡ ƯȭµÈ´Ù.
        Áï, °¢°¢ÀÇ enterprise Bean class ´Â primary key ¸¦ À§ÇØ ´Ù¸¥ Ŭ·¡½º¸¦ °¡Áú¼ö ÀÖ´Ù.

        Ŭ¶óÀÌ¾ðÆ®´Â getPrimaryKey() ¸Þ¼Òµå¸¦ È£ÃâÇÏ¿© EJB °´Ã¼ÀÇ primary key ¸¦
        ¾òÀ»¼ö ÀÖ´Ù.
 

        Ŭ¶óÀ̾ðÆ®Ãø¿¡¼­ µÎ EJB °´Ã¼ÂüÁ¶°¡ °°Àº Entity ¸¦ ÂüÁ¶ÇÏ´ÂÁö ¾Ë¼ö ÀÖ´Â ¹æ¹ý

            - isIdentical(otherObject) ¸Þ¼Òµå (Àü´ÞÀÎÀÚ·Î ´Ù¸¥ ÂüÁ¶¸¦ Àü´ÞÇÑ´Ù.)(Ãßõ)

                Account acc1 = ...;
                Account acc2 = ...;

                if (acc1.isIdentical(acc2)) {
                    //acc1 and acc2 are the same EJB objects
                } else {
                    //acc2 and acc2 are different EJB object
                }

            - entity objects's primary keys ¸¦ ȹµæÇÑ ´ÙÀ½  java.lang.Object.equals() ¸Þ¼Òµå¸¦
               ÀÌ¿ëÇØ key ¸¦ ºñ±³ÇÑ´Ù.
               (´Ü, µÎ EJB reference °¡ °°Àº Home¿¡ ÀÖ´Â EJB °´Ã¼¸¦ ÂüÁ¶ÇÒ¶§¸¸ °¡´ÉÇÏ´Ù)
               (ÇÏÁö¸¸, °°Àº °´Ã¼¸¦ ¹¦»çÇÏ´Â µÎ °´Ã¼ ÂüÁ¶ »ó¿¡¼­ java.lang.Object.hashCode()
                ¸Þ¼Òµå´Â Á¤È®ÇÑ °á°ú¸¦ º¸ÁõÇÒ¼ö ¾ø´Ù.)

        entity EJB object ÀÇ primary keys ¸¦ ¾Ë°íÀִ Ŭ¶óÀÌ¾ðÆ®´Â ÄÁÅ×À̳ʿ¡ ÀÇÇØ ½ÇÇàµÇ´Â
        Ȩ ÀÎÅÍÆäÀ̽ºÀÇ findByPrimaryKey(key) ¸¦ È£ÃâÇØ¼­ °´Ã¼ÀÇ ÂüÁ¶¸¦ ȹµæÇÒ ¼ö ÀÖ´Ù.

    5.6. Enterprise Bean's remote interface

        Ŭ¶óÀÌ¾ðÆ®´Â ¸®¸ðÆ® ÀÎÅÍÆäÀ̽º¸¦ ÅëÇØ Entity Bean ¿¡ Á¢±ÙÇÒ¼ö ÀÖ°í ¸®¸ðÆ® ÀÎÅÍÆäÀ̽º´Â
         business methods ¸¦ Á¤ÀÇÇÑ´Ù.

        ¸®¸ðÆ® ÀÎÅÍÆäÀ̽ºÀÇ Á¤ÀÇ ¿¹.

            public interface Account extends javax.ejb.EJBObject {

                void debit(double amount) throws java.rmi.RemoteException,
                                                                    InsufficientBalanceException;

                void credit(double amount) throws java.rmi.RemoteException;

                double getBalance() throws java.rmi.RemoteException;
          }

        javax.ejb.EJBObject interface ´Â Ŭ¶óÀÌ¾ðÆ®°¡ EJB °´Ã¼ ÂüÁ¶»ó¿¡¼­ ´ÙÀ½°ú °°Àº ÀÏÀ» ÇÒ¼ö
        ÀÖµµ·Ï  Á¤ÀÇÇÑ´Ù.

            - Obtain the home interface for the EJB class. (getEJBHome())

            - Remove the EJB object. (remove())

            - Obtain the EJB object's handle. (getHandle())

            - Obtain the EJB object's primary key. (getPrimaryKey())

        javax.ejb.EJBObject interface ¿¡ Á¤ÀÇµÈ ¸Þ¼ÒµåÀÇ ½ÇÇàÀº ÄÁÅ×À̳ʿ¡ ÀÇÇØ Á¦°øµÇ°í
        ºñÁî´Ï½º ¸Þ¼ÒµåÀÇ ½ÇÇàÀº  enterprise Bean class ¿¡ À§ÀӵȴÙ.

        5.7 Enterprise Bean's handle

            handle Àº EJB °´Ã¼¸¦ ½Äº°Çϱâ À§ÇÑ °´Ã¼À̸ç, EJB °´Ã¼ÀÇ ÂüÁ¶¸¦ °¡Áö´Â Ŭ¶óÀÌ¾ðÆ®°¡
            ÂüÁ¶»ó¿¡¼­ getHandle() ¸Þ¼Òµå¸¦ È£ÃâÇÔÀ¸·Î½á °´Ã¼ÀÇ ÇÚµéÀ» ¾òÀ»¼ö ÀÖ´Ù.

            handle Àº ÀϹÝÀûÀ¸·Î ÄÁÅ×À̳ʿ¡ ÀÇÇØ Á¦°øµÇÁö¸¸ Ŭ·¡½º¸¦ Á÷Á¢ ±¸ÇöÇÒ °æ¿ì´Â ¹Ýµå½Ã
            Á÷·ÄÈ­ °¡´ÉÇϱâÀ§ÇØ java.io.Serializable interface ¸¦ ±¸ÇöÇØ¾ß¸¸ ÇÑ´Ù.
           ±×¸®°í Ŭ¶óÀÌ¾ðÆ®´Â ÀÌÈÄ¿¡ Á÷·ÄÈ­µÈ ÇÚµéÀ» »ç¿ëÇÒ¼ö°¡ ÀÖ´Ù.

              // A client obtains a handle of an account EJB object and
              // stores the handle in stable storage.

             ObjectOutputStream stream = ...;
             Account account = ...;
             Handle handle = account.getHandle();
             stream.writeObject(handle);

             // A client can read the handle from stable storage, and use the
             // handle to ressurect an object reference to the
             // account EJB object.

             ObjectInputStream stream = ...;
             Handle handle = (Handle) stream.readObject(handle);
             Account account = (Account) handle.getEJBObject();
             account.debit(100.00);