eclipselink merge() without initial SELECT

我是研究僧i 提交于 2019-12-20 03:03:32

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/19991877/eclipselink-merge-without-initial-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!