eclipselink hints for setter updates

2019-08-02 19:44发布

问题:

The actual problem I am facing is to be able to force a eclipselink update irrespective of the content of the cache. Currently eclipselink doesn't create new update statements if the new update has the same value as in the cache. I don't want to use the refresh property( ) in the persistence xml.

I am looking for a solution where I can specify to the entity being update not to use the values in the cache.

EntityManager em=getEntityManager();
    EntityTransaction t = em.getTransaction();

    t.begin();

    Ticket ticket = get(ticketId);

    if(ticket == null) {
        return null;
    }

    ticket.setState(status);

    em.persist(ticket);
    t.commit();

If I change the above update(setState method) to a sql query then the query object has an option to do a setHint where we can mention the setHint(QueryHints.CACHE_USAGE, CacheUsage.DoNotCheckCache). I cannot do this either as there are a lot of such updates used.

In the above snippet the update is happening using a setter method, in this scenario how do we specify a hint to ignore cache? Or is there any other way to do the force updates using the setter object?

 My problem is similar to this : http://www.eclipse.org/forums/index.php/m/660750/

Thank you in advance.

回答1:

The call to persist is unnecessary. persist is used to make a transient entity persistent and managed, and your entity is already persistent and managed.

I don't really see the point of an update which would set all the columns of a row to their current value. But if you really want to trigger such an unnecessary update, consider adding a dummy counter persistent field and incrementing it. EclipseLink will detect a change from its cached value, and will trigger an update.



回答2:

Using the DescriptorCustomizer I was able to override the update sql generated from the eclipselink. Below is the sample snipppet. But this needs a lot of code changes. If any one of you know a way to do this less intrusively by using some annotations or some configruation file changes please let me know.

import org.eclipse.persistence.config.DescriptorCustomizer;
import org.eclipse.persistence.descriptors.ClassDescriptor;

public class TicketCustomizer implements DescriptorCustomizer{
     public void customize(ClassDescriptor descriptor) {

     String sqlString="update Ticket set DESCRIPTION = #DESCRIPTION, CUSTOMERNAME=#CUSTOMERNAME ,STATE= #STATE where ID = #ID and PRODUCTID= #PRODUCTID" ;

         descriptor.getQueryManager().setUpdateSQLString(sqlString);
        }

}

Then define the Customizer for the Entity class:

@Entity
@Customizer(com.oracle.ticketsystem.customizer.TicketCustomizer.class)
public class Ticket implements Serializable {......}