2011-04-17 53 views
1

我有一組JPA POJO包含映射到我的域所需的註釋。我也想公開一些將與這些域對象交互的REST服務。需要POJO用於​​JPA服務和REST客戶端的設計幫助

我目前的任務是創建一個Android應用程序來訪問這些REST服務。由於其包含的JPA註釋,我無法使用域對象。 Dalvik編譯器抱怨。

因此,我正在尋找一種策略,以便能夠以一種Android項目也可以使用這些對象的方式利用這些域對象,而不必重複那些POJO。

+1

如果希望重用源代碼,將映射移動到XML是合適的。 – 2011-04-17 18:14:11

+0

可能與http://stackoverflow.com/questions/7086433/jpa-best-practices/15656296有關 – java4africa 2013-03-27 10:27:32

回答

0

Victor建議將JPA映射外部化爲XML而不是使用註釋肯定會起作用,但如果您從僅生成註釋的工具中獲得JPA對象,可能會很不方便。

我假設您需要在客戶端配合您將在REST服務中序列化的對象的Java類。

創建DTO對象 - POJO與JPA對象與JPA對象中合適的構造函數完全匹配是可能的,但非常繁瑣。這似乎是一個不必要的努力。

必須編寫一個源代碼處理器來去除Java中的註釋。我不認爲一個簡單的正則表達式腳本解決方案會起作用,我想真正解析源代碼是必要的,所以我毫不猶豫地猜測這將是多少工作。但根據this question's answers,基本工具組是可用的。我會從這個方法開始。

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

我試圖解釋是否需要任何澄清和善地回覆。謝謝

相關問題