ForeignGeneratorpublic class ForeignGenerator extends Object implements Configurable, IdentifierGeneratorforeign
An Identifier generator that uses the value of the id property of an
associated object
One mapping parameter is required: property. |
Fields Summary |
---|
private String | propertyName | private String | entityName |
Methods Summary |
---|
public void | configure(org.hibernate.type.Type type, java.util.Properties params, org.hibernate.dialect.Dialect d)
propertyName = params.getProperty("property");
entityName = params.getProperty(ENTITY_NAME);
if (propertyName==null) throw new MappingException(
"param named \"property\" is required for foreign id generation strategy"
);
| public java.io.Serializable | generate(org.hibernate.engine.SessionImplementor sessionImplementor, java.lang.Object object)
Session session = (Session) sessionImplementor;
Object associatedObject = sessionImplementor.getFactory()
.getClassMetadata( entityName )
.getPropertyValue( object, propertyName, session.getEntityMode() );
if ( associatedObject == null ) {
throw new IdentifierGenerationException(
"attempted to assign id from null one-to-one property: " +
propertyName
);
}
EntityType type = (EntityType) sessionImplementor.getFactory()
.getClassMetadata( entityName )
.getPropertyType( propertyName );
Serializable id;
try {
id = ForeignKeys.getEntityIdentifierIfNotUnsaved(
type.getAssociatedEntityName(),
associatedObject,
sessionImplementor
);
}
catch (TransientObjectException toe) {
id = session.save( type.getAssociatedEntityName(), associatedObject );
}
if ( session.contains(object) ) {
//abort the save (the object is already saved by a circular cascade)
return IdentifierGeneratorFactory.SHORT_CIRCUIT_INDICATOR;
//throw new IdentifierGenerationException("save associated object first, or disable cascade for inverse association");
}
return id;
|
|