Question

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().

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top