【发布时间】:2014-01-12 00:27:03
【问题描述】:
这是我第一个使用 JDO 的例子 我有班级帐号:
public class Compte
{
@PrimaryKey
@Persistent(valueStrategy=IdGeneratorStrategy.INCREMENT)
private int idCompte;
// other attributes
private Regle regle;
// ....
}
我在 regle 表中保存了实体,当我想创建一个新的 Compte 时,我会检索 其中一个 Regle,我将它添加到新的 Compte 中,我让 Compte 持久化。 我这样做:
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();
}
}
新的 Compte 已添加到特定表中,但我的问题是: 我在 regle 表中有一个新实体。
你能帮帮我吗,我想用从数据库中检索的 Regle 创建一个新的 Compte,而不是一个新的 Regle。
注意:在 JDO 中不存在类似 JPA 中的 merge() 方法吗? 我认为在 jpa 中这个问题可以通过使用 merg() 来解决。
【问题讨论】:
-
compteParticulier.idCompte的值是多少?有没有可能是 0? -
是 idCompte = 0 因为 Compte 它是一个新实例但是 idRegle 不是 zoro (非空),因为我从数据库中检索到它