从两个表JDO DataNucleus创建新对象

时间:2013-04-11 19:34:28

标签: java persistence jdo dto datanucleus

我试图通过使用DataNucleus持久化两个类来创建DTO对象。

我想创建的DTO:

@PersistenceAware
DtoObject{

   Protected String Id;  //populated by Order class
   Protected String status; //populated by Order class
   Protected String phoneNumber; //populated by Customer class
   Protected String address;  //populated by Customer class
}
The Objects:

 @PersistenceCapable
 @FetchGroup(name="dto", members = {@Persistent(name = "Id"), 
 @Persistent(name="status")})
 public Class Order{
   @PrimaryKey
   @Persistent
   private String Id;
   @Persistent
   private String status;
   @Persistent
   private Customer customer;
}

@PersistenceCapable
@FechGroup(name="dto", members = { @Persistent(name = "phoneNumber"),
@Peristent(name="address") })
public Class Customer{

   @PrimaryKey
   @Persistent
   private String Id;
   @Persistent
   private String phoneNumber;
   @Persistent
   private string Address;
}

JDODL:

Query q = pm.newQuery(Order.class);
            pm.getFetchPlan().setGroup("dto");
            q.setUnique(true);
            q.setFilter("Id == id");
            q.declareParameters("String id");
            q.setResultClass(DtoObject.class);
            DtoObject dto = (DtoObject)q.execute(id);

我可以使用映射到Order.class的属性填充dto对象,但无法从Customer.class获取属性。 Data Nucleus连接表并从每个表中选择适当的列,但保留phoneNumber = null和address = null;

有关如何使用一个查询进行此操作的指示将不胜感激。

1 个答案:

答案 0 :(得分:0)

Query q = pm.newQuery("SELECT UNIQUE this.id, this.status, " + 
    " this.customer.phoneNumber, this.customer.address INTO " + 
    DtoObject.class.getName() + 
    " FROM " + Order.class.getName() + " WHERE this.id = :id");
DtoObject dto = (DtoObject)q.execute(idParam);

假设结果类(DtoObject)服从the conditions of a result class