JDO Persist an existing entity creats new entry in DB

佐手、 提交于 2019-12-25 02:23:12

问题


This is my too first exemple using JDO I have the class Account :

public class Compte
{
    @PrimaryKey
    @Persistent(valueStrategy=IdGeneratorStrategy.INCREMENT)
    private int idCompte;
    // other attributes
    private Regle regle;
    // ....
}

I have entities saved in the table regle, and when i want to create a new Compte i retreive one of those Regle and i add it to the new Compte and i make Compte persisted. I do that :

Compte compte = new Compte();
Regle regle = retreiveRegleByName(name);
compte.setRegle(regle);
saveCompte(compte);

// this is the code of the method saveCompte()
public void creerCompteParticulier(CompteParticulier compteParticulier)
{
    // Persistence of a particular client account
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try
    {
        // begin transaction
        tx.begin();
        pm.makePersistent(compteParticulier);
        tx.commit();

    } catch (Exception exp)
    {

        LOGGER.error("Error: ", exp);
    }
    finally
    {
        if (tx.isActive())
        {
            tx.rollback();
        }
        pm.close();
    }        
}

The new Compte is added to the specific table but my problem is : I have a new entity in the table regle.

Can you help me please, i'd like to create a new Compte with the Regle retreived from database and not a new Regle.

NB : dosn't exist a merge() method in JDO like in JPA ? I think in jpa this problem is resolved by using merg().


回答1:


makePersistent does "merging" of existing objects when the object is an existing detached object. Your object is clearly not detached, so suggest that you read up on detaching an object. Reading the log would have given plenty of insight



来源:https://stackoverflow.com/questions/21069733/jdo-persist-an-existing-entity-creats-new-entry-in-db

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!