开发者

Need Design help with POJO's used for JPA services and REST Client's

I have a set of JPA POJO's that contain annotations required for mapping to my domain. I 开发者_运维问答also want to expose some REST services that will interact with those domain objects.

My current task is to create an android application to access these REST services. I am not able to use the domain object due to the JPA annotations they contain. The Dalvik compiler complains.

So I am looking for a strategy to be able to leverage these domain objects in a way that an Android project can also use those objects and not have to duplicate those POJO's.


Victor's suggestion to externalise the JPA mappings to XML rather than use annotations would surely work, but might be inconvenient if you're getting your JPA objects from tooling that only generates annotations.

I assume that you need on the client side Java classes that match the objects you will serialise in your REST services.

It is possible, but very tedious, to create DTO objects - POJOs exactly matching the JPA objects with suitable constructors from the JPA objects. This seems like an undue amount of effort.

It must be possible to write a source-code processor to strip the annotations from the Java. I don't think a simple regex scripting solution will work, I guess that truly parsing the source is necessary, so I hesitate to guess how much work this would be. However according to this question's answers the basic set of tools is available. I would start with this approach.


I could work out with following strategy.
This strategy works very well when you dont want fetch whole collection , or fetch with some addition criteria, , you may retrieve it(collection relation) with named query. use separate DAO for CRUD operation on JOIN table of many to many relation e.g. User can have many accounts and account can be shared by many users. create domain models/ DAO for all the three tables, use relation mapping for just retrieval and for DDL use individual properties.



    @Entity
    @Table(name="account" )
    public class Account {
     @Id (name="accountid")
     private Long accountId;

     @Column
     private String type;

     // removed @OneToMany as it causes issue while serializing to xml
     @Transient
     private Collection accountUsers;  

      //rest of the properties n geter setter goes here
    } 

    @Entity
    @Table(name="user")
    public class User {

     @Id(name="userid")
     private Long userId;

     @Column
     private String name;
      // by making transient jpa / hibernate does not initialize it with proxy.. so it remains null
    /* you can populate this property using named query whenever  required .*/
     @Transient
     private Collection userAccounts; 

   // rest of the properties n getter setter goes here

    }

    @Entity
    @Table(name="AccountUser")
    public class AccountUser {
    // whatever your strategy to implement primary key here , 
    @Id (name="accountuserid")
    private Long accountUserId; 

   /* please note this annotation , made insertable/updatable  false , relation defined just for fetching relation
   */
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "accountid", referencedColumnName = "accountid", insertable = false, updatable = false)
    private Account account;

// look at insertable / updatable properties its turned off
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "userid", referencedColumnName = "userid", insertable = false, updatable = false)
    private User user;

 // 
    @Column ( name = "userid"  )
    private Long userId;

    @Column ( name = "accountid"  )
    private Long accountId;

    @Column ( name="opendate") 
    private Date opendate;

    }

    /* use separate dao to save above beans */
    // somthing like this
    public class AccountDAOImpl extends GenericDAOImpl implements AccountDAO {
    }

    public class UserDAOImpl extends GenericDAOImpl implements UserDAO {
    }

    public class AccountUserDAOImpl extends GenericDAOImpl implements AccountUserDAO {
    }

I tried to explain if need any clarification kindly revert back. thanks

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜