Skip to content

Commit 8aa37a9

Browse files
committed
fix the typing in ReplicationMode
and improve readability in DefaultReplicateEventListener even though they are deprecated Signed-off-by: Gavin King <[email protected]>
1 parent e351a00 commit 8aa37a9

File tree

2 files changed

+55
-66
lines changed

2 files changed

+55
-66
lines changed

hibernate-core/src/main/java/org/hibernate/ReplicationMode.java

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ public enum ReplicationMode {
2525
*/
2626
EXCEPTION {
2727
@Override
28-
public boolean shouldOverwriteCurrentVersion(
29-
Object entity,
30-
Object currentVersion, Object newVersion,
31-
BasicType<Object> versionType) {
28+
public <T> boolean shouldOverwriteCurrentVersion(
29+
T currentVersion, T newVersion,
30+
BasicType<T> versionType) {
3231
throw new AssertionFailure( "should not be called" );
3332
}
3433
},
@@ -37,10 +36,9 @@ public boolean shouldOverwriteCurrentVersion(
3736
*/
3837
IGNORE {
3938
@Override
40-
public boolean shouldOverwriteCurrentVersion(
41-
Object entity,
42-
Object currentVersion, Object newVersion,
43-
BasicType<Object> versionType) {
39+
public <T> boolean shouldOverwriteCurrentVersion(
40+
T currentVersion, T newVersion,
41+
BasicType<T> versionType) {
4442
return false;
4543
}
4644
},
@@ -49,10 +47,9 @@ public boolean shouldOverwriteCurrentVersion(
4947
*/
5048
OVERWRITE {
5149
@Override
52-
public boolean shouldOverwriteCurrentVersion(
53-
Object entity,
54-
Object currentVersion, Object newVersion,
55-
BasicType<Object> versionType) {
50+
public <T> boolean shouldOverwriteCurrentVersion(
51+
T currentVersion, T newVersion,
52+
BasicType<T> versionType) {
5653
return true;
5754
}
5855
},
@@ -61,10 +58,9 @@ public boolean shouldOverwriteCurrentVersion(
6158
*/
6259
LATEST_VERSION {
6360
@Override
64-
public boolean shouldOverwriteCurrentVersion(
65-
Object entity,
66-
Object currentVersion, Object newVersion,
67-
BasicType<Object> versionType) {
61+
public <T> boolean shouldOverwriteCurrentVersion(
62+
T currentVersion, T newVersion,
63+
BasicType<T> versionType) {
6864
// always overwrite non-versioned data (because we don't know which is newer)
6965
return versionType == null
7066
|| versionType.getJavaTypeDescriptor().getComparator()
@@ -82,9 +78,7 @@ public boolean shouldOverwriteCurrentVersion(
8278
*
8379
* @return {@code true} indicates the data should be overwritten; {@code false} indicates it should not.
8480
*/
85-
public abstract boolean shouldOverwriteCurrentVersion(
86-
Object entity,
87-
Object currentVersion, Object newVersion,
88-
BasicType<Object> versionType);
89-
81+
public abstract <T> boolean shouldOverwriteCurrentVersion(
82+
T currentVersion, T newVersion,
83+
BasicType<T> versionType);
9084
}

hibernate-core/src/main/java/org/hibernate/event/internal/DefaultReplicateEventListener.java

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424
import org.hibernate.internal.CoreLogging;
2525
import org.hibernate.internal.CoreMessageLogger;
2626
import org.hibernate.persister.entity.EntityPersister;
27-
import org.hibernate.pretty.MessageHelper;
2827
import org.hibernate.type.BasicType;
2928
import org.hibernate.type.Type;
3029

30+
import static org.hibernate.pretty.MessageHelper.infoString;
31+
3132
/**
3233
* Defines the default replicate event listener used by Hibernate to replicate
3334
* entities in response to generated replicate events.
@@ -55,79 +56,72 @@ public void onReplicate(ReplicateEvent event) {
5556
final PersistenceContext persistenceContext = source.getPersistenceContextInternal();
5657
if ( persistenceContext.reassociateIfUninitializedProxy( event.getObject() ) ) {
5758
LOG.trace( "Uninitialized proxy passed to replicate()" );
58-
return;
5959
}
60-
61-
Object entity = persistenceContext.unproxyAndReassociate( event.getObject() );
62-
63-
if ( persistenceContext.isEntryFor( entity ) ) {
64-
LOG.trace( "Ignoring persistent instance passed to replicate()" );
65-
//hum ... should we cascade anyway? throw an exception? fine like it is?
66-
return;
60+
else {
61+
final Object entity = persistenceContext.unproxyAndReassociate( event.getObject() );
62+
if ( persistenceContext.isEntryFor( entity ) ) {
63+
LOG.trace( "Ignoring persistent instance passed to replicate()" );
64+
//hum ... should we cascade anyway? throw an exception? fine like it is?
65+
}
66+
else {
67+
doReplicate( event, source, entity );
68+
}
6769
}
70+
}
6871

69-
EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity );
72+
private void doReplicate(ReplicateEvent event, EventSource source, Object entity) {
73+
final EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity);
74+
final ReplicationMode replicationMode = event.getReplicationMode();
7075

71-
// get the id from the object
72-
/*if ( persister.isUnsaved(entity, source) ) {
73-
throw new TransientObjectException("transient instance passed to replicate()");
74-
}*/
75-
Object id = persister.getIdentifier( entity, source );
76+
// get the id from the object - we accept almost anything at all,
77+
// except null (that is, even ids which look like they're unsaved)
78+
final Object id = persister.getIdentifier( entity, source );
7679
if ( id == null ) {
7780
throw new TransientObjectException( "instance with null id passed to replicate()" );
7881
}
7982

80-
final ReplicationMode replicationMode = event.getReplicationMode();
81-
8283
final Object oldVersion = replicationMode == ReplicationMode.EXCEPTION
83-
? null //always do an INSERT, and let it fail by constraint violation
84-
: persister.getCurrentVersion(id, source); //what is the version on the database?
84+
? null // always do an INSERT, and let it fail by constraint violation
85+
: persister.getCurrentVersion( id, source); // what is the version on the database?
8586

8687
if ( oldVersion != null ) {
8788
if ( LOG.isTraceEnabled() ) {
88-
LOG.tracev(
89-
"Found existing row for {0}",
90-
MessageHelper.infoString( persister, id, event.getFactory() )
91-
);
89+
LOG.tracev("Found existing row for {0}",
90+
infoString( persister, id, event.getFactory() ) );
9291
}
93-
94-
@SuppressWarnings("unchecked")
95-
final BasicType<Object> versionType = (BasicType<Object>) persister.getVersionType();
96-
final Object realOldVersion = persister.isVersioned() ? oldVersion : null; /// HHH-2378
97-
boolean canReplicate = replicationMode.shouldOverwriteCurrentVersion(
98-
entity,
99-
realOldVersion,
100-
persister.getVersion( entity ),
101-
versionType
102-
);
103-
104-
// if can replicate, will result in a SQL UPDATE
105-
// else do nothing (don't even re-associate object!)
106-
if ( canReplicate ) {
92+
// If the entity has no version, getCurrentVersion() just returns
93+
// a meaningless value to indicate that the row exists (HHH-2378)
94+
final Object realOldVersion = persister.isVersioned() ? oldVersion : null;
95+
if ( shouldOverwrite( replicationMode,
96+
persister.getVersion( entity ), realOldVersion,
97+
persister.getVersionType() ) ) {
98+
// execute a SQL UPDATE
10799
performReplication( entity, id, realOldVersion, persister, replicationMode, source );
108100
}
109101
else if ( LOG.isTraceEnabled() ) {
102+
// do nothing (don't even re-associate object!)
110103
LOG.trace( "No need to replicate" );
111104
}
112105

113106
//TODO: would it be better to do a refresh from db?
114107
}
115108
else {
116-
// no existing row - do an insert
109+
// no existing row - execute a SQL INSERT
117110
if ( LOG.isTraceEnabled() ) {
118-
LOG.tracev(
119-
"No existing row, replicating new instance {0}",
120-
MessageHelper.infoString( persister, id, event.getFactory() )
121-
);
111+
LOG.tracev( "No existing row, replicating new instance {0}",
112+
infoString( persister, id, event.getFactory() ) );
122113
}
123-
124114
final boolean regenerate = persister.isIdentifierAssignedByInsert(); // prefer re-generation of identity!
125115
final EntityKey key = regenerate ? null : source.generateEntityKey( id, persister );
126-
127116
performSaveOrReplicate( entity, key, persister, regenerate, replicationMode, source, false );
128117
}
129118
}
130119

120+
private static <T> boolean shouldOverwrite(
121+
ReplicationMode replicationMode, Object entityVersion, Object realOldVersion, BasicType<T> versionType) {
122+
return replicationMode.shouldOverwriteCurrentVersion( (T) realOldVersion, (T) entityVersion, versionType );
123+
}
124+
131125
@Override
132126
protected boolean visitCollectionsBeforeSave(
133127
Object entity,
@@ -165,7 +159,8 @@ private void performReplication(
165159
EventSource source) throws HibernateException {
166160

167161
if ( LOG.isTraceEnabled() ) {
168-
LOG.tracev( "Replicating changes to {0}", MessageHelper.infoString( persister, id, source.getFactory() ) );
162+
LOG.tracev( "Replicating changes to {0}",
163+
infoString( persister, id, source.getFactory() ) );
169164
}
170165

171166
new OnReplicateVisitor( source, id, entity, true ).process( entity, persister );

0 commit comments

Comments
 (0)