[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-7206) NaturalIdLoadAccess: NaturalId synchronization with no need to flush at all

Guenther Demetz (JIRA) noreply at atlassian.com
Thu Mar 29 08:50:50 EDT 2012


    [ https://hibernate.onjira.com/browse/HHH-7206?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=46116#comment-46116 ] 

Guenther Demetz commented on HHH-7206:
--------------------------------------

For further performance gain, it would be interesting giving the user a possibilty to synch the NaturalId's manually on demand,
thus allowing him to disable auto-synch on NaturalIdLoadAccess.

{code}

@Entity
class A {

  @Id 
  private long id; 

  @NaturalId(mutable=true)
  private String code;

  public void setCode(String code) {
      this.code = code;
      currentSession.synchNaturalId(this); // manually sync through new method synchNaturalId(Object entity) in Session interface
  }
}

 SimpleNaturalIdLoadAccess simpleNaturalIdLoadAccess = currentSession.bySimpleNaturalId(A.class);
 simpleNaturalIdLoadAccess.setAutoSynchronization(false); // new method to disable synch
 A a = (A) simp.load("1");
 assertNotNull(a);
 a.setCode("changed");
 assertEquals(a, simp.load("changed")); 

{code}


Following code changes would implement this second enhancement:

{code:title=SessionImpl.java|borderStyle=solid}
/**
* Does synchronize the context for successive no-auto-synch LoadNaturalId accesses  
* @param attached entity with changed, not yet flushed, NaturalId values 
*/
public void synchNaturalId(Object entity) {
    final EntityEntry entry = getPersistenceContext().getEntry(entity);
    if (entry == null || !entry.requiresDirtyCheck(entity)) 
    	return;
    final FlushEntityEvent entityEvent = new FlushEntityEvent( this, entity, entry );
    final DefaultFlushEntityEventListener evl = new DefaultFlushEntityEventListener();
    final Object[] values = evl.getValues( entity, entry, true, this );
    entityEvent.setPropertyValues(values);
    evl.dirtyCheck(entityEvent);
    int[] dirtyProperties  = entityEvent.getDirtyProperties();
    if (dirtyProperties != null) {
    	persistenceContext.entityStateUpdatedNotification( entry, values ); // synchronizes natural id caches
    }
}
{code}

Add synchNaturalId method declaration in Session interface.

{code:title=SessionImpl$BaseNaturalIdLoadAccessImpl.java|borderStyle=solid}

boolean autoSynch = true;

public void setAutoSynchronization(boolean onOff) {
   this.autoSynch = onOff;
}

protected final Serializable resolveNaturalId(Map<String, Object> naturalIdParameters) {
  if (autoSynch && isTransactionInProgress() && getPersistenceContext().getEntityEntries().size() > 0) {
  ... (code unchanged, as it was)
{code}

Finally add setAutoSynchronization declaration in NaturalIdLoadAccess and SimpleNaturalIdLoadAccess interfaces.







> NaturalIdLoadAccess: NaturalId synchronization with no need to flush at all
> ---------------------------------------------------------------------------
>
>                 Key: HHH-7206
>                 URL: https://hibernate.onjira.com/browse/HHH-7206
>             Project: Hibernate ORM
>          Issue Type: Improvement
>          Components: core
>    Affects Versions: 4.1.1
>         Environment: Hibernate4, database-independent
>            Reporter: Guenther Demetz
>              Labels: flush, naturalId
>
> With few code changes I think it is possible to make NaturalIdLoadAccess work with no need to rely on flush at all,
> please see following comments.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list