下面列出了org.hibernate.usertype.UserType#org.hibernate.classic.Lifecycle 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Type byClass(Class clazz, Properties parameters) {
if ( Type.class.isAssignableFrom( clazz ) ) {
return type( clazz, parameters );
}
if ( CompositeUserType.class.isAssignableFrom( clazz ) ) {
return customComponent( clazz, parameters );
}
if ( UserType.class.isAssignableFrom( clazz ) ) {
return custom( clazz, parameters );
}
if ( Lifecycle.class.isAssignableFrom( clazz ) ) {
// not really a many-to-one association *necessarily*
return manyToOne( clazz.getName() );
}
if ( Serializable.class.isAssignableFrom( clazz ) ) {
return serializable( clazz );
}
return null;
}
public PojoEntityTuplizer(EntityMetamodel entityMetamodel, PersistentClass mappedEntity) {
super( entityMetamodel, mappedEntity );
this.mappedClass = mappedEntity.getMappedClass();
this.proxyInterface = mappedEntity.getProxyInterface();
this.lifecycleImplementor = Lifecycle.class.isAssignableFrom( mappedClass );
this.isBytecodeEnhanced = entityMetamodel.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading();
String[] getterNames = new String[propertySpan];
String[] setterNames = new String[propertySpan];
Class[] propTypes = new Class[propertySpan];
for ( int i = 0; i < propertySpan; i++ ) {
getterNames[i] = getters[i].getMethodName();
setterNames[i] = setters[i].getMethodName();
propTypes[i] = getters[i].getReturnType();
}
if ( hasCustomAccessors || !Environment.useReflectionOptimizer() ) {
optimizer = null;
}
else {
// todo : YUCK!!!
optimizer = Environment.getBytecodeProvider().getReflectionOptimizer(
mappedClass,
getterNames,
setterNames,
propTypes
);
// optimizer = getFactory().getSettings().getBytecodeProvider().getReflectionOptimizer(
// mappedClass, getterNames, setterNames, propTypes
// );
}
}
protected boolean invokeDeleteLifecycle(EventSource session, Object entity, EntityPersister persister) {
callbackRegistry.preRemove( entity );
if ( persister.implementsLifecycle() ) {
LOG.debug( "Calling onDelete()" );
if ( ( (Lifecycle) entity ).onDelete( session ) ) {
LOG.debug( "Deletion vetoed by onDelete()" );
return true;
}
}
return false;
}
protected boolean invokeUpdateLifecycle(Object entity, EntityPersister persister, EventSource source) {
if ( persister.implementsLifecycle() ) {
LOG.debug( "Calling onUpdate()" );
if ( ( (Lifecycle) entity ).onUpdate( source ) ) {
LOG.debug( "Update vetoed by onUpdate()" );
return true;
}
}
return false;
}
@Override
public void onPostLoad(PostLoadEvent event) {
final Object entity = event.getEntity();
callbackRegistry.postLoad( entity );
final EntityEntry entry = event.getSession().getPersistenceContext().getEntry( entity );
if ( entry == null ) {
throw new AssertionFailure( "possible non-threadsafe access to the session" );
}
final LockMode lockMode = entry.getLockMode();
if ( LockMode.PESSIMISTIC_FORCE_INCREMENT.equals( lockMode ) ) {
final EntityPersister persister = entry.getPersister();
final Object nextVersion = persister.forceVersionIncrement(
entry.getId(),
entry.getVersion(),
event.getSession()
);
entry.forceLocked( entity, nextVersion );
}
else if ( LockMode.OPTIMISTIC_FORCE_INCREMENT.equals( lockMode ) ) {
final EntityIncrementVersionProcess incrementVersion = new EntityIncrementVersionProcess( entity, entry );
event.getSession().getActionQueue().registerProcess( incrementVersion );
}
else if ( LockMode.OPTIMISTIC.equals( lockMode ) ) {
final EntityVerifyVersionProcess verifyVersion = new EntityVerifyVersionProcess( entity, entry );
event.getSession().getActionQueue().registerProcess( verifyVersion );
}
if ( event.getPersister().implementsLifecycle() ) {
//log.debug( "calling onLoad()" );
( (Lifecycle) event.getEntity() ).onLoad( event.getSession(), event.getId() );
}
}
protected boolean invokeSaveLifecycle(Object entity, EntityPersister persister, EventSource source) {
// Sub-insertions should occur before containing insertion so
// Try to do the callback now
if ( persister.implementsLifecycle() ) {
LOG.debug( "Calling onSave()" );
if ( ((Lifecycle) entity).onSave( source ) ) {
LOG.debug( "Insertion vetoed by onSave()" );
return true;
}
}
return false;
}
public PojoEntityTuplizer(EntityMetamodel entityMetamodel, PersistentClass mappedEntity) {
super( entityMetamodel, mappedEntity );
this.mappedClass = mappedEntity.getMappedClass();
this.proxyInterface = mappedEntity.getProxyInterface();
this.lifecycleImplementor = Lifecycle.class.isAssignableFrom( mappedClass );
this.validatableImplementor = Validatable.class.isAssignableFrom( mappedClass );
Iterator iter = mappedEntity.getPropertyClosureIterator();
while ( iter.hasNext() ) {
Property property = (Property) iter.next();
if ( property.isLazy() ) {
lazyPropertyNames.add( property.getName() );
}
}
String[] getterNames = new String[propertySpan];
String[] setterNames = new String[propertySpan];
Class[] propTypes = new Class[propertySpan];
for ( int i = 0; i < propertySpan; i++ ) {
getterNames[i] = getters[i].getMethodName();
setterNames[i] = setters[i].getMethodName();
propTypes[i] = getters[i].getReturnType();
}
if ( hasCustomAccessors || !Environment.useReflectionOptimizer() ) {
optimizer = null;
}
else {
// todo : YUCK!!!
optimizer = Environment.getBytecodeProvider().getReflectionOptimizer( mappedClass, getterNames, setterNames, propTypes );
// optimizer = getFactory().getSettings().getBytecodeProvider().getReflectionOptimizer(
// mappedClass, getterNames, setterNames, propTypes
// );
}
}
protected boolean invokeDeleteLifecycle(EventSource session, Object entity, EntityPersister persister) {
if ( persister.implementsLifecycle( session.getEntityMode() ) ) {
log.debug( "calling onDelete()" );
if ( ( ( Lifecycle ) entity ).onDelete( session ) ) {
log.debug( "deletion vetoed by onDelete()" );
return true;
}
}
return false;
}
protected boolean invokeUpdateLifecycle(Object entity, EntityPersister persister, EventSource source) {
if ( persister.implementsLifecycle( source.getEntityMode() ) ) {
log.debug( "calling onUpdate()" );
if ( ( ( Lifecycle ) entity ).onUpdate( source ) ) {
log.debug( "update vetoed by onUpdate()" );
return true;
}
}
return false;
}
public void onPostLoad(PostLoadEvent event) {
if ( event.getPersister().implementsLifecycle( event.getSession().getEntityMode() ) ) {
//log.debug( "calling onLoad()" );
( ( Lifecycle ) event.getEntity() ).onLoad( event.getSession(), event.getId() );
}
}
protected boolean invokeSaveLifecycle(Object entity, EntityPersister persister, EventSource source) {
// Sub-insertions should occur before containing insertion so
// Try to do the callback now
if ( persister.implementsLifecycle( source.getEntityMode() ) ) {
log.debug( "calling onSave()" );
if ( ( ( Lifecycle ) entity ).onSave( source ) ) {
log.debug( "insertion vetoed by onSave()" );
return true;
}
}
return false;
}
/**
* Uses heuristics to deduce a Hibernate type given a string naming the type or Java class.
* Return an instance of <tt>org.hibernate.type.Type</tt>.
*/
public static Type heuristicType(String typeName, Properties parameters)
throws MappingException {
Type type = TypeFactory.basic( typeName );
if ( type == null ) {
Class typeClass;
try {
typeClass = ReflectHelper.classForName( typeName );
}
catch (ClassNotFoundException cnfe) {
typeClass = null;
}
if ( typeClass != null ) {
if ( Type.class.isAssignableFrom( typeClass ) ) {
try {
type = (Type) typeClass.newInstance();
}
catch (Exception e) {
throw new MappingException(
"Could not instantiate Type: " + typeClass.getName(),
e
);
}
injectParameters(type, parameters);
}
else if ( CompositeUserType.class.isAssignableFrom( typeClass ) ) {
type = new CompositeCustomType( typeClass, parameters );
}
else if ( UserType.class.isAssignableFrom( typeClass ) ) {
type = new CustomType( typeClass, parameters );
}
else if ( Lifecycle.class.isAssignableFrom( typeClass ) ) {
type = Hibernate.entity( typeClass );
}
else if ( Serializable.class.isAssignableFrom( typeClass ) ) {
type = Hibernate.serializable( typeClass );
}
}
}
return type;
}
/**
* Implemented due to Lifecycle interface being implemented. Not actually
* used.
*/
@Override
public boolean onSave(Session s) throws CallbackException {
return Lifecycle.NO_VETO;
}
/**
* Implemented due to Lifecycle interface being implemented. Not actually
* used.
*/
@Override
public boolean onUpdate(Session s) throws CallbackException {
return Lifecycle.NO_VETO;
}
/**
* Implemented due to Lifecycle interface being implemented. Not actually
* used.
*/
@Override
public boolean onDelete(Session s) throws CallbackException {
return Lifecycle.NO_VETO;
}
/**
* Implemented due to Lifecycle interface being implemented. Not actually
* used.
*/
@Override
public boolean onSave(Session s) throws CallbackException {
return Lifecycle.NO_VETO;
}
/**
* Implemented due to Lifecycle interface being implemented. Not actually
* used.
*/
@Override
public boolean onUpdate(Session s) throws CallbackException {
return Lifecycle.NO_VETO;
}
/**
* Implemented due to Lifecycle interface being implemented. Not actually
* used.
*/
@Override
public boolean onDelete(Session s) throws CallbackException {
return Lifecycle.NO_VETO;
}
/**
* Implemented due to Lifecycle interface being implemented. Not actually
* used.
*/
@Override
public boolean onSave(Session s) throws CallbackException {
return Lifecycle.NO_VETO;
}
/**
* Implemented due to Lifecycle interface being implemented. Not actually
* used.
*/
@Override
public boolean onUpdate(Session s) throws CallbackException {
return Lifecycle.NO_VETO;
}
/**
* Implemented due to Lifecycle interface being implemented. Not actually
* used.
*/
@Override
public boolean onDelete(Session s) throws CallbackException {
return Lifecycle.NO_VETO;
}
/**
* Implemented due to Lifecycle interface being implemented. Not actually
* used.
*/
@Override
public boolean onSave(Session s) throws CallbackException {
return Lifecycle.NO_VETO;
}
/**
* Implemented due to Lifecycle interface being implemented. Not actually
* used.
*/
@Override
public boolean onUpdate(Session s) throws CallbackException {
return Lifecycle.NO_VETO;
}
/**
* Implemented due to Lifecycle interface being implemented. Not actually
* used.
*/
@Override
public boolean onDelete(Session s) throws CallbackException {
return Lifecycle.NO_VETO;
}