下面列出了怎么用org.hibernate.engine.OptimisticLockStyle的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 构造Hibernate的乐观锁
*/
private void handleVersion(Property prop, PersistentClass pclazz) {
if (!(pclazz instanceof RootClass)) {
throw new AnnotationException(
"Unable to define/override @Version on a subclass: "
+ pclazz.getEntityName());
}
RootClass root = (RootClass) pclazz;
root.setVersion(prop);
root.setDeclaredVersion(prop);
root.setOptimisticLockStyle(OptimisticLockStyle.VERSION);
}
OptimisticLockStyle getVersioning(OptimisticLockType type) {
switch ( type ) {
case VERSION:
return OptimisticLockStyle.VERSION;
case NONE:
return OptimisticLockStyle.NONE;
case DIRTY:
return OptimisticLockStyle.DIRTY;
case ALL:
return OptimisticLockStyle.ALL;
default:
throw new AssertionFailure( "optimistic locking not supported: " + type );
}
}
protected void bindVersion(PersistentProperty version, RootClass entity,
InFlightMetadataCollector mappings, String sessionFactoryBeanName) {
if(version != null) {
SimpleValue val = new SimpleValue(metadataBuildingContext, entity.getTable());
bindSimpleValue(version, null, val, EMPTY_PATH, mappings, sessionFactoryBeanName);
if (val.isTypeSpecified()) {
if (!(val.getType() instanceof IntegerType ||
val.getType() instanceof LongType ||
val.getType() instanceof TimestampType)) {
LOG.warn("Invalid version class specified in " + version.getOwner().getName() +
"; must be one of [int, Integer, long, Long, Timestamp, Date]. Not mapping the version.");
return;
}
}
else {
val.setTypeName("version".equals(version.getName()) ? "integer" : "timestamp");
}
Property prop = new Property();
prop.setValue(val);
bindProperty(version, prop, mappings);
prop.setLazy(false);
val.setNullValue("undefined");
entity.setVersion(prop);
entity.setDeclaredVersion(prop);
entity.setOptimisticLockStyle(OptimisticLockStyle.VERSION);
entity.addProperty(prop);
}
}
default boolean isAllOrDirtyOptimisticLocking() {
OptimisticLockStyle optimisticLockStyle =
delegate().getEntityMetamodel().getOptimisticLockStyle();
return optimisticLockStyle == OptimisticLockStyle.DIRTY
|| optimisticLockStyle == OptimisticLockStyle.ALL;
}
default CompletionStage<Boolean> updateReactive(
final Serializable id,
final Object[] fields,
final Object[] oldFields,
final Object rowId,
final boolean[] includeProperty,
final int j,
final Object oldVersion,
final Object object,
final String sql,
final SharedSessionContractImplementor session) throws HibernateException {
final Expectation expectation = appropriateExpectation( delegate().getUpdateResultCheckStyles()[j] );
// final int jdbcBatchSizeToUse = session.getConfiguredJdbcBatchSize();
// final boolean useBatch = expectation.canBeBatched() && isBatchable() && jdbcBatchSizeToUse > 1;
// if ( useBatch && updateBatchKey == null ) {
// updateBatchKey = new BasicBatchKey(
// delegate.getEntityName() + "#UPDATE",
// expectation
// );
// }
// final boolean callable = delegate.isUpdateCallable( j );
final boolean useVersion = j == 0 && delegate().isVersioned();
if ( log.isTraceEnabled() ) {
log.tracev( "Updating entity: {0}", infoString(delegate(), id, delegate().getFactory() ) );
if ( useVersion ) {
log.tracev( "Existing version: {0} -> New version:{1}", oldVersion, fields[delegate().getVersionProperty()] );
}
}
// if ( useBatch ) {
// update = session
// .getJdbcCoordinator()
// .getBatch( updateBatchKey )
// .getBatchStatement( sql, callable );
// }
Object[] params = PreparedStatementAdaptor.bind( update -> {
int index = 1;
index += expectation.prepare( update );
//Now write the values of fields onto the prepared statement
index = delegate().dehydrate(
id,
fields,
rowId,
includeProperty,
delegate().getPropertyColumnUpdateable(),
j,
update,
session,
index,
true
);
// Write any appropriate versioning conditional parameters
if ( useVersion && delegate().getEntityMetamodel().getOptimisticLockStyle() == OptimisticLockStyle.VERSION ) {
if ( delegate().checkVersion( includeProperty ) ) {
delegate().getVersionType().nullSafeSet( update, oldVersion, index, session );
}
}
else if ( isAllOrDirtyOptimisticLocking() && oldFields != null ) {
boolean[] versionability = delegate().getPropertyVersionability(); //TODO: is this really necessary????
boolean[] includeOldField = delegate().getEntityMetamodel().getOptimisticLockStyle() == OptimisticLockStyle.ALL
? delegate().getPropertyUpdateability()
: includeProperty;
Type[] types = delegate().getPropertyTypes();
for (int i = 0; i < delegate().getEntityMetamodel().getPropertySpan(); i++ ) {
boolean include = includeOldField[i] &&
delegate().isPropertyOfTable( i, j ) &&
versionability[i]; //TODO: is this really necessary????
if ( include ) {
boolean[] settable = types[i].toColumnNullness( oldFields[i], delegate().getFactory() );
types[i].nullSafeSet(
update,
oldFields[i],
index,
settable,
session
);
index += ArrayHelper.countTrue( settable );
}
}
}
} );
// if ( useBatch ) {
// session.getJdbcCoordinator().getBatch( updateBatchKey ).addToBatch();
// return true;
// }
return getReactiveConnection(session)
.update( sql, params )
.thenApply( count -> check( count, id, j, expectation, new PreparedStatementAdaptor() ) );
}
@Override
public OptimisticLockStyle getOptimisticLockStyle() {
return superclass.getOptimisticLockStyle();
}
/**
* @deprecated prefer {@link #setOptimisticLockStyle}
*/
@Deprecated
public void setOptimisticLockMode(int optimisticLockMode) {
setOptimisticLockStyle( OptimisticLockStyle.interpretOldCode( optimisticLockMode ) );
}
public OptimisticLockStyle getOptimisticLockStyle() {
return optimisticLockStyle;
}
public void setOptimisticLockStyle(OptimisticLockStyle optimisticLockStyle) {
this.optimisticLockStyle = optimisticLockStyle;
}
public boolean isBatchable() {
return optimisticLockStyle() == OptimisticLockStyle.NONE
|| ( !isVersioned() && optimisticLockStyle() == OptimisticLockStyle.VERSION )
|| getFactory().getSessionFactoryOptions().isJdbcBatchVersionedData();
}
private boolean isAllOrDirtyOptLocking() {
return entityMetamodel.getOptimisticLockStyle() == OptimisticLockStyle.DIRTY
|| entityMetamodel.getOptimisticLockStyle() == OptimisticLockStyle.ALL;
}
protected final OptimisticLockStyle optimisticLockStyle() {
return entityMetamodel.getOptimisticLockStyle();
}
public OptimisticLockStyle getOptimisticLockStyle() {
return optimisticLockStyle;
}
@Override
public OptimisticLockStyle getOptimisticLockStyle() {
return entityElement().getOptimisticLock();
}
public static OptimisticLockStyle fromXml(String name) {
return OptimisticLockStyle.valueOf( name == null ? null : name.toUpperCase( Locale.ENGLISH ) );
}
public static String toXml(OptimisticLockStyle lockMode) {
return lockMode == null ? null : lockMode.name().toLowerCase( Locale.ENGLISH );
}
/**
* Obtain the optimistic locking style for this entity.
*
* @return The optimistic locking style.
*/
public OptimisticLockStyle getOptimisticLockStyle();