use of org.hibernate.MappingException in project hibernate-orm by hibernate.
the class InFlightMetadataCollectorImpl method getLogicalColumnName.
@Override
public String getLogicalColumnName(Table table, Identifier physicalName) throws MappingException {
final String physicalNameString = physicalName.render(getDatabase().getJdbcEnvironment().getDialect());
Identifier logicalName = null;
Table currentTable = table;
while (currentTable != null) {
final TableColumnNameBinding binding = columnNameBindingByTableMap.get(currentTable);
if (binding != null) {
logicalName = binding.physicalToLogical.get(physicalNameString);
if (logicalName != null) {
break;
}
}
if (DenormalizedTable.class.isInstance(currentTable)) {
currentTable = ((DenormalizedTable) currentTable).getIncludedTable();
} else {
currentTable = null;
}
}
if (logicalName == null) {
throw new MappingException("Unable to find column with physical name " + physicalNameString + " in table " + table.getName());
}
return logicalName.render();
}
use of org.hibernate.MappingException in project hibernate-orm by hibernate.
the class InFlightMetadataCollectorImpl method topologicalSort.
/* naive O(n^3) topological sort */
private void topologicalSort(List<CopyIdentifierComponentSecondPass> sorted, Set<CopyIdentifierComponentSecondPass> toSort) {
while (!toSort.isEmpty()) {
CopyIdentifierComponentSecondPass independent = null;
searchForIndependent: for (CopyIdentifierComponentSecondPass secondPass : toSort) {
for (CopyIdentifierComponentSecondPass other : toSort) {
if (secondPass.dependentUpon(other)) {
continue searchForIndependent;
}
}
independent = secondPass;
break;
}
if (independent == null) {
throw new MappingException("cyclic dependency in derived identities");
}
toSort.remove(independent);
sorted.add(independent);
}
}
use of org.hibernate.MappingException in project hibernate-orm by hibernate.
the class BinderHelper method findColumnOwner.
/**
* Find the column owner (ie PersistentClass or Join) of columnName.
* If columnName is null or empty, persistentClass is returned
*/
public static Object findColumnOwner(PersistentClass persistentClass, String columnName, MetadataBuildingContext context) {
if (StringHelper.isEmpty(columnName)) {
//shortcut for implicit referenced column names
return persistentClass;
}
PersistentClass current = persistentClass;
Object result;
boolean found = false;
do {
result = current;
Table currentTable = current.getTable();
try {
context.getMetadataCollector().getPhysicalColumnName(currentTable, columnName);
found = true;
} catch (MappingException me) {
//swallow it
}
Iterator joins = current.getJoinIterator();
while (!found && joins.hasNext()) {
result = joins.next();
currentTable = ((Join) result).getTable();
try {
context.getMetadataCollector().getPhysicalColumnName(currentTable, columnName);
found = true;
} catch (MappingException me) {
//swallow it
}
}
current = current.getSuperclass();
} while (!found && current != null);
return found ? result : null;
}
use of org.hibernate.MappingException in project hibernate-orm by hibernate.
the class BinderHelper method buildAnyValue.
public static Any buildAnyValue(String anyMetaDefName, Ejb3JoinColumn[] columns, javax.persistence.Column metaColumn, PropertyData inferredData, boolean cascadeOnDelete, Nullability nullability, PropertyHolder propertyHolder, EntityBinder entityBinder, boolean optional, MetadataBuildingContext context) {
//All FK columns should be in the same table
Any value = new Any(context.getMetadataCollector(), columns[0].getTable());
AnyMetaDef metaAnnDef = inferredData.getProperty().getAnnotation(AnyMetaDef.class);
if (metaAnnDef != null) {
//local has precedence over general and can be mapped for future reference if named
bindAnyMetaDefs(inferredData.getProperty(), context);
} else {
metaAnnDef = context.getMetadataCollector().getAnyMetaDef(anyMetaDefName);
}
if (metaAnnDef != null) {
value.setIdentifierType(metaAnnDef.idType());
value.setMetaType(metaAnnDef.metaType());
HashMap values = new HashMap();
org.hibernate.type.Type metaType = context.getMetadataCollector().getTypeResolver().heuristicType(value.getMetaType());
for (MetaValue metaValue : metaAnnDef.metaValues()) {
try {
Object discrim = ((org.hibernate.type.DiscriminatorType) metaType).stringToObject(metaValue.value());
String entityName = metaValue.targetEntity().getName();
values.put(discrim, entityName);
} catch (ClassCastException cce) {
throw new MappingException("metaType was not a DiscriminatorType: " + metaType.getName());
} catch (Exception e) {
throw new MappingException("could not interpret metaValue", e);
}
}
if (!values.isEmpty()) {
value.setMetaValues(values);
}
} else {
throw new AnnotationException("Unable to find @AnyMetaDef for an @(ManyTo)Any mapping: " + StringHelper.qualify(propertyHolder.getPath(), inferredData.getPropertyName()));
}
value.setCascadeDeleteEnabled(cascadeOnDelete);
if (!optional) {
for (Ejb3JoinColumn column : columns) {
column.setNullable(false);
}
}
Ejb3Column[] metaColumns = Ejb3Column.buildColumnFromAnnotation(new javax.persistence.Column[] { metaColumn }, null, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), context);
//set metaColumn to the right table
for (Ejb3Column column : metaColumns) {
column.setTable(value.getTable());
}
//meta column
for (Ejb3Column column : metaColumns) {
column.linkWithValue(value);
}
//id columns
final String propertyName = inferredData.getPropertyName();
Ejb3Column.checkPropertyConsistency(columns, propertyHolder.getEntityName() + "." + propertyName);
for (Ejb3JoinColumn column : columns) {
column.linkWithValue(value);
}
return value;
}
use of org.hibernate.MappingException in project hibernate-orm by hibernate.
the class BinderHelper method findPropertyByName.
/**
* 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 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;
}
Aggregations