有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JPA实体已经使用GAE持久化了

这个问题与这个问题类似,但提问者从未证实它是否有效。 entityManager.persist(user) -> javax.persistence.EntityExistsException: User@b3089 is already persistent

场景 ProductCategoryAccount具有OneToMany关系,后者与wuth ProductCategory具有ManyToOne关系。插入ProductCategory时,帐户不可用。因此插入ProductCategory时不带帐户。稍后当账户可用时,我想在accounts表中插入账户,并用accounts更新ProductCategory。问题在于更新ProducCategory中的帐户。当我使用mgr时。对于productCategory,我得到一个错误实体已经是持久的!。当我不使用persist时(根据建议,链接提供者(datanucleus)将在提交时负责将其写入数据库),它不会更新。实体和方法如下:

@Entity

public class ProductCategory {

    @Id
    @Column(name = "CAT_ID", allowsNull="false")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key catId;

    @Column(name = "CAT_SHORT_NAME", length=30)
    private String catShortName;

    //other fields

    @OneToMany(mappedBy="productCategory",targetEntity=Account.class, 
            fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    private ArrayList<Account> accounts;  

        //getters & setters

@Entity

public class Account {

    @Id
    @Column(name = "ACCT_NBR_KEY", allowsNull="false")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key acctNbrKey;

    @Column(name = "CAT_ID")
    private Key acctCatId;

    //other fields

    @ManyToOne(optional=false, fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    @JoinColumn(name="CAT_ID",  insertable=false, updatable=false)
    private ProductCategory productCategory;

//getters & setters

AccountEndpoint.java


public void insertAccountBulk() {
        log.info("AccountEndpoint.insertAccountBulk....");

        Account account = new Account();

        ProductCategory pc = (new ProductCategoryEndpoint()).getProductCategoryByShortName("Savings");
        account.setProductCategory(pc);
        account.setAcctCatId(pc.getCatId());

        //setting other fields

        //updationg accounts in product category
            getEntityManager().detach(pc);
        if(pc.getAccounts() == null){
            ArrayList<Account> accts = new ArrayList<Account>();
            accts.add(account);
            pc.setAccounts(accts);
        }
        else{
            pc.getAccounts().add(account);
        }
        getEntityManager().merge(pc);    

        **//new ProductCategoryEndpoint().updateProductCategory(pc);** 

ProductCategoryEndpoint。java

@ApiMethod(name = "updateProductCategory")
public ProductCategory updateProductCategory(ProductCategory productcategory) {
    EntityManager mgr = getEntityManager();
    try {
        if (!containsProductCategory(productcategory)) {
            throw new EntityNotFoundException("Object does not exist");
        }
        mgr.persist(productcategory);
    } finally {
        mgr.close();
    }
    return productcategory;
}

    **If I uncomment new `ProductCategoryEndpoint().updateProductCategory(pc)` I get the error Entity already persistent.

如果我对其进行了评论,则账户不会在^{中更新**


共 (2) 个答案

  1. # 1 楼答案

    尝试更改@JoinColumn注释以允许插入和更新ProductCategory。我认为这是在阻止CategoriesAccount相关联

    @ManyToOne(optional=false, fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    @JoinColumn(name="CAT_ID")
    private ProductCategory productCategory;
    
  2. # 2 楼答案

    根据您的另一个问题判断,您使用的是GAE/Datastore而不是RDBMS,因此也没有使用Hibernate——请更新您的问题并删除该标记

    关于这个问题,如果实体已经是持久的,那么当一个具有某个键的对象已经存在时,您正试图持久化一个具有该键的对象(即,您传递了一个临时对象来持久化。您应该向该方法传递一个分离的对象。日志会告诉您对象处于什么状态)