下面列出了org.hibernate.mapping.RootClass#setIdentifier ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void bindCompositeId(Element idNode, RootClass entity, Mappings mappings,
java.util.Map inheritedMetas) throws MappingException {
String propertyName = idNode.attributeValue( "name" );
Component id = new Component( entity );
entity.setIdentifier( id );
bindCompositeId( idNode, id, entity, propertyName, mappings, inheritedMetas );
if ( propertyName == null ) {
entity.setEmbeddedIdentifier( id.isEmbedded() );
if ( id.isEmbedded() ) {
// todo : what is the implication of this?
id.setDynamic( !entity.hasPojoRepresentation() );
/*
* Property prop = new Property(); prop.setName("id");
* prop.setPropertyAccessorName("embedded"); prop.setValue(id);
* entity.setIdentifierProperty(prop);
*/
}
}
else {
Property prop = new Property();
prop.setValue( id );
bindProperty( idNode, prop, mappings, inheritedMetas );
entity.setIdentifierProperty( prop );
}
makeIdentifier( idNode, id, mappings );
}
private Property bind(Property prop) {
if (isId) {
final RootClass rootClass = ( RootClass ) holder.getPersistentClass();
//if an xToMany, it as to be wrapped today.
//FIXME this pose a problem as the PK is the class instead of the associated class which is not really compliant with the spec
if ( isXToMany || entityBinder.wrapIdsInEmbeddedComponents() ) {
Component identifier = (Component) rootClass.getIdentifier();
if (identifier == null) {
identifier = AnnotationBinder.createComponent(
holder,
new PropertyPreloadedData(null, null, null),
true,
false,
buildingContext
);
rootClass.setIdentifier( identifier );
identifier.setNullValue( "undefined" );
rootClass.setEmbeddedIdentifier( true );
rootClass.setIdentifierMapper( identifier );
}
//FIXME is it good enough?
identifier.addProperty( prop );
}
else {
rootClass.setIdentifier( ( KeyValue ) getValue() );
if (embedded) {
rootClass.setEmbeddedIdentifier( true );
}
else {
rootClass.setIdentifierProperty( prop );
final org.hibernate.mapping.MappedSuperclass superclass = BinderHelper.getMappedSuperclassOrNull(
declaringClass,
inheritanceStatePerClass,
buildingContext
);
if (superclass != null) {
superclass.setDeclaredIdentifierProperty(prop);
}
else {
//we know the property is on the actual entity
rootClass.setDeclaredIdentifierProperty( prop );
}
}
}
}
else {
holder.addProperty( prop, columns, declaringClass );
}
return prop;
}
private void bindSimpleEntityIdentifier(
MappingDocument sourceDocument,
final EntityHierarchySourceImpl hierarchySource,
RootClass rootEntityDescriptor) {
final IdentifierSourceSimple idSource = (IdentifierSourceSimple) hierarchySource.getIdentifierSource();
final SimpleValue idValue = new SimpleValue(
sourceDocument,
rootEntityDescriptor.getTable()
);
rootEntityDescriptor.setIdentifier( idValue );
bindSimpleValueType(
sourceDocument,
idSource.getIdentifierAttributeSource().getTypeInformation(),
idValue
);
final String propertyName = idSource.getIdentifierAttributeSource().getName();
if ( propertyName == null || !rootEntityDescriptor.hasPojoRepresentation() ) {
if ( !idValue.isTypeSpecified() ) {
throw new MappingException(
"must specify an identifier type: " + rootEntityDescriptor.getEntityName(),
sourceDocument.getOrigin()
);
}
}
else {
idValue.setTypeUsingReflection( rootEntityDescriptor.getClassName(), propertyName );
}
relationalObjectBinder.bindColumnsAndFormulas(
sourceDocument,
( (RelationalValueSourceContainer) idSource.getIdentifierAttributeSource() ).getRelationalValueSources(),
idValue,
false,
new RelationalObjectBinder.ColumnNamingDelegate() {
@Override
public Identifier determineImplicitName(final LocalMetadataBuildingContext context) {
context.getBuildingOptions().getImplicitNamingStrategy().determineIdentifierColumnName(
new ImplicitIdentifierColumnNameSource() {
@Override
public EntityNaming getEntityNaming() {
return hierarchySource.getRoot().getEntityNamingSource();
}
@Override
public AttributePath getIdentifierAttributePath() {
return idSource.getIdentifierAttributeSource().getAttributePath();
}
@Override
public MetadataBuildingContext getBuildingContext() {
return context;
}
}
);
return database.toIdentifier( propertyName );
}
}
);
if ( propertyName != null ) {
Property prop = new Property();
prop.setValue( idValue );
bindProperty(
sourceDocument,
idSource.getIdentifierAttributeSource(),
prop
);
rootEntityDescriptor.setIdentifierProperty( prop );
rootEntityDescriptor.setDeclaredIdentifierProperty( prop );
}
makeIdentifier(
sourceDocument,
idSource.getIdentifierGeneratorDescriptor(),
idSource.getUnsavedValue(),
idValue
);
}
private void bindAggregatedCompositeEntityIdentifier(
MappingDocument mappingDocument,
EntityHierarchySourceImpl hierarchySource,
RootClass rootEntityDescriptor) {
// an aggregated composite-id is a composite-id that defines a singular
// (composite) attribute as part of the entity to represent the id.
final IdentifierSourceAggregatedComposite identifierSource
= (IdentifierSourceAggregatedComposite) hierarchySource.getIdentifierSource();
final Component cid = new Component( mappingDocument, rootEntityDescriptor );
cid.setKey( true );
rootEntityDescriptor.setIdentifier( cid );
final String idClassName = extractIdClassName( identifierSource );
final String idPropertyName = identifierSource.getIdentifierAttributeSource().getName();
final String pathPart = idPropertyName == null ? "<id>" : idPropertyName;
bindComponent(
mappingDocument,
hierarchySource.getRoot().getAttributeRoleBase().append( pathPart ).getFullPath(),
identifierSource.getEmbeddableSource(),
cid,
idClassName,
rootEntityDescriptor.getClassName(),
idPropertyName,
idClassName == null && idPropertyName == null,
identifierSource.getEmbeddableSource().isDynamic(),
identifierSource.getIdentifierAttributeSource().getXmlNodeName()
);
finishBindingCompositeIdentifier(
mappingDocument,
rootEntityDescriptor,
identifierSource,
cid,
idPropertyName
);
}
private void bindNonAggregatedCompositeEntityIdentifier(
MappingDocument mappingDocument,
EntityHierarchySourceImpl hierarchySource,
RootClass rootEntityDescriptor) {
final IdentifierSourceNonAggregatedComposite identifierSource
= (IdentifierSourceNonAggregatedComposite) hierarchySource.getIdentifierSource();
final Component cid = new Component( mappingDocument, rootEntityDescriptor );
cid.setKey( true );
rootEntityDescriptor.setIdentifier( cid );
final String idClassName = extractIdClassName( identifierSource );
bindComponent(
mappingDocument,
hierarchySource.getRoot().getAttributeRoleBase().append( "<id>" ).getFullPath(),
identifierSource.getEmbeddableSource(),
cid,
idClassName,
rootEntityDescriptor.getClassName(),
null,
idClassName == null,
false,
null
);
if ( idClassName != null ) {
// we also need to bind the "id mapper". ugh, terrible name. Basically we need to
// create a virtual (embedded) composite for the non-aggregated attributes on the entity
// itself.
final Component mapper = new Component( mappingDocument, rootEntityDescriptor );
bindComponent(
mappingDocument,
hierarchySource.getRoot().getAttributeRoleBase().append( ID_MAPPER_PATH_PART ).getFullPath(),
identifierSource.getEmbeddableSource(),
mapper,
rootEntityDescriptor.getClassName(),
null,
null,
true,
false,
null
);
rootEntityDescriptor.setIdentifierMapper(mapper);
Property property = new Property();
property.setName( PropertyPath.IDENTIFIER_MAPPER_PROPERTY );
property.setUpdateable( false );
property.setInsertable( false );
property.setValue( mapper );
property.setPropertyAccessorName( "embedded" );
rootEntityDescriptor.addProperty( property );
}
finishBindingCompositeIdentifier( mappingDocument, rootEntityDescriptor, identifierSource, cid, null );
}
private static void bindSimpleId(Element idNode, RootClass entity, Mappings mappings,
java.util.Map inheritedMetas) throws MappingException {
String propertyName = idNode.attributeValue( "name" );
SimpleValue id = new SimpleValue( entity.getTable() );
entity.setIdentifier( id );
// if ( propertyName == null || entity.getPojoRepresentation() == null ) {
// bindSimpleValue( idNode, id, false, RootClass.DEFAULT_IDENTIFIER_COLUMN_NAME, mappings );
// if ( !id.isTypeSpecified() ) {
// throw new MappingException( "must specify an identifier type: " + entity.getEntityName()
// );
// }
// }
// else {
// bindSimpleValue( idNode, id, false, propertyName, mappings );
// PojoRepresentation pojo = entity.getPojoRepresentation();
// id.setTypeUsingReflection( pojo.getClassName(), propertyName );
//
// Property prop = new Property();
// prop.setValue( id );
// bindProperty( idNode, prop, mappings, inheritedMetas );
// entity.setIdentifierProperty( prop );
// }
if ( propertyName == null ) {
bindSimpleValue( idNode, id, false, RootClass.DEFAULT_IDENTIFIER_COLUMN_NAME, mappings );
}
else {
bindSimpleValue( idNode, id, false, propertyName, mappings );
}
if ( propertyName == null || !entity.hasPojoRepresentation() ) {
if ( !id.isTypeSpecified() ) {
throw new MappingException( "must specify an identifier type: "
+ entity.getEntityName() );
}
}
else {
id.setTypeUsingReflection( entity.getClassName(), propertyName );
}
if ( propertyName != null ) {
Property prop = new Property();
prop.setValue( id );
bindProperty( idNode, prop, mappings, inheritedMetas );
entity.setIdentifierProperty( prop );
}
// TODO:
/*
* if ( id.getHibernateType().getReturnedClass().isArray() ) throw new MappingException(
* "illegal use of an array as an identifier (arrays don't reimplement equals)" );
*/
makeIdentifier( idNode, id, mappings );
}