Results 1 to 6 of 6
  1. #1
    einnhann is offline Member
    Join Date
    Dec 2009
    Posts
    6
    Rep Power
    0

    Default How to map 2 different objects together?

    Hi, I not sure posting this topic here is correct or not, sorry if I am wrong I don't know under which topic should I post. My current project got 2 modules ModuleA and ModuleB, inside ModuleA and ModuleB got a class call 'Student' (same class name same attributes, but for some purpose ModuleA must call ModuleB to do the actual task) . They communicate to each other through Web Services. Now I want ModuleA WS will call ModuleB proxy to do the actual task.

    In my ModuleA WS I got a method to create a record.

    public void createStudent(ModuleA.Student student){
    // Here will call ModuleB proxy to do the actual task which is create.

    moduleBFacade().createStudent( );
    }

    In my ModuleB Proxy

    public void createStudent(ModuleB.Student student){}

    So now the problem is I can not pass in the moduleA object into the createStudent method as it take in moduleB object.

    Any idea how to deal this problem? Please give me some suggestion.

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    maybe create a utility method to do a conversion between these two same classes in different packages.

    Java Code:
      public static ModuleB.Student moduleA2ModuleB(ModuleA.Student a) {
        ModuleB.Student b = new ModuleB.Student();
        b.setName(a.getName()); 
        // and so on, every setter in B invoked with corresponding getter in A
        return b;
      }
    
    
      public static ModuleA.Student moduleB2ModuleA(ModuleB.Student b) {
        ModuleA.Student a = new ModuleA.Student();
        a.setName(a.getName()); 
        // and so on, every setter in A invoked with corresponding getter in B
        return a;
      }
    and just call that when needing to transpose thigns.

    Another idea, if possible, to create a super class moduleC, that has the getter and setters, and are able to make both modules A,B, extend this class, so that the acutal contents of ModuleA.Student and ModuleB.Student might be just empty classes for the decoration, but the properties and methods really exist in C. but I can't see how that would work in all cases either.

  3. #3
    einnhann is offline Member
    Join Date
    Dec 2009
    Posts
    6
    Rep Power
    0

    Default

    Hi ya thanks for your solution, I have thought about this solution manually convert, but beside of this solution is there any efficient method, as I am invoking web services is it possible to use xml mapping to directly map the objectA to objectB

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,850
    Rep Power
    19

    Default

    You probably should have defined your objects first, possibly as XML (since it's a web service).

    Why can't Module A simply use the Student object from Module B? What's the point of having two of them?

    Otherwise, remapping as above is probably the only solution...it's one I've had to use where web service definitions have been mucked up, or ill defined.

  5. #5
    einnhann is offline Member
    Join Date
    Dec 2009
    Posts
    6
    Rep Power
    0

    Default

    Hi thanks, for some reason must go through this process can you elaborate more on the remapping ? i had researched few days already what technology should i use to remap the objects ?

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,850
    Rep Power
    19

    Default

    There is no framework, tool or technology. It's just as travishein has given above. Manually building either a StudentA from a StudentB, or vice versa.

    You have two different classes as far as Java is concerned, so you have to map them yourself.

Similar Threads

  1. Objects
    By Silverlining in forum New To Java
    Replies: 3
    Last Post: 11-11-2009, 02:36 PM
  2. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  3. Two Objects
    By losintikfos in forum New To Java
    Replies: 3
    Last Post: 11-14-2008, 07:04 PM
  4. 1 to 1 Objects
    By this.that in forum New To Java
    Replies: 4
    Last Post: 08-07-2008, 10:09 PM
  5. how many objects ?
    By kevinsong in forum Advanced Java
    Replies: 16
    Last Post: 07-16-2008, 05:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •