2
我知道GAE上XG交易有5個實體組的限制,但我認爲我在一次交易中只使用3組(商品,類別,商品類別)並仍然得到這個異常: 引起:java.lang.IllegalArgumentException:在單個事務中操作太多的實體組。GAE,JPA,XG交易,太多的實體組異常
這裏是我的數據模型的代碼和DAO要害部位:
分類模型
@Entity(name = "Category")
public class Category extends BaseDatastoreEntity{
private String name;
private Key parentKey;
private String parentName;
@Unowned
@OneToMany(cascade= CascadeType.ALL)
private Set<CommodityCategory> commodityCategories = new HashSet<CommodityCategory>();
.
.
public void addCommodityCategoryInternal(CommodityCategory commodityCategory) {
this.commodityCategories.add(commodityCategory);
}
商品型號
@Entity(name = "Commodity")
public class Commodity extends BaseDatastoreEntity implements IRateable{
private String name;
private BigDecimal price;
.
.
@OneToMany(mappedBy = "commodity", cascade= CascadeType.ALL, fetch= FetchType.EAGER)
private Set<Picture> pictures = new HashSet<Picture>();
@Unowned
@OneToMany(cascade= CascadeType.ALL)
private Set<CommodityCategory> commodityCategories = new HashSet<CommodityCategory>();
.
.
public void addCommodityCategoryInternal(CommodityCategory commodityCategory) {
this.commodityCategories.add(commodityCategory);
}
CommodityCategory模型
@Entity(name="CommodityCategory")
public class CommodityCategory extends BaseDatastoreEntity{
private boolean isDefaultCategory;
@Unowned
@ManyToOne(cascade= CascadeType.ALL)
private Key commodity;
@Unowned
@ManyToOne(cascade= CascadeType.ALL)
private Key category;
@SuppressWarnings("LeakingThisInConstructor")
public CommodityCategory(boolean isDefaultCategory, Commodity commodity, Category category) {
super(true);
this.isDefaultCategory = isDefaultCategory;
this.commodity = commodity.getId();
this.category = category.getId();
category.addCommodityCategoryInternal(this);
commodity.addCommodityCategoryInternal(this);
}
商品分類DAO實施
@Repository("commodityCategoryDAOImpl")
public class CommodityCategoryDAOImpl extends AbstractDAO<CommodityCategory, Key> implements CommodityCategoryDAO{
@Override
public CommodityCategory create(boolean isDefaultCategory, Commodity comm, Category cat) {
EntityManager em = EMF.get().createEntityManager();
setEntityManager(em);
getEntityManager().clear();
getEntityManager().getTransaction().begin();
Commodity commodity = getEntityManager().find(Commodity.class, comm.getId());
Category category = getEntityManager().find(Category.class, cat.getId());
CommodityCategory commodityCategory = new CommodityCategory(isDefaultCategory, commodity, category);
getEntityManager().persist(commodityCategory);
getEntityManager().getTransaction().commit();//here is the exception
getEntityManager().clear();
return commodityCategory;
}
任何想法,爲什麼這不應該工作?
感謝您的任何答案!
Pobo
感謝您的回覆,但是當我堅持新CommodityCategory對象時發生的錯誤。而且只有1個類別對象和1個商品對象,所以5 +就不會有問題。我不使用事務來檢索列表。 – Pobo