Hi guys:
I currently running into issues that: second update for
inventoryDao.addRootCategory(category2); will throw exception that:
Caused by: java.lang.IllegalArgumentException: can't operate on
multiple entity groups in a single transaction. found both Element {
type: "CategoryEntity"
id: 1
}
and Element {
type: "CategoryEntity"
id: 8
}
anyone can help?
///////////////////////////////////////////////
public class CategoryEntity extends BaseEntity {
private static final long serialVersionUID = 4289692014710028046L;
private String categoryCode;
private int sortOrder;
//owned
@OneToMany(mappedBy = "categoryEntity", cascade = CascadeType.ALL)
private List<ProductEntity> products = new
ArrayList<ProductEntity>();
//unowned
@OneToMany(cascade = CascadeType.ALL)
private List<NameEntity> categoryNames = new
ArrayList<NameEntity>();
//unowned
@OneToMany(cascade = CascadeType.ALL)
private List<ImageEntity> images = new ArrayList<ImageEntity>();
//unowned
@OneToMany(cascade = CascadeType.ALL)
private List<AttributeEntity> categoryAttributes = new
ArrayList<AttributeEntity>();
//unowned
@OneToMany(cascade = CascadeType.ALL)
private List<DescriptionEntity> categoryDescriptions = new
ArrayList<DescriptionEntity>();
/////////////////////////////////////////////////////////
@Entity
@MappedSuperclass
public class BaseEntity implements Serializable {
private static final long serialVersionUID = -1042459872290397535L;
@Transient
public static SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy
hh:mm:ss a");
@Temporal(TemporalType.TIMESTAMP)
private Date creationDate;
private String creationDateString = "";
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key key;
@Version
private long version;
public BaseEntity() {
setCreationDate(Calendar.getInstance().getTime());
}
/**
* @see java.lang.Object#equals(Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BaseEntity other = (BaseEntity) obj;
if (getKey() == null) {
if (other.getKey() != null)
return false;
} else if (!getKey().equals(other.getKey())) {
return false;
}
if (getKey() != null && other.getKey() != null) {
if (getKey().getId() == other.getKey().getId()) {
return true;
} else {
return false;
}
}
return true;
}
public Date getCreationDate() {
return creationDate;
}
public String getCreationDateString() {
return creationDateString;
}
/**
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getKey() == null) ? 0 :
getKey().hashCode());
return result;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
if (null != creationDate) {
this.creationDateString = sdf.format(creationDate);
}
}
public void setCreationDateString(String creationDateString) {
this.creationDateString = creationDateString;
}
public long getVersion() {
return version;
}
public void setVersion(long version) {
this.version = version;
}
public Key getKey() {
return key;
}
public void setKey(Key key) {
this.key = key;
}
}
/////////////////////////////////////////////////////////////////////////
public CategoryEntity addRootCategory(CategoryEntity category) throws
GcommerceException {
if (null != category) {
if (null != category.getKey()) {
throw new
GcommerceEntityAlreadyPersistentException(CategoryEntity.class.getName());
}
try {
getEntityManager().getTransaction().begin();
getEntityManager().persist(category);
getEntityManager().getTransaction().commit();
return category;
} finally {
if
(getEntityManager().getTransaction().isActive()) {
getEntityManager().getTransaction().rollback();
}
}
} else {
throw new NullPointerException("category can NOT be
null!");
}
}
////////////////////////////////////////////////////////////////////////
@Autowired
InventoryDao inventoryDao;
CategoryEntity category1;
CategoryEntity category2;
CategoryEntity category3;
CategoryEntity category4;
CountryEntity country1;
CountryEntity country2;
CountryEntity country3;
CountryEntity country4;
CountryEntity country5;
CountryEntity country6;
CountryEntity country7;
CountryEntity country8;
CountryEntity country9;
CountryEntity country10;
CountryEntity country11;
CountryEntity country12;
NameEntity categoryName1;
NameEntity categoryName2;
NameEntity categoryName3;
NameEntity categoryName4;
DescriptionEntity categoryDescriptionEntity1;
DescriptionEntity categoryDescriptionEntity2;
DescriptionEntity categoryDescriptionEntity3;
DescriptionEntity categoryDescriptionEntity4;
NameEntity attrName1;
NameEntity attrName2;
NameEntity attrName3;
NameEntity attrName4;
AttributeEntity categoryAttribute1;
AttributeEntity categoryAttribute2;
AttributeEntity categoryAttribute3;
AttributeEntity categoryAttribute4;
@Before
public void setupData() throws ParseException {
country1 = new CountryEntity();
country1.setCountryCode("AU");
country1.setCountryName("Australia");
country2 = new CountryEntity();
country2.setCountryCode("AU");
country2.setCountryName("Australia");
country3 = new CountryEntity();
country3.setCountryCode("AU");
country3.setCountryName("Australia");
country4 = new CountryEntity();
country4.setCountryCode("AU");
country4.setCountryName("Australia");
categoryDescriptionEntity1 = new DescriptionEntity();
categoryDescriptionEntity1.setCountryEntity(country1);
categoryDescriptionEntity1.setDesc("category desc1");
categoryDescriptionEntity2 = new DescriptionEntity();
categoryDescriptionEntity2.setCountryEntity(country2);
categoryDescriptionEntity2.setDesc("category desc2");
categoryDescriptionEntity3 = new DescriptionEntity();
categoryDescriptionEntity3.setCountryEntity(country3);
categoryDescriptionEntity3.setDesc("category desc3");
categoryDescriptionEntity4 = new DescriptionEntity();
categoryDescriptionEntity4.setCountryEntity(country4);
categoryDescriptionEntity4.setDesc("category desc4");
categoryName1 = new NameEntity();
categoryName1.setCountryEntity(country5);
categoryName1.setName("categoryName1");
categoryName2 = new NameEntity();
categoryName2.setCountryEntity(country6);
categoryName2.setName("categoryName2");
categoryName3 = new NameEntity();
categoryName3.setCountryEntity(country7);
categoryName3.setName("categoryName3");
categoryName4 = new NameEntity();
categoryName4.setCountryEntity(country8);
categoryName4.setName("categoryName4");
attrName1 = new NameEntity();
attrName1.setCountryEntity(country9);
attrName1.setName("attr1");
attrName2 = new NameEntity();
attrName2.setCountryEntity(country10);
attrName2.setName("attr2");
attrName3 = new NameEntity();
attrName3.setCountryEntity(country11);
attrName3.setName("attr3");
attrName4 = new NameEntity();
attrName4.setCountryEntity(country12);
attrName4.setName("attr4");
categoryAttribute1 = new AttributeEntity();
categoryAttribute1.getAttributeNames().add(attrName1);
categoryAttribute1.setAttributeValue("categoryAttribute1");
categoryAttribute1.setAttributeKey("categoryAttribute1");
categoryAttribute2 = new AttributeEntity();
categoryAttribute2.getAttributeNames().add(attrName1);
categoryAttribute2.setAttributeValue("categoryAttribute2");
categoryAttribute2.setAttributeKey("categoryAttribute2");
categoryAttribute3 = new AttributeEntity();
categoryAttribute3.getAttributeNames().add(attrName1);
categoryAttribute3.setAttributeValue("categoryAttribute3");
categoryAttribute3.setAttributeKey("categoryAttribute13");
categoryAttribute4 = new AttributeEntity();
categoryAttribute4.getAttributeNames().add(attrName1);
categoryAttribute4.setAttributeValue("categoryAttribute4");
categoryAttribute4.setAttributeKey("categoryAttribute14");
ImageEntity categoryImage1 = new ImageEntity();
ImageEntity categoryImage2 = new ImageEntity();
ImageEntity categoryImage3 = new ImageEntity();
ImageEntity categoryImage4 = new ImageEntity();
category1 = new CategoryEntity();
category1.getCategoryNames().add(categoryName1);
category1.getCategoryAttributes().add(categoryAttribute2);
category1.setCategoryCode("category1");
category1.getCategoryDescriptions().add(categoryDescriptionEntity1);
category1.getImages().add(categoryImage1);
category2 = new CategoryEntity();
category2.getCategoryNames().add(categoryName1);
category2.getCategoryAttributes().add(categoryAttribute2);
category2.setCategoryCode("category2");
category2.getCategoryDescriptions().add(categoryDescriptionEntity2);
category2.getImages().add(categoryImage2);
category3 = new CategoryEntity();
category3.getCategoryNames().add(categoryName1);
category3.getCategoryAttributes().add(categoryAttribute3);
category3.setCategoryCode("category3");
category3.getCategoryDescriptions().add(categoryDescriptionEntity3);
category3.getImages().add(categoryImage3);
category4 = new CategoryEntity();
category4.getCategoryNames().add(categoryName1);
category4.getCategoryAttributes().add(categoryAttribute4);
category4.setCategoryCode("category4");
category4.getCategoryDescriptions().add(categoryDescriptionEntity4);
category4.getImages().add(categoryImage4);
}
@Test
public void testAddRootCategory() throws GcommerceException {
inventoryDao.addRootCategory(category1);
assertEquals(category1.getKey() != null, true);
inventoryDao.addRootCategory(category2);
assertEquals(category2.getKey() != null, true);
inventoryDao.addRootCategory(category3);
assertEquals(category3.getKey() != null, true);
inventoryDao.addRootCategory(category4);
assertEquals(category4.getKey() != null, true);
}
--
You received this message because you are subscribed to the Google Groups
"Google App Engine for Java" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine-java?hl=en.