3

我是 GAE 和 Datastore 的新手

我正在尝试使用 JPA 在 GAE 数据存储中插入不同类型的实体。例如,我必须插入 Employee、EmployeePersonalInfo(拥有 Employee 密钥)、EmployeeAddressInfo(拥有 EmployeePersonalInfo 密钥)。在这里,我没有在实体之间创建任何外键关系。我的实体将如下所示

public class Employee{

private String name;
private Key key;
}

public class EmployeePersonalInfo{
private String emailAddress;
private Key key;
private Key employeeKey;
}

public class EmployeeAddressInfo{
private String cityName;
private Key key;
private Key employeePersonalInfoKey;
}

i am trying to insert around 5 record on each table like

public class EmployeeController{

/*This method will be called for 5 times*/
public void insertEmployeeDetails(Employee emp,EmployeePersonalInfo perInfo,EmployeeAddressInfo addressInfo){
employeeDao.save(emp);
employeePersonalInfoDao.save(perInfo);
employeeAddressInfoDao.save(addressInfo);
}
}

每次调用 DAO 类的 Save 方法时,我都会打开 EntityManager 对象,并在操作后关闭。

public void save(Employee emp){
        EntityManager em = EMFService.get().createEntityManager();
                em.save(emp);
                em.close();

}

有时我会遇到一个例外,例如

Caused by: java.lang.IllegalArgumentException: cross-group transaction need to be explicitly specified, see TransactionOptions.Builder.withXG

我已经看到了很多解决这个问题的方法,但我无法理解真正的问题和解决方案是什么。请帮我

4

0 回答 0