use of org.hibernate.mapping.KeyValue in project hibernate-orm by hibernate.
the class ToOneFkSecondPass method isInPrimaryKey.
@Override
public boolean isInPrimaryKey() {
if (entityClassName == null)
return false;
final PersistentClass persistentClass = buildingContext.getMetadataCollector().getEntityBinding(entityClassName);
Property property = persistentClass.getIdentifierProperty();
if (path == null) {
return false;
} else if (property != null) {
// try explicit identifier property
return path.startsWith(property.getName() + ".");
} else {
// embedded property starts their path with 'id.' See PropertyPreloadedData( ) use when idClass != null in AnnotationSourceProcessor
if (path.startsWith("id.")) {
KeyValue valueIdentifier = persistentClass.getIdentifier();
String localPath = path.substring(3);
if (valueIdentifier instanceof Component) {
Iterator it = ((Component) valueIdentifier).getPropertyIterator();
while (it.hasNext()) {
Property idProperty = (Property) it.next();
if (localPath.startsWith(idProperty.getName()))
return true;
}
}
}
}
return false;
}
use of org.hibernate.mapping.KeyValue in project hibernate-orm by hibernate.
the class CollectionBinder method bindManytoManyInverseFk.
/**
* bind the inverse FK of a ManyToMany
* If we are in a mappedBy case, read the columns from the associated
* collection element
* Otherwise delegates to the usual algorithm
*/
public static void bindManytoManyInverseFk(PersistentClass referencedEntity, Ejb3JoinColumn[] columns, SimpleValue value, boolean unique, MetadataBuildingContext buildingContext) {
final String mappedBy = columns[0].getMappedBy();
if (StringHelper.isNotEmpty(mappedBy)) {
final Property property = referencedEntity.getRecursiveProperty(mappedBy);
Iterator mappedByColumns;
if (property.getValue() instanceof Collection) {
mappedByColumns = ((Collection) property.getValue()).getKey().getColumnIterator();
} else {
// find the appropriate reference key, can be in a join
Iterator joinsIt = referencedEntity.getJoinIterator();
KeyValue key = null;
while (joinsIt.hasNext()) {
Join join = (Join) joinsIt.next();
if (join.containsProperty(property)) {
key = join.getKey();
break;
}
}
if (key == null)
key = property.getPersistentClass().getIdentifier();
mappedByColumns = key.getColumnIterator();
}
while (mappedByColumns.hasNext()) {
Column column = (Column) mappedByColumns.next();
columns[0].linkValueUsingAColumnCopy(column, value);
}
String referencedPropertyName = buildingContext.getMetadataCollector().getPropertyReferencedAssociation("inverse__" + referencedEntity.getEntityName(), mappedBy);
if (referencedPropertyName != null) {
// TODO always a many to one?
((ManyToOne) value).setReferencedPropertyName(referencedPropertyName);
buildingContext.getMetadataCollector().addUniquePropertyReference(referencedEntity.getEntityName(), referencedPropertyName);
}
((ManyToOne) value).setReferenceToPrimaryKey(referencedPropertyName == null);
value.createForeignKey();
} else {
BinderHelper.createSyntheticPropertyReference(columns, referencedEntity, null, value, true, buildingContext);
TableBinder.bindFk(referencedEntity, null, columns, value, unique, buildingContext);
}
}
use of org.hibernate.mapping.KeyValue in project hibernate-orm by hibernate.
the class CollectionBinder method buildCollectionKey.
private static SimpleValue buildCollectionKey(Collection collValue, Ejb3JoinColumn[] joinColumns, boolean cascadeDeleteEnabled, XProperty property, PropertyHolder propertyHolder, MetadataBuildingContext buildingContext) {
// binding key reference using column
KeyValue keyVal;
// has to do that here because the referencedProperty creation happens in a FKSecondPass for Many to one yuk!
if (joinColumns.length > 0 && StringHelper.isNotEmpty(joinColumns[0].getMappedBy())) {
String entityName = joinColumns[0].getManyToManyOwnerSideEntityName() != null ? "inverse__" + joinColumns[0].getManyToManyOwnerSideEntityName() : joinColumns[0].getPropertyHolder().getEntityName();
String propRef = buildingContext.getMetadataCollector().getPropertyReferencedAssociation(entityName, joinColumns[0].getMappedBy());
if (propRef != null) {
collValue.setReferencedPropertyName(propRef);
buildingContext.getMetadataCollector().addPropertyReference(collValue.getOwnerEntityName(), propRef);
}
}
String propRef = collValue.getReferencedPropertyName();
if (propRef == null) {
keyVal = collValue.getOwner().getIdentifier();
} else {
keyVal = (KeyValue) collValue.getOwner().getReferencedProperty(propRef).getValue();
}
DependantValue key = new DependantValue(buildingContext, collValue.getCollectionTable(), keyVal);
key.setTypeName(null);
Ejb3Column.checkPropertyConsistency(joinColumns, collValue.getOwnerEntityName());
key.setNullable(joinColumns.length == 0 || joinColumns[0].isNullable());
key.setUpdateable(joinColumns.length == 0 || joinColumns[0].isUpdatable());
key.setCascadeDeleteEnabled(cascadeDeleteEnabled);
collValue.setKey(key);
if (property != null) {
final ForeignKey fk = property.getAnnotation(ForeignKey.class);
if (fk != null && !BinderHelper.isEmptyAnnotationValue(fk.name())) {
key.setForeignKeyName(fk.name());
} else {
final CollectionTable collectionTableAnn = property.getAnnotation(CollectionTable.class);
if (collectionTableAnn != null) {
if (collectionTableAnn.foreignKey().value() == ConstraintMode.NO_CONSTRAINT) {
key.setForeignKeyName("none");
} else {
key.setForeignKeyName(StringHelper.nullIfEmpty(collectionTableAnn.foreignKey().name()));
key.setForeignKeyDefinition(StringHelper.nullIfEmpty(collectionTableAnn.foreignKey().foreignKeyDefinition()));
if (key.getForeignKeyName() == null && key.getForeignKeyDefinition() == null && collectionTableAnn.joinColumns().length == 1) {
JoinColumn joinColumn = collectionTableAnn.joinColumns()[0];
key.setForeignKeyName(StringHelper.nullIfEmpty(joinColumn.foreignKey().name()));
key.setForeignKeyDefinition(StringHelper.nullIfEmpty(joinColumn.foreignKey().foreignKeyDefinition()));
}
}
} else {
final JoinTable joinTableAnn = property.getAnnotation(JoinTable.class);
if (joinTableAnn != null) {
String foreignKeyName = joinTableAnn.foreignKey().name();
String foreignKeyDefinition = joinTableAnn.foreignKey().foreignKeyDefinition();
ConstraintMode foreignKeyValue = joinTableAnn.foreignKey().value();
if (joinTableAnn.joinColumns().length != 0) {
final JoinColumn joinColumnAnn = joinTableAnn.joinColumns()[0];
if ("".equals(foreignKeyName)) {
foreignKeyName = joinColumnAnn.foreignKey().name();
foreignKeyDefinition = joinColumnAnn.foreignKey().foreignKeyDefinition();
}
if (foreignKeyValue != ConstraintMode.NO_CONSTRAINT) {
foreignKeyValue = joinColumnAnn.foreignKey().value();
}
}
if (foreignKeyValue == ConstraintMode.NO_CONSTRAINT) {
key.setForeignKeyName("none");
} else {
key.setForeignKeyName(StringHelper.nullIfEmpty(foreignKeyName));
key.setForeignKeyDefinition(StringHelper.nullIfEmpty(foreignKeyDefinition));
}
} else {
final javax.persistence.ForeignKey fkOverride = propertyHolder.getOverriddenForeignKey(StringHelper.qualify(propertyHolder.getPath(), property.getName()));
if (fkOverride != null && fkOverride.value() == ConstraintMode.NO_CONSTRAINT) {
key.setForeignKeyName("none");
} else if (fkOverride != null) {
key.setForeignKeyName(StringHelper.nullIfEmpty(fkOverride.name()));
key.setForeignKeyDefinition(StringHelper.nullIfEmpty(fkOverride.foreignKeyDefinition()));
} else {
final JoinColumn joinColumnAnn = property.getAnnotation(JoinColumn.class);
if (joinColumnAnn != null) {
if (joinColumnAnn.foreignKey().value() == ConstraintMode.NO_CONSTRAINT) {
key.setForeignKeyName("none");
} else {
key.setForeignKeyName(StringHelper.nullIfEmpty(joinColumnAnn.foreignKey().name()));
key.setForeignKeyDefinition(StringHelper.nullIfEmpty(joinColumnAnn.foreignKey().foreignKeyDefinition()));
}
}
}
}
}
}
}
return key;
}
use of org.hibernate.mapping.KeyValue in project hibernate-orm by hibernate.
the class PropertyFactory method buildVersionProperty.
/**
* Generates a VersionProperty representation for an entity mapping given its
* version mapping Property.
*
* @param property The version mapping Property.
* @param lazyAvailable Is property lazy loading currently available.
*
* @return The appropriate VersionProperty definition.
*/
public static VersionProperty buildVersionProperty(EntityPersister persister, SessionFactoryImplementor sessionFactory, int attributeNumber, Property property, boolean lazyAvailable) {
String mappedUnsavedValue = ((KeyValue) property.getValue()).getNullValue();
VersionValue unsavedValue = UnsavedValueFactory.getUnsavedVersionValue(mappedUnsavedValue, getGetter(property), (VersionType) property.getType(), getConstructor(property.getPersistentClass()));
boolean lazy = lazyAvailable && property.isLazy();
return new VersionProperty(persister, sessionFactory, attributeNumber, property.getName(), property.getValue().getType(), new BaselineAttributeInformation.Builder().setLazy(lazy).setInsertable(property.isInsertable()).setUpdateable(property.isUpdateable()).setValueGenerationStrategy(property.getValueGenerationStrategy()).setNullable(property.isOptional()).setDirtyCheckable(property.isUpdateable() && !lazy).setVersionable(property.isOptimisticLocked()).setCascadeStyle(property.getCascadeStyle()).createInformation(), unsavedValue);
}
use of org.hibernate.mapping.KeyValue in project jbosstools-hibernate by jbosstools.
the class ValueFacadeTest method testSetKey.
@Test
public void testSetKey() {
KeyValue keyValueTarget = new SimpleValue();
IValue keyValueFacade = FACADE_FACTORY.createValue(keyValueTarget);
Collection collectionTarget = new Bag(null);
IValue collectionFacade = FACADE_FACTORY.createValue(collectionTarget);
Assert.assertNull(collectionTarget.getKey());
collectionFacade.setKey(keyValueFacade);
Assert.assertSame(keyValueTarget, collectionTarget.getKey());
}
Aggregations