问题
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