【Java开源代码栏目提醒】:网学会员Java开源代码为您提供ForeignKeys.java参考,解决您在ForeignKeys.java学习中工作中的难题,参考学习。
//$Id: ForeignKeys.java 9012 2006-01-10 18:54:11Z steveebersole $
package org.hibernate.engine;
import java.io.Serializable;
import org.hibernate.HibernateException;
import org.hibernate.TransientObjectException;
import org.hibernate.intercept.LazyPropertyInitializer;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer;
import org.hibernate.type.AbstractComponentType;
import org.hibernate.type.EntityType;
import org.hibernate.type.Type;
/**
* Algorithms related to foreign key constraint transparency
*
* @author Gavin King
*/
public final class ForeignKeys {
private ForeignKeys() {}
public static class Nullifier {
private final boolean isDelete;
private final boolean isEarlyInsert;
private final SessionImplementor session;
private final Object self;
public Nullifier(Object self, boolean isDelete, boolean isEarlyInsert, SessionImplementor session) {
this.isDelete = isDelete;
this.isEarlyInsert = isEarlyInsert;
this.session = session;
this.self = self;
}
/**
* Nullify all references to entities that have not yet
* been inserted in the database, where the foreign key
* points toward that entity
*/
public void nullifyTransientReferences(final Object[] values, final Type[] types)
throws HibernateException {
for ( int i = 0; i < types.length; i++ ) {
values[i] = nullifyTransientReferences( values[i], types[i] );
}
}
/**
* Return null if the argument is an "unsaved" entity (ie.
* one with no existing database row), or the input argument
* otherwise. This is how Hibernate avoids foreign key constraint
* violations.
*/
private Object nullifyTransientReferences(final Object value, final Type type)
throws HibernateException {
if ( value == null ) {
return null;
}
else if ( type.isEntityType() ) {
EntityType entityType = (EntityType) type;
if ( entityType.isOneToOne() ) {
return value;
}
else {
String entityName = entityType.getAssociatedEntityName();
return isNullifiable(entityName, value) ? null : value;
}
}
else if ( type.isAnyType() ) {
return isNullifiable(null, value) ? null : value;
}
else if ( type.isComponentType() ) {
AbstractComponentType actype = (AbstractComponentType) type;
Object[] subvalues = actype.getPropertyValues(value, session);
Type[] subtypes = actype.getSubtypes();
boolean substitute = false;
for ( int i = 0; i < subvalues.length; i++ ) {
Object replacement = nullifyTransientReferences( subvalues[i], subtypes[i] );
if ( replacement != subvalues[i] ) {
substitute = true;
subvalues[i] = replacement;
}
}
if (substitute) actype.setPropertyValues( value, subvalues, session.getEntityMode() );
return value;
}
else {
return value;
}
}
/**
* Determine if the object already exists in the database,
* using a "best guess"
*/
private boolean isNullifiable(final String entityName, Object object)
throws HibernateException {
if (object==LazyPropertyInitializer.UNFETCHED_PROPERTY) return false; //this is kinda the best we can do...
if ( object instanceof HibernateProxy ) {
// if its an uninitialized proxy it can't be transient
LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer();
if ( li.getImplementation(session) == null ) {
return false;
// ie. we never have to null out a reference to
// an uninitialized proxy
}
else {
//unwrap it
object