1

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.

4

2 回答 2

0

我可以制定以下策略。
当您不想获取整个集合或使用某些附加条件进行获取时,此策略非常有效,您可以使用命名查询检索它(集合关系)。对多对多关系的 JOIN 表使用单独的 DAO 进行 CRUD 操作,例如,用户可以有多个帐户,并且帐户可以由多个用户共享。为所有三个表创建域模型/ DAO,使用关系映射进行检索,DDL 使用单个属性。



    @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 {
    }

我试图解释是否需要任何澄清,请回复。谢谢

于 2011-08-08T07:30:25.743 回答
0

Victor 将 JPA 映射外部化为 XML 而不是使用注释的建议肯定会奏效,但如果您从仅生成注释的工具中获取 JPA 对象,则可能会带来不便。

我假设您在客户端需要与您将在 REST 服务中序列化的对象相匹配的 Java 类。

创建 DTO 对象是可能的,但非常乏味 - POJO 与 JPA 对象与来自 JPA 对象的合适构造函数完全匹配。这似乎是一种过度的努力。

必须可以编写源代码处理器以从 Java 中剥离注释。我不认为一个简单的正则表达式脚本解决方案会起作用,我想真正解析源代码是必要的,所以我犹豫猜测这将是多少工作。然而,根据这个问题的答案,基本的工具集是可用的。我将从这种方法开始。

于 2011-08-08T07:50:39.933 回答