下面列出了org.hibernate.type.EntityType#isReferenceToPrimaryKey ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected void initIdentifierPropertyPaths(
final String path,
final EntityType etype,
final String[] columns,
final String[] columnReaders,
final String[] columnReaderTemplates,
final Mapping factory) throws MappingException {
Type idtype = etype.getIdentifierOrUniqueKeyType( factory );
String idPropName = etype.getIdentifierOrUniqueKeyPropertyName( factory );
boolean hasNonIdentifierPropertyNamedId = hasNonIdentifierPropertyNamedId( etype, factory );
if ( etype.isReferenceToPrimaryKey() ) {
if ( !hasNonIdentifierPropertyNamedId ) {
String idpath1 = extendPath( path, EntityPersister.ENTITY_ID );
addPropertyPath( idpath1, idtype, columns, columnReaders, columnReaderTemplates, null, factory );
initPropertyPaths( idpath1, idtype, columns, columnReaders, columnReaderTemplates, null, factory );
}
}
if ( idPropName != null ) {
String idpath2 = extendPath( path, idPropName );
addPropertyPath( idpath2, idtype, columns, columnReaders, columnReaderTemplates, null, factory );
initPropertyPaths( idpath2, idtype, columns, columnReaders, columnReaderTemplates, null, factory );
}
}
/**
* Is the given property name a reference to the primary key of the associated
* entity construed by the given entity type?
* <p/>
* For example, consider a fragment like order.customer.id
* (where order is a from-element alias). Here, we'd have:
* propertyName = "id" AND
* owningType = ManyToOneType(Customer)
* and are being asked to determine whether "customer.id" is a reference
* to customer's PK...
*
* @param propertyName The name of the property to check.
* @param owningType The type represeting the entity "owning" the property
*
* @return True if propertyName references the entity's (owningType->associatedEntity)
* primary key; false otherwise.
*/
private boolean isReferenceToPrimaryKey(String propertyName, EntityType owningType) {
EntityPersister persister = getSessionFactoryHelper()
.getFactory()
.getEntityPersister( owningType.getAssociatedEntityName() );
if ( persister.getEntityMetamodel().hasNonIdentifierPropertyNamedId() ) {
// only the identifier property field name can be a reference to the associated entity's PK...
return propertyName.equals( persister.getIdentifierPropertyName() ) && owningType.isReferenceToPrimaryKey();
}
// here, we have two possibilities:
// 1) the property-name matches the explicitly identifier property name
// 2) the property-name matches the implicit 'id' property name
// the referenced node text is the special 'id'
if ( EntityPersister.ENTITY_ID.equals( propertyName ) ) {
return owningType.isReferenceToPrimaryKey();
}
String keyPropertyName = getSessionFactoryHelper().getIdentifierOrUniqueKeyPropertyName( owningType );
return keyPropertyName != null && keyPropertyName.equals( propertyName ) && owningType.isReferenceToPrimaryKey();
}
protected void initIdentifierPropertyPaths(
final String path,
final EntityType etype,
final String[] columns,
final Mapping factory) throws MappingException {
Type idtype = etype.getIdentifierOrUniqueKeyType( factory );
String idPropName = etype.getIdentifierOrUniqueKeyPropertyName(factory);
boolean hasNonIdentifierPropertyNamedId = hasNonIdentifierPropertyNamedId( etype, factory );
if ( etype.isReferenceToPrimaryKey() ) {
if ( !hasNonIdentifierPropertyNamedId ) {
String idpath1 = extendPath(path, EntityPersister.ENTITY_ID);
addPropertyPath(idpath1, idtype, columns, null);
initPropertyPaths(idpath1, idtype, columns, null, factory);
}
}
if (idPropName!=null) {
String idpath2 = extendPath(path, idPropName);
addPropertyPath(idpath2, idtype, columns, null);
initPropertyPaths(idpath2, idtype, columns, null, factory);
}
}
/**
* Is the given property name a reference to the primary key of the associated
* entity construed by the given entity type?
* <p/>
* For example, consider a fragment like order.customer.id
* (where order is a from-element alias). Here, we'd have:
* propertyName = "id" AND
* owningType = ManyToOneType(Customer)
* and are being asked to determine whether "customer.id" is a reference
* to customer's PK...
*
* @param propertyName The name of the property to check.
* @param owningType The type represeting the entity "owning" the property
* @return True if propertyName references the entity's (owningType->associatedEntity)
* primary key; false otherwise.
*/
private boolean isReferenceToPrimaryKey(String propertyName, EntityType owningType) {
EntityPersister persister = getSessionFactoryHelper()
.getFactory()
.getEntityPersister( owningType.getAssociatedEntityName() );
if ( persister.getEntityMetamodel().hasNonIdentifierPropertyNamedId() ) {
// only the identifier property field name can be a reference to the associated entity's PK...
return propertyName.equals( persister.getIdentifierPropertyName() ) && owningType.isReferenceToPrimaryKey();
}
else {
// here, we have two possibilities:
// 1) the property-name matches the explicitly identifier property name
// 2) the property-name matches the implicit 'id' property name
if ( EntityPersister.ENTITY_ID.equals( propertyName ) ) {
// the referenced node text is the special 'id'
return owningType.isReferenceToPrimaryKey();
}
else {
String keyPropertyName = getSessionFactoryHelper().getIdentifierOrUniqueKeyPropertyName( owningType );
return keyPropertyName != null && keyPropertyName.equals( propertyName ) && owningType.isReferenceToPrimaryKey();
}
}
}
private void dereferenceEntity(String propertyName, EntityType propertyType, QueryTranslatorImpl q)
throws QueryException {
//NOTE: we avoid joining to the next table if the named property is just the foreign key value
//if its "id"
boolean isIdShortcut = EntityPersister.ENTITY_ID.equals( propertyName ) &&
propertyType.isReferenceToPrimaryKey();
//or its the id property name
final String idPropertyName;
try {
idPropertyName = propertyType.getIdentifierOrUniqueKeyPropertyName( q.getFactory() );
}
catch ( MappingException me ) {
throw new QueryException( me );
}
boolean isNamedIdPropertyShortcut = idPropertyName != null
&& idPropertyName.equals( propertyName )
&& propertyType.isReferenceToPrimaryKey();
if ( isIdShortcut || isNamedIdPropertyShortcut ) {
// special shortcut for id properties, skip the join!
// this must only occur at the _end_ of a path expression
if ( componentPath.length() > 0 ) componentPath.append( '.' );
componentPath.append( propertyName );
}
else {
String entityClass = propertyType.getAssociatedEntityName();
String name = q.createNameFor( entityClass );
q.addType( name, entityClass );
addJoin( name, propertyType );
if ( propertyType.isOneToOne() ) oneToOneOwnerName = currentName;
ownerAssociationType = propertyType;
currentName = name;
currentProperty = propertyName;
q.addPathAliasAndJoin( path.substring( 0, path.toString().lastIndexOf( '.' ) ), name, joinSequence.copy() );
componentPath.setLength( 0 );
currentPropertyMapping = q.getEntityPersister( entityClass );
}
}
private void dereferenceEntity(String propertyName, EntityType propertyType, QueryTranslatorImpl q)
throws QueryException {
//NOTE: we avoid joining to the next table if the named property is just the foreign key value
//if its "id"
boolean isIdShortcut = EntityPersister.ENTITY_ID.equals( propertyName )
&& propertyType.isReferenceToPrimaryKey();
//or its the id property name
final String idPropertyName;
try {
idPropertyName = propertyType.getIdentifierOrUniqueKeyPropertyName( q.getFactory() );
}
catch ( MappingException me ) {
throw new QueryException( me );
}
boolean isNamedIdPropertyShortcut = idPropertyName != null
&& idPropertyName.equals( propertyName )
&& propertyType.isReferenceToPrimaryKey();
if ( isIdShortcut || isNamedIdPropertyShortcut ) {
// special shortcut for id properties, skip the join!
// this must only occur at the _end_ of a path expression
if ( componentPath.length() > 0 ) {
componentPath.append( '.' );
}
componentPath.append( propertyName );
}
else {
String entityClass = propertyType.getAssociatedEntityName();
String name = q.createNameFor( entityClass );
q.addType( name, entityClass );
addJoin( name, propertyType );
if ( propertyType.isOneToOne() ) {
oneToOneOwnerName = currentName;
}
ownerAssociationType = propertyType;
currentName = name;
currentProperty = propertyName;
q.addPathAliasAndJoin( path.substring( 0, path.toString().lastIndexOf( '.' ) ), name, joinSequence.copy() );
componentPath.setLength( 0 );
currentPropertyMapping = q.getEntityPersister( entityClass );
}
}