Update without select in EclipseLink 2.7

2019-07-26 16:32发布

问题:

I need a solution to update entity without select using EclipeLink 2.7. Here I found the following solution:

EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
UnitOfWork unitOfWork = ((EntityManagerImpl) entityManager.getDelegate()).getUnitOfWork();
AbstractSession session = unitOfWork.getParent();
session.updateObject(entity);
entityManager.getTransaction().commit();

And this is what I get:

Caused by: java.lang.ClassCastException: org.eclipse.persistence.internal.sessions.IsolatedClientSession cannot be cast to org.eclipse.persistence.internal.sessions.UnitOfWorkImpl
    at org.eclipse.persistence.internal.jpa.metadata.listeners.BeanValidationListener.preUpdate(BeanValidationListener.java:90) ~[org.eclipse.persistence.jpa.jar:na]
    at org.eclipse.persistence.descriptors.DescriptorEventManager.notifyListener(DescriptorEventManager.java:726) ~[org.eclipse.persistence.core.jar:na]
    at org.eclipse.persistence.descriptors.DescriptorEventManager.notifyEJB30Listeners(DescriptorEventManager.java:696) ~[org.eclipse.persistence.core.jar:na]
    at org.eclipse.persistence.descriptors.DescriptorEventManager.executeEvent(DescriptorEventManager.java:233) ~[org.eclipse.persistence.core.jar:na]
    at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.updateObjectForWrite(DatabaseQueryMechanism.java:901) ~[org.eclipse.persistence.core.jar:na]
    at org.eclipse.persistence.queries.UpdateObjectQuery.executeCommit(UpdateObjectQuery.java:69) ~[org.eclipse.persistence.core.jar:na]
    at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.executeWrite(DatabaseQueryMechanism.java:259) ~[org.eclipse.persistence.core.jar:na]
    at org.eclipse.persistence.queries.WriteObjectQuery.executeDatabaseQuery(WriteObjectQuery.java:60) ~[org.eclipse.persistence.core.jar:na]
    at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:911) ~[org.eclipse.persistence.core.jar:na]
    at org.eclipse.persistence.internal.sessions.AbstractSession.internalExecuteQuery(AbstractSession.java:3346) ~[org.eclipse.persistence.core.jar:na]
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1892) ~[org.eclipse.persistence.core.jar:na]
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1874) ~[org.eclipse.persistence.core.jar:na]
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1824) ~[org.eclipse.persistence.core.jar:na]
    at org.eclipse.persistence.internal.sessions.AbstractSession.updateObject(AbstractSession.java:4324) ~[org.eclipse.persistence.core.jar:na]

I tried to add to entity

@ExistenceChecking(ExistenceType.CHECK_CACHE)
public class MyEntity {...}

and

@ExistenceChecking(ExistenceType.ASSUME_EXISTENCE)
public class MyEntity {...}

however it didn't help. How to do update without select in EclipseLink 2.7?

Edit
I also tried

EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
UpdateObjectQuery updateQuery= new UpdateObjectQuery();
updateQuery.setObject(entity);
Session session = ((JpaEntityManager)entityManager.getDelegate()).getUnitOfWork().getParent();
session.executeQuery(updateQuery);
entityManager.getTransaction().commit();
entityManager.close();

But got :

Caused by: java.lang.ClassCastException: org.eclipse.persistence.internal.sessions.IsolatedClientSession cannot be cast to org.eclipse.persistence.internal.sessions.UnitOfWorkImpl
    at org.eclipse.persistence.internal.jpa.metadata.listeners.BeanValidationListener.preUpdate(BeanValidationListener.java:90) ~[org.eclipse.persistence.jpa.jar:na]
    at org.eclipse.persistence.descriptors.DescriptorEventManager.notifyListener(DescriptorEventManager.java:726) ~[org.eclipse.persistence.core.jar:na]
    at org.eclipse.persistence.descriptors.DescriptorEventManager.notifyEJB30Listeners(DescriptorEventManager.java:696) ~[org.eclipse.persistence.core.jar:na]
    at org.eclipse.persistence.descriptors.DescriptorEventManager.executeEvent(DescriptorEventManager.java:233) ~[org.eclipse.persistence.core.jar:na]
    at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.updateObjectForWrite(DatabaseQueryMechanism.java:901) ~[org.eclipse.persistence.core.jar:na]
    at org.eclipse.persistence.queries.UpdateObjectQuery.executeCommit(UpdateObjectQuery.java:69) ~[org.eclipse.persistence.core.jar:na]
    at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.executeWrite(DatabaseQueryMechanism.java:259) ~[org.eclipse.persistence.core.jar:na]
    at org.eclipse.persistence.queries.WriteObjectQuery.executeDatabaseQuery(WriteObjectQuery.java:60) ~[org.eclipse.persistence.core.jar:na]
    at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:911) ~[org.eclipse.persistence.core.jar:na]
    at org.eclipse.persistence.internal.sessions.AbstractSession.internalExecuteQuery(AbstractSession.java:3346) ~[org.eclipse.persistence.core.jar:na]
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1892) ~[org.eclipse.persistence.core.jar:na]
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1874) ~[org.eclipse.persistence.core.jar:na]
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1824) ~[org.eclipse.persistence.core.jar:na]

回答1:

JPA merge is a specific API that requires providers pull an instance of your object from the cache or DB and then merge your instance's changes into it.

  UpdateObjectQuery updateQuery= new UpdateObjectQuery();
  updateQuery.setObject(myEntityInstance);
  Session session = ((JpaEntityManager)em.getDelegate()).getUnitOfWork().getParent();

  updated = (MyEntity)session.executeQuery(updateQuery);

instead of merge when I knew the object already exists and don't mind having all the fields included in the update statement. This allows me to use merge in other use cases when I have previously read this instance in this context already, or I need to make sure only updated fields are changed.



回答2:

@Chris pointed in comments that it is necessary to turn off bean validation. So I added to persistence.xml the following line

<property name="javax.persistence.validation.mode" value="none"/>

And the problem was solved.