java - GAE、JPA、XG-transactions、太多实体组异常

标签 java google-app-engine jpa datanucleus

我知道在 GAE 上的 XG 交易中有 5 个实体组的限制,但我认为我在一次交易中只使用了 3 个组(商品、类别、商品类别),但仍然出现此异常: Caused by: java.lang.IllegalArgumentException: 在单个事务中对太多实体组进行操作。

以下是我的数据模型和 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);
}

商品类别模型

@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);
}

CommodityCategory 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;
}

知道为什么这不起作用吗?

感谢您的回答!

波波

最佳答案

我遇到了类似的问题。我尝试获得超过 5 个相同的实体!从 GAE 数据存储中键入并获得太多实体组异常。所以我认为它不依赖于不同的类型,而是依赖于持久实体的数量。因为同一类型的每个对象都有不同的实体组。当试图获取所有对象的事务时,你会遇到问题。

因此,为了获取实体列表,我不再使用事务并且它有效

关于java - GAE、JPA、XG-transactions、太多实体组异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16675041/

相关文章:

java - 让构造函数抛出异常是个好习惯吗?

java - JAVA 中的 GAE DataStore 查询过滤器与 Query.FilterOperator.EQUAL 无法与数值进行比较

java - 如何在 Hibernate 中使用 PostgreSQL XML 字段?

java - 如何将有关使用 Opencv 进行模糊检测的代码从 C++ 转换为 Java

java - 如何抑制 ResponseHeader

java - 初学者 : Number Guessing Game

Python 谷歌应用引擎 : Send Mail Error

java - Eclipse Luna Gradle插件添加了未引用的依赖项

java - 如何使用不同的命名查询

java - 从 hibernate 查询中获取通用对象