请选择 进入手机版 | 继续访问电脑版

技术控

    今日:0| 主题:61300
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Migrating EJB2 Entity Beans to EJB3 and JPA

[复制链接]
零食戒了 发表于 2016-10-6 07:58:17
275 5
Scenario: you have to migrate all of the EJB2 Entity Beans from a legacy project to EJB3 Entity Beans with JPA. Hibernate will be your JPA implementation and Spring Framework cannot be used. No other layer that the EJB2 Entity Beans layer will be migrated.
   The application also contains an EJB2 Session Beans layer. This one will not be replaced, but updated to work with the new EJB3 Entity Beans. (Note: this article will not explain the J2EE architecture. If you want to find more about that, please check here . Please take the patterns presented in the link with a grain of salt. Some of them are obsolete, but some still make sense.)
   Assumption: The 10K feet view of your legacy back end layers looks like below:
   

Migrating EJB2 Entity Beans to EJB3 and JPA

Migrating EJB2 Entity Beans to EJB3 and JPA-1-技术控-available,presented,Business,article,please

   EmployeeDelegate: a Business Delegate pattern implementation and it is the only class that exposes the available functionality to a client. More about the Business Delegate pattern here .
   initializeEmployeeSessionFacadeHome: a function that will locate and create the  EmployeeSessionFacadeHome and return an EmployeeSessionFacade interface.
   EmployeeSessionFacadeBean: the session facade that will actually contain the functionality exposed by EmployeeSessionFacade .
   findEmployeeHome: is the function responsible for finding the EJB2 Entity Bean for working with employee data. This function will return EmployeeHome which is an interface.
   EmployeeBean: is actually the class that contains the code to save and remove and work with employee data.
  Now, the standard classes and interfaces of an Employee EJB2 CMP entity bean are shown below:
   

Migrating EJB2 Entity Beans to EJB3 and JPA

Migrating EJB2 Entity Beans to EJB3 and JPA-2-技术控-available,presented,Business,article,please

   EmployeeHome: used to locate the bean and define the create and remove operations.
   EmployeeBean: the entity bean that is actually managed by the container and implements the operations.
   Employee: is the local interface of the bean.
   EmployeeDTO: a data transfer object used to transfer data between various layers.
  (Note: I will not detail here what an EJB 2 Entity Bean is or how it is configured.)
   If you want to find out more about this subject, please check the official documentation .
   Migrating Employee Bean From EJB 2.1 to EJB 3

  This is assuming that you want to do this while keeping EmployeeSessionFacade as an EJB 2 session facade bean.
  When migrating the new EJB 3 entity beans, I will try to expose the same functions as the old EJB 2 Entity Beans were exposing in order to minimize the changes in the other layers that are using that beans.
  The specifications for EJB 3 Entity Beans also mention the Home Local and Remote interfaces.
   

Migrating EJB2 Entity Beans to EJB3 and JPA

Migrating EJB2 Entity Beans to EJB3 and JPA-3-技术控-available,presented,Business,article,please

   The EJB3Entity class, which will be the base class of all EJB3 Entity Beans:
  1. public interface EJB3Entity<ID> {
  2.   ID getId();
  3.   void setId(ID id);
  4. }
复制代码
  and the Employee EJB3 :
  1. @Entity
  2. @Cacheable
  3. @Table(name = "employee")
  4. public class Employee implements EJB3Entity<Integer>, Serializable {
  5. @Id
  6. @GeneratedValue(strategy = GenerationType.IDENTITY)
  7.     @Column(name = "id")
  8.     private Integer id;
  9.     @Column(name = "name", length = 100)
  10.     private String name;
  11.     @OneToOne(mappedBy="department_id")
  12.     private Department department;
  13. ...............
  14. }
复制代码
  The EmployeeHome interface looks like below:
   

Migrating EJB2 Entity Beans to EJB3 and JPA

Migrating EJB2 Entity Beans to EJB3 and JPA-4-技术控-available,presented,Business,article,please

   where BaseHome is exposing the same functions as EmployeHomeEJB2 was exposing (here, the name of the functions do not contain the prefix ejb ).
   The reason for the BaseHome to expose the same function is that if we keep the same function names (as per the EJB2 version), then the EmployeeSessionFacadeBean will suffer minor changes during this migration.
   The Remote EmployeeHome interface looks like this:
  1. @Remote
  2. public interface EmployeeHome extends BaseHome<Employee, Integer, EmployeeDTO> {
  3. List<Employee> findAll() throws Exception;
  4. //more functions here
  5. }
复制代码
  Please see below the class diagram for the Employee EJB3 Entity Bean :
   

Migrating EJB2 Entity Beans to EJB3 and JPA

Migrating EJB2 Entity Beans to EJB3 and JPA-5-技术控-available,presented,Business,article,please

   and below how JPA is used for the findAll function:
  1. public List<Employee> findAll() throws Exception {
  2.   String sqlQuery = "select emp from Employee emp order by emp.name ";
  3.   Query query = entityManager.createQuery(sqlQuery);
  4.   List<Employee> list = query.getResultList();
  5.   return list;
  6. }
复制代码
  And now, let’s change EmployeeSessionFacadeBean (which is still an EJB 2 session facade) to use the new EJB3 Entity Bean:
  1. public class DmsAnnotationCommtTypeSessionFacadeBean implements SessionBean {
  2.   /**
  3.     * this is an example that works with Wildfly 10
  4.     */
  5.   private void findEmployeeHome(...) throws EJBException {
  6.       if (employeeHome == null) {
  7.           String entityBeanJndi = "java:jboss/exported/EMPLOYEE-EAR/EmployeeEJBs/EmployeeSessionFacade!com.myawesomeapp.employee.session.EmployeeSessionFacadeHome";
  8.           Context ctx = getContext();
  9.           Object ejb3EntityHome = ctx.lookup(entityBeanJndi);
  10.           employeeHome = (EmployeeHome)ejb3EntityHome;
  11.         }
  12.         return employeeHome;
  13.    }
  14.    public List<EmployeeDTO> findAll() throws EJBException, FinderException {
  15.   List<EmployeeDTO> dtoList = new ArrayList<EmployeeDTO>();
  16.       try {
  17.           List<Employee> employeeList= employeeHome.findAll();
  18.           List<EmployeeDTO> dtoList = convertToDTO(employeeList);
  19.       } catch (Exception e) {
  20.           throw new EJBException(e);
  21.       }
  22.       return dtoList;
  23.    }
  24.   .....
  25. }
复制代码
...where:
   findEmployeeHome: it is used to locate the home interface of the new EJB3 entity bean. In this case, the entityBeanJndi string is specific to Wildfly 10 .
   If you want to find out more about Wildfly 10 and EJBs, please check here .
   The findAll method is a good example of how simple it is now to work with the new EJB3 entity bean.
   Conclusion: This article explained how with properly designed interfaces, it is possible to limit the amount of work needed to migrate from the old EJB 2 Entity Beans to the new EJB 3 Entity Beans and JPA .
  (Note: In a real project the speed of migration has been 10 to 15 entity beans migrated per day using this design. Of course in real life the user cases are more complicated, for instance there are also complex search queries that require this architecture to be extended in order for that use cases to be also addressed.)
垂婷丞松 发表于 2016-10-19 02:15:35
拍脑袋决策,拍胸脯保证,拍屁股走人.
回复 支持 反对

使用道具 举报

雷天航 发表于 2016-10-19 04:40:48
1v1飘过
回复 支持 反对

使用道具 举报

山柏 发表于 2016-11-8 15:35:58
零食戒了是一个典型的文艺青年啊!
回复 支持 反对

使用道具 举报

天天万新奇 发表于 2016-11-17 19:02:51
我就是路过,拿积分走人!!
回复 支持 反对

使用道具 举报

aghag 发表于 2016-11-20 19:13:35
为什么我躺着也中枪
回复 支持 反对

使用道具 举报

我要投稿

回页顶回复上一篇下一篇回列表
手机版/c.CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表