Top Description Inners Constructors Methods
org.hibernate.engine.internal

public final Class ForeignKeys

extends Object
Class Inheritance
Imports
java.io.Serializable, org.hibernate.EntityMode, .HibernateException, .TransientObjectException, org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer, org.hibernate.engine.spi.EntityEntry, .SelfDirtinessTracker, .SharedSessionContractImplementor, org.hibernate.internal.util.StringHelper, org.hibernate.persister.entity.EntityPersister, org.hibernate.proxy.HibernateProxy, .LazyInitializer, org.hibernate.type.CompositeType, .EntityType, .Type

Algorithms related to foreign key constraint transparency
Author
Gavin King

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
ForeignKeys.Nullifier

Delegate for handling nullifying ("null"ing-out) non-cascaded associations

Constructor Summary

AccessConstructor and Description
private
ForeignKeys()

Disallow instantiation

Method Summary

Modifier and TypeMethod and Description
private static void
collectNonNullableTransientEntities(ForeignKeys.Nullifier nullifier, Object value, String propertyName, Type type, boolean isNullable, SharedSessionContractImplementor session, NonNullableTransientDependencies nonNullableTransientEntities)

public static NonNullableTransientDependencies

Returns:

the transient unsaved entity dependencies that are non-nullable, or null if there are none.
findNonNullableTransientEntities
(String
- the entity name
entityName
,
Object
- the entity instance
entity
,
Object[]
- insertable properties of the object (including backrefs), possibly with substitutions
values
,
boolean
- true if the entity needs to be executed as soon as possible (e.g., to generate an ID)
isEarlyInsert
,
SharedSessionContractImplementor
- the session
session
)

Find all non-nullable references to entities that have not yet been inserted in the database, where the foreign key is a reference to an unsaved transient entity.

public static Serializable

Returns:

The identifier
getEntityIdentifierIfNotUnsaved
(final String
The name of the entity
entityName
,
final Object
The entity instance
object
,
final SharedSessionContractImplementor
The session
session
)

Return the identifier of the persistent or transient object, or throw an exception if the instance is "unsaved"

Used by OneToOneType and ManyToOneType to determine what id value should be used for an object that may or may not be associated with the session.

public static boolean

Returns:

true if the given entity is not transient (meaning it is either detached/persistent)
isNotTransient
(String
The name of the entity
entityName
,
Object
The entity instance
entity
,
Boolean
The assumed return value, if avoiding database hit is desired
assumed
,
SharedSessionContractImplementor
The session
session
)

Is this instance persistent or detached?

public static boolean

Returns:

true if the given entity is transient (unsaved)
isTransient
(String
The name of the entity
entityName
,
Object
The entity instance
entity
,
Boolean
The assumed return value, if avoiding database hit is desired
assumed
,
SharedSessionContractImplementor
The session
session
)

Is this instance, which we know is not persistent, actually transient?

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ForeignKeysback to summary
private ForeignKeys()

Disallow instantiation

Method Detail

collectNonNullableTransientEntitiesback to summary
private static void collectNonNullableTransientEntities(ForeignKeys.Nullifier nullifier, Object value, String propertyName, Type type, boolean isNullable, SharedSessionContractImplementor session, NonNullableTransientDependencies nonNullableTransientEntities)
findNonNullableTransientEntitiesback to summary
public static NonNullableTransientDependencies findNonNullableTransientEntities(String entityName, Object entity, Object[] values, boolean isEarlyInsert, SharedSessionContractImplementor session)

Find all non-nullable references to entities that have not yet been inserted in the database, where the foreign key is a reference to an unsaved transient entity. .

Parameters
entityName:String

- the entity name

entity:Object

- the entity instance

values:Object[]

- insertable properties of the object (including backrefs), possibly with substitutions

isEarlyInsert:boolean

- true if the entity needs to be executed as soon as possible (e.g., to generate an ID)

session:SharedSessionContractImplementor

- the session

Returns:NonNullableTransientDependencies

the transient unsaved entity dependencies that are non-nullable, or null if there are none.

getEntityIdentifierIfNotUnsavedback to summary
public static Serializable getEntityIdentifierIfNotUnsaved(final String entityName, final Object object, final SharedSessionContractImplementor session) throws TransientObjectException

Return the identifier of the persistent or transient object, or throw an exception if the instance is "unsaved"

Used by OneToOneType and ManyToOneType to determine what id value should be used for an object that may or may not be associated with the session. This does a "best guess" using any/all info available to use (not just the EntityEntry).

Parameters
entityName:String

The name of the entity

object:Object

The entity instance

session:SharedSessionContractImplementor

The session

Returns:Serializable

The identifier

Exceptions
TransientObjectException:
if the entity is transient (does not yet have an identifier)
isNotTransientback to summary
public static boolean isNotTransient(String entityName, Object entity, Boolean assumed, SharedSessionContractImplementor session)

Is this instance persistent or detached?

If assumed is non-null, don't hit the database to make the determination, instead assume that value; the client code must be prepared to "recover" in the case that this assumed result is incorrect.

Parameters
entityName:String

The name of the entity

entity:Object

The entity instance

assumed:Boolean

The assumed return value, if avoiding database hit is desired

session:SharedSessionContractImplementor

The session

Returns:boolean

true if the given entity is not transient (meaning it is either detached/persistent)

Annotations
@SuppressWarnings:SimplifiableIfStatement
isTransientback to summary
public static boolean isTransient(String entityName, Object entity, Boolean assumed, SharedSessionContractImplementor session)

Is this instance, which we know is not persistent, actually transient?

If assumed is non-null, don't hit the database to make the determination, instead assume that value; the client code must be prepared to "recover" in the case that this assumed result is incorrect.

Parameters
entityName:String

The name of the entity

entity:Object

The entity instance

assumed:Boolean

The assumed return value, if avoiding database hit is desired

session:SharedSessionContractImplementor

The session

Returns:boolean

true if the given entity is transient (unsaved)

org.hibernate.engine.internal back to summary

public Class ForeignKeys.Nullifier

extends Object
Class Inheritance

Delegate for handling nullifying ("null"ing-out) non-cascaded associations

Field Summary

Modifier and TypeField and Description
private final boolean
private final boolean
private final EntityPersister
private final Object
private final SharedSessionContractImplementor

Constructor Summary

AccessConstructor and Description
public
Nullifier(final Object
The entity
self
,
final boolean
Are we in the middle of a delete action?
isDelete
,
final boolean
Is this an early insert (INSERT generated id strategy)?
isEarlyInsert
,
final SharedSessionContractImplementor
The session
session
,
final EntityPersister
The EntityPersister for self
persister
)

Constructs a Nullifier

Method Summary

Modifier and TypeMethod and Description
private Object
initializeIfNecessary(final Object value, final String propertyName, final Type type)

private boolean
isNullifiable(final String
The name of the entity
entityName
,
Object
The entity instance
object
)

Determine if the object already exists in the database, using a "best guess"

public void
nullifyTransientReferences(final Object[]
The entity attribute values
values
)

Nullify all references to entities that have not yet been inserted in the database, where the foreign key points toward that entity.

private Object

Returns:

null if the argument is an unsaved entity; otherwise return the argument.
nullifyTransientReferences
(final Object
An entity attribute value
value
,
final String
An entity attribute name
propertyName
,
final Type
An entity attribute type
type
)

Return null if the argument is an "unsaved" entity (ie.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

isDeleteback to summary
private final boolean isDelete
isEarlyInsertback to summary
private final boolean isEarlyInsert
persisterback to summary
private final EntityPersister persister
selfback to summary
private final Object self
sessionback to summary
private final SharedSessionContractImplementor session

Constructor Detail

Nullifierback to summary
public Nullifier(final Object self, final boolean isDelete, final boolean isEarlyInsert, final SharedSessionContractImplementor session, final EntityPersister persister)

Constructs a Nullifier

Parameters
self:Object

The entity

isDelete:boolean

Are we in the middle of a delete action?

isEarlyInsert:boolean

Is this an early insert (INSERT generated id strategy)?

session:SharedSessionContractImplementor

The session

persister:EntityPersister

The EntityPersister for self

Method Detail

initializeIfNecessaryback to summary
private Object initializeIfNecessary(final Object value, final String propertyName, final Type type)
isNullifiableback to summary
private boolean isNullifiable(final String entityName, Object object) throws HibernateException

Determine if the object already exists in the database, using a "best guess"

Parameters
entityName:String

The name of the entity

object:Object

The entity instance

nullifyTransientReferencesback to summary
public void nullifyTransientReferences(final Object[] values)

Nullify all references to entities that have not yet been inserted in the database, where the foreign key points toward that entity.

Parameters
values:Object[]

The entity attribute values

nullifyTransientReferencesback to summary
private Object nullifyTransientReferences(final Object value, final String propertyName, final Type type)

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.

Parameters
value:Object

An entity attribute value

propertyName:String

An entity attribute name

type:Type

An entity attribute type

Returns:Object

null if the argument is an unsaved entity; otherwise return the argument.