JDO坚持现有实体在数据库中创建新条目 [英] JDO Persist an existing entity creats new entry in DB

查看:79
本文介绍了JDO坚持现有实体在数据库中创建新条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我使用JDO的第一个示例 我有Account课程:

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;
    // ....
}

我在表regle中保存了实体,并且当我想创建一个新的Compte时我会回溯 那些Regle之一,我将其添加到新的Compte中,然后我使Compte保持不变. 我这样做:

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();
    }        
}

新的Compte已添加到特定表中,但我的问题是: 我在表regle中有一个新实体.

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

能帮我吗,我想用数据库中的Regle创建一个新的Compte,而不是一个新的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:在JDO中不像JPA中那样存在merge()方法吗? 我认为在jpa中,可以通过使用merg()解决此问题.

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

推荐答案

makePersistent当对象是现有的分离对象时,会合并"现有的对象.您的对象显然没有分离,因此建议您阅读有关分离对象的内容.阅读日志将提供大量的见识

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

这篇关于JDO坚持现有实体在数据库中创建新条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆