| Method from org.hibernate.cfg.BinderHelper Detail: |
public static void createSyntheticPropertyReference(Ejb3JoinColumn[] columns,
PersistentClass ownerEntity,
PersistentClass associatedEntity,
Value value,
boolean inverse,
ExtendedMappings mappings) {
//associated entity only used for more precise exception, yuk!
if ( columns[0].isImplicit() || StringHelper.isNotEmpty( columns[0].getMappedBy() ) ) return;
int fkEnum = Ejb3JoinColumn.checkReferencedColumnsType( columns, ownerEntity, mappings );
PersistentClass associatedClass = columns[0].getPropertyHolder() != null ?
columns[0].getPropertyHolder().getPersistentClass() :
null;
if ( Ejb3JoinColumn.NON_PK_REFERENCE == fkEnum ) {
/**
* Create a synthetic property to refer to including an
* embedded component value containing all the properties
* mapped to the referenced columns
* We need to shallow copy those properties to mark them
* as non insertable / non updatable
*/
StringBuilder propertyNameBuffer = new StringBuilder( "_" );
propertyNameBuffer.append( associatedClass.getEntityName().replace( '.", '_" ) );
propertyNameBuffer.append( "_" ).append( columns[0].getPropertyName() );
String syntheticPropertyName = propertyNameBuffer.toString();
//find properties associated to a certain column
Object columnOwner = findColumnOwner( ownerEntity, columns[0].getReferencedColumn(), mappings );
List< Property > properties = findPropertiesByColumns( columnOwner, columns, mappings );
//create an embeddable component
Property synthProp = null;
if ( properties != null ) {
//todo how about properties.size() == 1, this should be much simpler
Component embeddedComp = columnOwner instanceof PersistentClass ?
new Component( (PersistentClass) columnOwner ) :
new Component( (Join) columnOwner );
embeddedComp.setEmbedded( true );
embeddedComp.setNodeName( syntheticPropertyName );
embeddedComp.setComponentClassName( embeddedComp.getOwner().getClassName() );
for ( Property property : properties ) {
Property clone = BinderHelper.shallowCopy( property );
clone.setInsertable( false );
clone.setUpdateable( false );
clone.setNaturalIdentifier( false );
embeddedComp.addProperty( clone );
}
synthProp = new Property();
synthProp.setName( syntheticPropertyName );
synthProp.setNodeName(syntheticPropertyName);
synthProp.setPersistentClass( ownerEntity );
synthProp.setUpdateable( false );
synthProp.setInsertable( false );
synthProp.setValue( embeddedComp );
synthProp.setPropertyAccessorName( "embedded" );
ownerEntity.addProperty( synthProp );
//make it unique
TableBinder.createUniqueConstraint( embeddedComp );
}
else {
//TODO use a ToOne type doing a second select
StringBuilder columnsList = new StringBuilder();
columnsList.append( "referencedColumnNames(" );
for ( Ejb3JoinColumn column : columns ) {
columnsList.append( column.getReferencedColumn() ).append( ", " );
}
columnsList.setLength( columnsList.length() - 2 );
columnsList.append( ") " );
if ( associatedEntity != null ) {
//overidden destination
columnsList.append( "of " )
.append( associatedEntity.getEntityName() )
.append( "." )
.append( columns[0].getPropertyName() )
.append( " " );
}
else {
if ( columns[0].getPropertyHolder() != null ) {
columnsList.append( "of " )
.append( columns[0].getPropertyHolder().getEntityName() )
.append( "." )
.append( columns[0].getPropertyName() )
.append( " " );
}
}
columnsList.append( "referencing " )
.append( ownerEntity.getEntityName() )
.append( " not mapped to a single property" );
throw new AnnotationException( columnsList.toString() );
}
/**
* creating the property ref to the new synthetic property
*/
if ( value instanceof ToOne ) {
( (ToOne) value ).setReferencedPropertyName( syntheticPropertyName );
mappings.addUniquePropertyReference( ownerEntity.getEntityName(), syntheticPropertyName );
}
else if ( value instanceof Collection ) {
( (Collection) value ).setReferencedPropertyName( syntheticPropertyName );
//not unique because we could create a mtm wo association table
mappings.addPropertyReference( ownerEntity.getEntityName(), syntheticPropertyName );
}
else {
throw new AssertionFailure(
"Do a property ref on an unexpected Value type: "
+ value.getClass().getName()
);
}
mappings.addPropertyReferencedAssociation(
( inverse ? "inverse__" : "" ) + associatedClass.getEntityName(),
columns[0].getPropertyName(),
syntheticPropertyName
);
}
}
|
public static Object findColumnOwner(PersistentClass persistentClass,
String columnName,
ExtendedMappings mappings) {
if ( StringHelper.isEmpty( columnName ) ) {
return persistentClass; //shortcut for implicit referenced column names
}
PersistentClass current = persistentClass;
Object result = null;
boolean found = false;
do {
result = current;
Table currentTable = current.getTable();
try {
mappings.getPhysicalColumnName( columnName, currentTable );
found = true;
}
catch (MappingException me) {
//swallow it
}
Iterator joins = current.getJoinIterator();
while ( ! found && joins.hasNext() ) {
result = joins.next();
currentTable = ( (Join) result ).getTable();
try {
mappings.getPhysicalColumnName( columnName, currentTable );
found = true;
}
catch (MappingException me) {
//swallow it
}
}
current = current.getSuperclass();
}
while ( !found && current != null );
return found ? result : null;
}
Find the column owner (ie PersistentClass or Join) of columnName.
If columnName is null or empty, persistentClass is returned |
public static Property findPropertyByName(PersistentClass associatedClass,
String propertyName) {
Property property = null;
Property idProperty = associatedClass.getIdentifierProperty();
String idName = idProperty != null ? idProperty.getName() : null;
try {
if ( propertyName == null
|| propertyName.length() == 0
|| propertyName.equals( idName ) ) {
//default to id
property = idProperty;
}
else {
if ( propertyName.indexOf( idName + "." ) == 0 ) {
property = idProperty;
propertyName = propertyName.substring( idName.length() + 1 );
}
StringTokenizer st = new StringTokenizer( propertyName, ".", false );
while ( st.hasMoreElements() ) {
String element = (String) st.nextElement();
if ( property == null ) {
property = associatedClass.getProperty( element );
}
else {
if ( ! property.isComposite() ) return null;
property = ( (Component) property.getValue() ).getProperty( element );
}
}
}
}
catch (MappingException e) {
try {
//if we do not find it try to check the identifier mapper
if ( associatedClass.getIdentifierMapper() == null ) return null;
StringTokenizer st = new StringTokenizer( propertyName, ".", false );
while ( st.hasMoreElements() ) {
String element = (String) st.nextElement();
if ( property == null ) {
property = associatedClass.getIdentifierMapper().getProperty( element );
}
else {
if ( ! property.isComposite() ) return null;
property = ( (Component) property.getValue() ).getProperty( element );
}
}
}
catch (MappingException ee) {
return null;
}
}
return property;
}
Retrieve the property by path in a recursive way, including IndetifierProperty in the loop
If propertyName is null or empty, the IdentifierProperty is returned |
public static String getRelativePath(PropertyHolder propertyHolder,
String propertyName) {
if ( propertyHolder == null ) return propertyName;
String path = propertyHolder.getPath();
String entityName = propertyHolder.getPersistentClass().getEntityName();
if ( path.length() == entityName.length() ) {
return propertyName;
}
else {
return StringHelper.qualify( path.substring( entityName.length() + 1 ), propertyName );
}
}
|
public static boolean isDefault(String annotationString) {
return annotationString != null && annotationString.length() == 0;
//equivalent to (but faster) ANNOTATION_STRING_DEFAULT.equals( annotationString );
}
|
public static void makeIdGenerator(SimpleValue id,
String generatorType,
String generatorName,
ExtendedMappings mappings,
Map localGenerators) {
Table table = id.getTable();
table.setIdentifierValue( id );
//generator settings
id.setIdentifierGeneratorStrategy( generatorType );
Properties params = new Properties();
//always settable
params.setProperty(
PersistentIdentifierGenerator.TABLE, table.getName()
);
if ( id.getColumnSpan() == 1 ) {
params.setProperty(
PersistentIdentifierGenerator.PK,
( (org.hibernate.mapping.Column) id.getColumnIterator().next() ).getName()
);
}
if ( ! isDefault( generatorName ) ) {
//we have a named generator
IdGenerator gen = mappings.getGenerator( generatorName, localGenerators );
if ( gen == null ) {
throw new AnnotationException( "Unknown Id.generator: " + generatorName );
}
//This is quite vague in the spec but a generator could override the generate choice
String identifierGeneratorStrategy = gen.getIdentifierGeneratorStrategy();
//yuk! this is a hack not to override 'AUTO' even if generator is set
final boolean avoidOverriding =
identifierGeneratorStrategy.equals( "identity" )
|| identifierGeneratorStrategy.equals( "seqhilo" )
|| identifierGeneratorStrategy.equals( MultipleHiLoPerTableGenerator.class.getName() );
if ( generatorType == null || ! avoidOverriding ) {
id.setIdentifierGeneratorStrategy( identifierGeneratorStrategy );
}
//checkIfMatchingGenerator(gen, generatorType, generatorName);
Iterator genParams = gen.getParams().entrySet().iterator();
while ( genParams.hasNext() ) {
Map.Entry elt = (Map.Entry) genParams.next();
params.setProperty( (String) elt.getKey(), (String) elt.getValue() );
}
}
if ( "assigned".equals( generatorType ) ) id.setNullValue( "undefined" );
id.setIdentifierGeneratorProperties( params );
}
apply an id generator to a SimpleValue |
public static Property shallowCopy(Property property) {
Property clone = new Property();
clone.setCascade( property.getCascade() );
clone.setInsertable( property.isInsertable() );
clone.setLazy( property.isLazy() );
clone.setName( property.getName() );
clone.setNodeName( property.getNodeName() );
clone.setNaturalIdentifier( property.isNaturalIdentifier() );
clone.setOptimisticLocked( property.isOptimisticLocked() );
clone.setOptional( property.isOptional() );
clone.setPersistentClass( property.getPersistentClass() );
clone.setPropertyAccessorName( property.getPropertyAccessorName() );
clone.setSelectable( property.isSelectable() );
clone.setUpdateable( property.isUpdateable() );
clone.setValue( property.getValue() );
return clone;
}
create a property copy reusing the same value |