use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.
the class StandardForeignKeyExporter method getSqlCreateStrings.
@Override
public String[] getSqlCreateStrings(ForeignKey foreignKey, Metadata metadata) {
if (!dialect.hasAlterTable()) {
return NO_COMMANDS;
}
if (!foreignKey.isCreationEnabled()) {
return NO_COMMANDS;
}
if (!foreignKey.isPhysicalConstraint()) {
return NO_COMMANDS;
}
final int numberOfColumns = foreignKey.getColumnSpan();
final String[] columnNames = new String[numberOfColumns];
final String[] targetColumnNames = new String[numberOfColumns];
final Iterator targetItr;
if (foreignKey.isReferenceToPrimaryKey()) {
if (numberOfColumns != foreignKey.getReferencedTable().getPrimaryKey().getColumnSpan()) {
throw new AssertionFailure(String.format(Locale.ENGLISH, COLUMN_MISMATCH_MSG, numberOfColumns, foreignKey.getReferencedTable().getPrimaryKey().getColumnSpan(), foreignKey.getName(), foreignKey.getTable().getName(), foreignKey.getReferencedTable().getName()));
}
targetItr = foreignKey.getReferencedTable().getPrimaryKey().getColumnIterator();
} else {
if (numberOfColumns != foreignKey.getReferencedColumns().size()) {
throw new AssertionFailure(String.format(Locale.ENGLISH, COLUMN_MISMATCH_MSG, numberOfColumns, foreignKey.getReferencedColumns().size(), foreignKey.getName(), foreignKey.getTable().getName(), foreignKey.getReferencedTable().getName()));
}
targetItr = foreignKey.getReferencedColumns().iterator();
}
int i = 0;
final Iterator itr = foreignKey.getColumnIterator();
while (itr.hasNext()) {
columnNames[i] = ((Column) itr.next()).getQuotedName(dialect);
targetColumnNames[i] = ((Column) targetItr.next()).getQuotedName(dialect);
i++;
}
final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();
final String sourceTableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(foreignKey.getTable().getQualifiedTableName(), dialect);
final String targetTableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(foreignKey.getReferencedTable().getQualifiedTableName(), dialect);
final StringBuilder buffer = new StringBuilder("alter table ").append(sourceTableName).append(foreignKey.getKeyDefinition() != null ? dialect.getAddForeignKeyConstraintString(foreignKey.getName(), foreignKey.getKeyDefinition()) : dialect.getAddForeignKeyConstraintString(foreignKey.getName(), columnNames, targetTableName, targetColumnNames, foreignKey.isReferenceToPrimaryKey()));
if (dialect.supportsCascadeDelete()) {
if (foreignKey.isCascadeDeleteEnabled()) {
buffer.append(" on delete cascade");
}
}
return new String[] { buffer.toString() };
}
use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.
the class InterceptorTest method testPrepareStatementFaultIntercept.
public void testPrepareStatementFaultIntercept() {
final Interceptor interceptor = new EmptyInterceptor() {
@Override
public String onPrepareStatement(String sql) {
return null;
}
};
Session s = openSession(interceptor);
try {
Transaction t = s.beginTransaction();
User u = new User("Kinga", "Mroz");
s.persist(u);
t.commit();
} catch (TransactionException e) {
assertTrue(e.getCause() instanceof AssertionFailure);
} finally {
s.close();
}
}
use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.
the class ModelBinder method bindAllCompositeAttributes.
private void bindAllCompositeAttributes(MappingDocument sourceDocument, EmbeddableSource embeddableSource, Component component) {
for (AttributeSource attributeSource : embeddableSource.attributeSources()) {
Property attribute = null;
if (SingularAttributeSourceBasic.class.isInstance(attributeSource)) {
attribute = createBasicAttribute(sourceDocument, (SingularAttributeSourceBasic) attributeSource, new SimpleValue(sourceDocument.getMetadataCollector(), component.getTable()), component.getComponentClassName());
} else if (SingularAttributeSourceEmbedded.class.isInstance(attributeSource)) {
attribute = createEmbeddedAttribute(sourceDocument, (SingularAttributeSourceEmbedded) attributeSource, new Component(sourceDocument.getMetadataCollector(), component), component.getComponentClassName());
} else if (SingularAttributeSourceManyToOne.class.isInstance(attributeSource)) {
attribute = createManyToOneAttribute(sourceDocument, (SingularAttributeSourceManyToOne) attributeSource, new ManyToOne(sourceDocument.getMetadataCollector(), component.getTable()), component.getComponentClassName());
} else if (SingularAttributeSourceOneToOne.class.isInstance(attributeSource)) {
attribute = createOneToOneAttribute(sourceDocument, (SingularAttributeSourceOneToOne) attributeSource, new OneToOne(sourceDocument.getMetadataCollector(), component.getTable(), component.getOwner()), component.getComponentClassName());
} else if (SingularAttributeSourceAny.class.isInstance(attributeSource)) {
attribute = createAnyAssociationAttribute(sourceDocument, (SingularAttributeSourceAny) attributeSource, new Any(sourceDocument.getMetadataCollector(), component.getTable()), component.getComponentClassName());
} else if (PluralAttributeSource.class.isInstance(attributeSource)) {
attribute = createPluralAttribute(sourceDocument, (PluralAttributeSource) attributeSource, component.getOwner());
} else {
throw new AssertionFailure(String.format(Locale.ENGLISH, "Unexpected AttributeSource sub-type [%s] as part of composite [%s]", attributeSource.getClass().getName(), attributeSource.getAttributeRole().getFullPath()));
}
component.addProperty(attribute);
}
}
use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.
the class ModelBinder method createPluralAttribute.
private Property createPluralAttribute(MappingDocument sourceDocument, PluralAttributeSource attributeSource, PersistentClass entityDescriptor) {
final Collection collectionBinding;
if (attributeSource instanceof PluralAttributeSourceListImpl) {
collectionBinding = new org.hibernate.mapping.List(sourceDocument.getMetadataCollector(), entityDescriptor);
bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
registerSecondPass(new PluralAttributeListSecondPass(sourceDocument, (IndexedPluralAttributeSource) attributeSource, (org.hibernate.mapping.List) collectionBinding), sourceDocument);
} else if (attributeSource instanceof PluralAttributeSourceSetImpl) {
collectionBinding = new Set(sourceDocument.getMetadataCollector(), entityDescriptor);
bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
registerSecondPass(new PluralAttributeSetSecondPass(sourceDocument, attributeSource, collectionBinding), sourceDocument);
} else if (attributeSource instanceof PluralAttributeSourceMapImpl) {
collectionBinding = new org.hibernate.mapping.Map(sourceDocument.getMetadataCollector(), entityDescriptor);
bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
registerSecondPass(new PluralAttributeMapSecondPass(sourceDocument, (IndexedPluralAttributeSource) attributeSource, (org.hibernate.mapping.Map) collectionBinding), sourceDocument);
} else if (attributeSource instanceof PluralAttributeSourceBagImpl) {
collectionBinding = new Bag(sourceDocument.getMetadataCollector(), entityDescriptor);
bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
registerSecondPass(new PluralAttributeBagSecondPass(sourceDocument, attributeSource, collectionBinding), sourceDocument);
} else if (attributeSource instanceof PluralAttributeSourceIdBagImpl) {
collectionBinding = new IdentifierBag(sourceDocument.getMetadataCollector(), entityDescriptor);
bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
registerSecondPass(new PluralAttributeIdBagSecondPass(sourceDocument, attributeSource, collectionBinding), sourceDocument);
} else if (attributeSource instanceof PluralAttributeSourceArrayImpl) {
final PluralAttributeSourceArray arraySource = (PluralAttributeSourceArray) attributeSource;
collectionBinding = new Array(sourceDocument.getMetadataCollector(), entityDescriptor);
bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
((Array) collectionBinding).setElementClassName(sourceDocument.qualifyClassName(arraySource.getElementClass()));
registerSecondPass(new PluralAttributeArraySecondPass(sourceDocument, arraySource, (Array) collectionBinding), sourceDocument);
} else if (attributeSource instanceof PluralAttributeSourcePrimitiveArrayImpl) {
collectionBinding = new PrimitiveArray(sourceDocument.getMetadataCollector(), entityDescriptor);
bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
registerSecondPass(new PluralAttributePrimitiveArraySecondPass(sourceDocument, (IndexedPluralAttributeSource) attributeSource, (PrimitiveArray) collectionBinding), sourceDocument);
} else {
throw new AssertionFailure("Unexpected PluralAttributeSource type : " + attributeSource.getClass().getName());
}
sourceDocument.getMetadataCollector().addCollectionBinding(collectionBinding);
final Property attribute = new Property();
attribute.setValue(collectionBinding);
bindProperty(sourceDocument, attributeSource, attribute);
return attribute;
}
use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.
the class ModelBinder method bindCollectionMetadata.
private void bindCollectionMetadata(MappingDocument mappingDocument, PluralAttributeSource source, Collection binding) {
binding.setRole(source.getAttributeRole().getFullPath());
binding.setInverse(source.isInverse());
binding.setMutable(source.isMutable());
binding.setOptimisticLocked(source.isIncludedInOptimisticLocking());
if (source.getCustomPersisterClassName() != null) {
binding.setCollectionPersisterClass(mappingDocument.getClassLoaderAccess().classForName(mappingDocument.qualifyClassName(source.getCustomPersisterClassName())));
}
applyCaching(mappingDocument, source.getCaching(), binding);
// bind the collection type info
String typeName = source.getTypeInformation().getName();
Map typeParameters = new HashMap();
if (typeName != null) {
// see if there is a corresponding type-def
final TypeDefinition typeDef = mappingDocument.getMetadataCollector().getTypeDefinition(typeName);
if (typeDef != null) {
typeName = typeDef.getTypeImplementorClass().getName();
if (typeDef.getParameters() != null) {
typeParameters.putAll(typeDef.getParameters());
}
} else {
// it could be a unqualified class name, in which case we should qualify
// it with the implicit package name for this context, if one.
typeName = mappingDocument.qualifyClassName(typeName);
}
}
if (source.getTypeInformation().getParameters() != null) {
typeParameters.putAll(source.getTypeInformation().getParameters());
}
binding.setTypeName(typeName);
binding.setTypeParameters(typeParameters);
if (source.getFetchCharacteristics().getFetchTiming() == FetchTiming.DELAYED) {
binding.setLazy(true);
binding.setExtraLazy(source.getFetchCharacteristics().isExtraLazy());
} else {
binding.setLazy(false);
}
switch(source.getFetchCharacteristics().getFetchStyle()) {
case SELECT:
{
binding.setFetchMode(FetchMode.SELECT);
break;
}
case JOIN:
{
binding.setFetchMode(FetchMode.JOIN);
break;
}
case BATCH:
{
binding.setFetchMode(FetchMode.SELECT);
binding.setBatchSize(source.getFetchCharacteristics().getBatchSize());
break;
}
case SUBSELECT:
{
binding.setFetchMode(FetchMode.SELECT);
binding.setSubselectLoadable(true);
// todo : this could totally be done using a "symbol map" approach
binding.getOwner().setSubselectLoadableCollections(true);
break;
}
default:
{
throw new AssertionFailure("Unexpected FetchStyle : " + source.getFetchCharacteristics().getFetchStyle().name());
}
}
for (String name : source.getSynchronizedTableNames()) {
binding.getSynchronizedTables().add(name);
}
binding.setWhere(source.getWhere());
binding.setLoaderName(source.getCustomLoaderName());
if (source.getCustomSqlInsert() != null) {
binding.setCustomSQLInsert(source.getCustomSqlInsert().getSql(), source.getCustomSqlInsert().isCallable(), source.getCustomSqlInsert().getCheckStyle());
}
if (source.getCustomSqlUpdate() != null) {
binding.setCustomSQLUpdate(source.getCustomSqlUpdate().getSql(), source.getCustomSqlUpdate().isCallable(), source.getCustomSqlUpdate().getCheckStyle());
}
if (source.getCustomSqlDelete() != null) {
binding.setCustomSQLDelete(source.getCustomSqlDelete().getSql(), source.getCustomSqlDelete().isCallable(), source.getCustomSqlDelete().getCheckStyle());
}
if (source.getCustomSqlDeleteAll() != null) {
binding.setCustomSQLDeleteAll(source.getCustomSqlDeleteAll().getSql(), source.getCustomSqlDeleteAll().isCallable(), source.getCustomSqlDeleteAll().getCheckStyle());
}
if (source instanceof Sortable) {
final Sortable sortable = (Sortable) source;
if (sortable.isSorted()) {
binding.setSorted(true);
if (!sortable.getComparatorName().equals("natural")) {
binding.setComparatorClassName(sortable.getComparatorName());
}
} else {
binding.setSorted(false);
}
}
if (source instanceof Orderable) {
if (((Orderable) source).isOrdered()) {
binding.setOrderBy(((Orderable) source).getOrder());
}
}
final String cascadeStyle = source.getCascadeStyleName();
if (cascadeStyle != null && cascadeStyle.contains("delete-orphan")) {
binding.setOrphanDelete(true);
}
for (FilterSource filterSource : source.getFilterSources()) {
String condition = filterSource.getCondition();
if (condition == null) {
final FilterDefinition filterDefinition = mappingDocument.getMetadataCollector().getFilterDefinition(filterSource.getName());
if (filterDefinition != null) {
condition = filterDefinition.getDefaultFilterCondition();
}
}
binding.addFilter(filterSource.getName(), condition, filterSource.shouldAutoInjectAliases(), filterSource.getAliasToTableMap(), filterSource.getAliasToEntityMap());
}
}
Aggregations