没有初始SELECT的eclipselink merge() [英] eclipselink merge() without initial SELECT

查看:80
本文介绍了没有初始SELECT的eclipselink merge()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用eclipselink执行merge(entity),并且我想指示eclipse是更新还是插入,因此它不必执行初始的选择查询.多亏了这个问题所取得的进步,我有以下几点:

但是,我得到以下信息:

org.eclipse.persistence.exceptions.QueryException: Exception Description: Objects cannot be written during a UnitOfWork, they must be registered. Query: UpdateObjectQuery

我正以正确的方式来处理吗?如果是这样,我该如何纠正错误?

谢谢

解决方案

感谢答案的作者

I am trying to perform a merge(entity) using eclipselink, and I would like to indicate to eclipse if that will be an update or insert, so it does not have to perform the initial select query. Thanks to the progress made in this question, I have the following:

UnitOfWorkImpl uow = (UnitOfWorkImpl) ((EntityManagerImpl) em.getDelegate()).getUnitOfWork();

if (dbObj.isInDB())
{
    uow.updateObject(dbObj);
}
else
{
    uow.insertObject(dbObj);
}

However, i get the following:

org.eclipse.persistence.exceptions.QueryException: Exception Description: Objects cannot be written during a UnitOfWork, they must be registered. Query: UpdateObjectQuery

Am I approaching this the correct way? If so, how can I correct the error?

Thanks

解决方案

Thanks to the author of the answer here, the working solution is as follows, keeping track myself of what has gone into the DB, where 'em' is the eclipselink entity manager:

AbstractSession session = ((EntityManagerImpl) em.getDelegate()).getUnitOfWork().getParent();
if (dbObj.getLastModifiedTime().isAfter(lastUpdated))
{
    if (dbObj.isInDB())
    {
        session.updateObject(dbObj);
    }
    else
    {
        session.insertObject(dbObj);
    }
}

这篇关于没有初始SELECT的eclipselink merge()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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