use of org.hibernate.mapping.SimpleValue in project hibernate-orm by hibernate.
the class ListBinder method bindIndex.
private void bindIndex(final MetadataBuildingContext buildingContext) {
if (!indexColumn.isImplicit()) {
PropertyHolder valueHolder = PropertyHolderBuilder.buildPropertyHolder(this.collection, StringHelper.qualify(this.collection.getRole(), "key"), null, null, propertyHolder, getBuildingContext());
List list = (List) this.collection;
if (!list.isOneToMany())
indexColumn.forceNotNull();
indexColumn.setPropertyHolder(valueHolder);
SimpleValueBinder value = new SimpleValueBinder();
value.setColumns(new Ejb3Column[] { indexColumn });
value.setExplicitType("integer");
value.setBuildingContext(getBuildingContext());
SimpleValue indexValue = value.make();
indexColumn.linkWithValue(indexValue);
list.setIndex(indexValue);
list.setBaseIndex(indexColumn.getBase());
if (list.isOneToMany() && !list.getKey().isNullable() && !list.isInverse()) {
String entityName = ((OneToMany) list.getElement()).getReferencedEntityName();
PersistentClass referenced = buildingContext.getMetadataCollector().getEntityBinding(entityName);
IndexBackref ib = new IndexBackref();
ib.setName('_' + propertyName + "IndexBackref");
ib.setUpdateable(false);
ib.setSelectable(false);
ib.setCollectionRole(list.getRole());
ib.setEntityName(list.getOwner().getEntityName());
ib.setValue(list.getIndex());
referenced.addProperty(ib);
}
} else {
Collection coll = this.collection;
throw new AnnotationException("List/array has to be annotated with an @OrderColumn (or @IndexColumn): " + coll.getRole());
}
}
use of org.hibernate.mapping.SimpleValue in project hibernate-orm by hibernate.
the class MapBinder method createFormulatedValue.
protected Value createFormulatedValue(Value value, Collection collection, String targetPropertyName, PersistentClass associatedClass, PersistentClass targetPropertyPersistentClass, MetadataBuildingContext buildingContext) {
Value element = collection.getElement();
String fromAndWhere = null;
if (!(element instanceof OneToMany)) {
String referencedPropertyName = null;
if (element instanceof ToOne) {
referencedPropertyName = ((ToOne) element).getReferencedPropertyName();
} else if (element instanceof DependantValue) {
//TODO this never happen I think
if (propertyName != null) {
referencedPropertyName = collection.getReferencedPropertyName();
} else {
throw new AnnotationException("SecondaryTable JoinColumn cannot reference a non primary key");
}
}
Iterator<Selectable> referencedEntityColumns;
if (referencedPropertyName == null) {
referencedEntityColumns = associatedClass.getIdentifier().getColumnIterator();
} else {
Property referencedProperty = associatedClass.getRecursiveProperty(referencedPropertyName);
referencedEntityColumns = referencedProperty.getColumnIterator();
}
fromAndWhere = getFromAndWhereFormula(associatedClass.getTable().getName(), element.getColumnIterator(), referencedEntityColumns);
} else {
// HHH-11005 - only if we are OneToMany and location of map key property is at a different level, need to add a select
if (!associatedClass.equals(targetPropertyPersistentClass)) {
fromAndWhere = getFromAndWhereFormula(targetPropertyPersistentClass.getTable().getQualifiedTableName().toString(), element.getColumnIterator(), associatedClass.getIdentifier().getColumnIterator());
}
}
if (value instanceof Component) {
Component component = (Component) value;
Iterator properties = component.getPropertyIterator();
Component indexComponent = new Component(getBuildingContext().getMetadataCollector(), collection);
indexComponent.setComponentClassName(component.getComponentClassName());
while (properties.hasNext()) {
Property current = (Property) properties.next();
Property newProperty = new Property();
newProperty.setCascade(current.getCascade());
newProperty.setValueGenerationStrategy(current.getValueGenerationStrategy());
newProperty.setInsertable(false);
newProperty.setUpdateable(false);
newProperty.setMetaAttributes(current.getMetaAttributes());
newProperty.setName(current.getName());
newProperty.setNaturalIdentifier(false);
//newProperty.setOptimisticLocked( false );
newProperty.setOptional(false);
newProperty.setPersistentClass(current.getPersistentClass());
newProperty.setPropertyAccessorName(current.getPropertyAccessorName());
newProperty.setSelectable(current.isSelectable());
newProperty.setValue(createFormulatedValue(current.getValue(), collection, targetPropertyName, associatedClass, associatedClass, buildingContext));
indexComponent.addProperty(newProperty);
}
return indexComponent;
} else if (value instanceof SimpleValue) {
SimpleValue sourceValue = (SimpleValue) value;
SimpleValue targetValue;
if (value instanceof ManyToOne) {
ManyToOne sourceManyToOne = (ManyToOne) sourceValue;
ManyToOne targetManyToOne = new ManyToOne(getBuildingContext().getMetadataCollector(), collection.getCollectionTable());
targetManyToOne.setFetchMode(FetchMode.DEFAULT);
targetManyToOne.setLazy(true);
//targetValue.setIgnoreNotFound( ); does not make sense for a map key
targetManyToOne.setReferencedEntityName(sourceManyToOne.getReferencedEntityName());
targetValue = targetManyToOne;
} else {
targetValue = new SimpleValue(getBuildingContext().getMetadataCollector(), collection.getCollectionTable());
targetValue.copyTypeFrom(sourceValue);
}
Iterator columns = sourceValue.getColumnIterator();
Random random = new Random();
while (columns.hasNext()) {
Object current = columns.next();
Formula formula = new Formula();
String formulaString;
if (current instanceof Column) {
formulaString = ((Column) current).getQuotedName();
} else if (current instanceof Formula) {
formulaString = ((Formula) current).getFormula();
} else {
throw new AssertionFailure("Unknown element in column iterator: " + current.getClass());
}
if (fromAndWhere != null) {
formulaString = Template.renderWhereStringTemplate(formulaString, "$alias$", new HSQLDialect());
formulaString = "(select " + formulaString + fromAndWhere + ")";
formulaString = StringHelper.replace(formulaString, "$alias$", "a" + random.nextInt(16));
}
formula.setFormula(formulaString);
targetValue.addFormula(formula);
}
return targetValue;
} else {
throw new AssertionFailure("Unknown type encounters for map key: " + value.getClass());
}
}
use of org.hibernate.mapping.SimpleValue in project hibernate-orm by hibernate.
the class PropertyBinder method makePropertyAndValue.
private Property makePropertyAndValue() {
validateBind();
LOG.debugf("MetadataSourceProcessor property %s with lazy=%s", name, lazy);
final String containerClassName = holder.getClassName();
holder.startingProperty(property);
simpleValueBinder = new SimpleValueBinder();
simpleValueBinder.setBuildingContext(buildingContext);
simpleValueBinder.setPropertyName(name);
simpleValueBinder.setReturnedClassName(returnedClassName);
simpleValueBinder.setColumns(columns);
simpleValueBinder.setPersistentClassName(containerClassName);
simpleValueBinder.setType(property, returnedClass, containerClassName, holder.resolveAttributeConverterDescriptor(property));
simpleValueBinder.setReferencedEntityName(referencedEntityName);
simpleValueBinder.setAccessType(accessType);
SimpleValue propertyValue = simpleValueBinder.make();
setValue(propertyValue);
return makeProperty();
}
use of org.hibernate.mapping.SimpleValue in project hibernate-orm by hibernate.
the class SimpleValueBinder method make.
public SimpleValue make() {
validate();
LOG.debugf("building SimpleValue for %s", propertyName);
if (table == null) {
table = columns[0].getTable();
}
simpleValue = new SimpleValue(buildingContext.getMetadataCollector(), table);
if (isVersion) {
simpleValue.makeVersion();
}
if (isNationalized) {
simpleValue.makeNationalized();
}
if (isLob) {
simpleValue.makeLob();
}
linkWithValue();
boolean isInSecondPass = buildingContext.getMetadataCollector().isInSecondPass();
if (!isInSecondPass) {
//Defer this to the second pass
buildingContext.getMetadataCollector().addSecondPass(new SetSimpleValueTypeSecondPass(this));
} else {
//We are already in second pass
fillSimpleValue();
}
return simpleValue;
}
use of org.hibernate.mapping.SimpleValue in project hibernate-orm by hibernate.
the class TableBinder method bindFk.
public static void bindFk(PersistentClass referencedEntity, PersistentClass destinationEntity, Ejb3JoinColumn[] columns, SimpleValue value, boolean unique, MetadataBuildingContext buildingContext) {
PersistentClass associatedClass;
if (destinationEntity != null) {
//overridden destination
associatedClass = destinationEntity;
} else {
associatedClass = columns[0].getPropertyHolder() == null ? null : columns[0].getPropertyHolder().getPersistentClass();
}
final String mappedByProperty = columns[0].getMappedBy();
if (StringHelper.isNotEmpty(mappedByProperty)) {
/**
* Get the columns of the mapped-by property
* copy them and link the copy to the actual value
*/
LOG.debugf("Retrieving property %s.%s", associatedClass.getEntityName(), mappedByProperty);
final Property property = associatedClass.getRecursiveProperty(columns[0].getMappedBy());
Iterator mappedByColumns;
if (property.getValue() instanceof Collection) {
Collection collection = ((Collection) property.getValue());
Value element = collection.getElement();
if (element == null) {
throw new AnnotationException("Illegal use of mappedBy on both sides of the relationship: " + associatedClass.getEntityName() + "." + mappedByProperty);
}
mappedByColumns = element.getColumnIterator();
} else {
mappedByColumns = property.getValue().getColumnIterator();
}
while (mappedByColumns.hasNext()) {
Column column = (Column) mappedByColumns.next();
columns[0].overrideFromReferencedColumnIfNecessary(column);
columns[0].linkValueUsingAColumnCopy(column, value);
}
} else if (columns[0].isImplicit()) {
/**
* if columns are implicit, then create the columns based on the
* referenced entity id columns
*/
Iterator idColumns;
if (referencedEntity instanceof JoinedSubclass) {
idColumns = referencedEntity.getKey().getColumnIterator();
} else {
idColumns = referencedEntity.getIdentifier().getColumnIterator();
}
while (idColumns.hasNext()) {
Column column = (Column) idColumns.next();
columns[0].linkValueUsingDefaultColumnNaming(column, referencedEntity, value);
columns[0].overrideFromReferencedColumnIfNecessary(column);
}
} else {
int fkEnum = Ejb3JoinColumn.checkReferencedColumnsType(columns, referencedEntity, buildingContext);
if (Ejb3JoinColumn.NON_PK_REFERENCE == fkEnum) {
String referencedPropertyName;
if (value instanceof ToOne) {
referencedPropertyName = ((ToOne) value).getReferencedPropertyName();
} else if (value instanceof DependantValue) {
String propertyName = columns[0].getPropertyName();
if (propertyName != null) {
Collection collection = (Collection) referencedEntity.getRecursiveProperty(propertyName).getValue();
referencedPropertyName = collection.getReferencedPropertyName();
} else {
throw new AnnotationException("SecondaryTable JoinColumn cannot reference a non primary key");
}
} else {
throw new AssertionFailure("Do a property ref on an unexpected Value type: " + value.getClass().getName());
}
if (referencedPropertyName == null) {
throw new AssertionFailure("No property ref found while expected");
}
Property synthProp = referencedEntity.getReferencedProperty(referencedPropertyName);
if (synthProp == null) {
throw new AssertionFailure("Cannot find synthProp: " + referencedEntity.getEntityName() + "." + referencedPropertyName);
}
linkJoinColumnWithValueOverridingNameIfImplicit(referencedEntity, synthProp.getColumnIterator(), columns, value);
} else {
if (Ejb3JoinColumn.NO_REFERENCE == fkEnum) {
//implicit case, we hope PK and FK columns are in the same order
if (columns.length != referencedEntity.getIdentifier().getColumnSpan()) {
throw new AnnotationException("A Foreign key refering " + referencedEntity.getEntityName() + " from " + associatedClass.getEntityName() + " has the wrong number of column. should be " + referencedEntity.getIdentifier().getColumnSpan());
}
linkJoinColumnWithValueOverridingNameIfImplicit(referencedEntity, referencedEntity.getIdentifier().getColumnIterator(), columns, value);
} else {
//explicit referencedColumnName
Iterator idColItr = referencedEntity.getKey().getColumnIterator();
org.hibernate.mapping.Column col;
//works cause the pk has to be on the primary table
Table table = referencedEntity.getTable();
if (!idColItr.hasNext()) {
LOG.debug("No column in the identifier!");
}
while (idColItr.hasNext()) {
boolean match = false;
//for each PK column, find the associated FK column.
col = (org.hibernate.mapping.Column) idColItr.next();
for (Ejb3JoinColumn joinCol : columns) {
String referencedColumn = joinCol.getReferencedColumn();
referencedColumn = buildingContext.getMetadataCollector().getPhysicalColumnName(table, referencedColumn);
//In JPA 2 referencedColumnName is case insensitive
if (referencedColumn.equalsIgnoreCase(col.getQuotedName(buildingContext.getMetadataCollector().getDatabase().getJdbcEnvironment().getDialect()))) {
//proper join column
if (joinCol.isNameDeferred()) {
joinCol.linkValueUsingDefaultColumnNaming(col, referencedEntity, value);
} else {
joinCol.linkWithValue(value);
}
joinCol.overrideFromReferencedColumnIfNecessary(col);
match = true;
break;
}
}
if (!match) {
throw new AnnotationException("Column name " + col.getName() + " of " + referencedEntity.getEntityName() + " not found in JoinColumns.referencedColumnName");
}
}
}
}
}
value.createForeignKey();
if (unique) {
createUniqueConstraint(value);
}
}
Aggregations