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());
}
}
use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.
the class StatementPreparerImpl method prepareQueryStatement.
@Override
public PreparedStatement prepareQueryStatement(String sql, final boolean isCallable, final ScrollMode scrollMode) {
if (scrollMode != null && !scrollMode.equals(ScrollMode.FORWARD_ONLY)) {
if (!settings().isScrollableResultSetsEnabled()) {
throw new AssertionFailure("scrollable result sets are not enabled");
}
final PreparedStatement ps = new QueryStatementPreparationTemplate(sql) {
public PreparedStatement doPrepare() throws SQLException {
return isCallable ? connection().prepareCall(sql, scrollMode.toResultSetType(), ResultSet.CONCUR_READ_ONLY) : connection().prepareStatement(sql, scrollMode.toResultSetType(), ResultSet.CONCUR_READ_ONLY);
}
}.prepareStatement();
jdbcCoordinator.registerLastQuery(ps);
return ps;
} else {
final PreparedStatement ps = new QueryStatementPreparationTemplate(sql) {
public PreparedStatement doPrepare() throws SQLException {
return isCallable ? connection().prepareCall(sql) : connection().prepareStatement(sql);
}
}.prepareStatement();
jdbcCoordinator.registerLastQuery(ps);
return ps;
}
}
use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.
the class DefaultSaveOrUpdateEventListener method entityIsDetached.
/**
* The given save-update event named a detached entity.
* <p/>
* Here, we will perform the update processing.
*
* @param event The update event to be handled.
*/
protected void entityIsDetached(SaveOrUpdateEvent event) {
LOG.trace("Updating detached instance");
if (event.getSession().getPersistenceContext().isEntryFor(event.getEntity())) {
//TODO: assertion only, could be optimized away
throw new AssertionFailure("entity was persistent");
}
Object entity = event.getEntity();
EntityPersister persister = event.getSession().getEntityPersister(event.getEntityName(), entity);
event.setRequestedId(getUpdateId(entity, persister, event.getRequestedId(), event.getSession()));
performUpdate(event, entity, persister);
}
use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.
the class DefaultSaveOrUpdateEventListener method entityIsPersistent.
protected Serializable entityIsPersistent(SaveOrUpdateEvent event) throws HibernateException {
final boolean traceEnabled = LOG.isTraceEnabled();
if (traceEnabled) {
LOG.trace("Ignoring persistent instance");
}
EntityEntry entityEntry = event.getEntry();
if (entityEntry == null) {
throw new AssertionFailure("entity was transient or detached");
} else {
if (entityEntry.getStatus() == Status.DELETED) {
throw new AssertionFailure("entity was deleted");
}
final SessionFactoryImplementor factory = event.getSession().getFactory();
Serializable requestedId = event.getRequestedId();
Serializable savedId;
if (requestedId == null) {
savedId = entityEntry.getId();
} else {
final boolean isEqual = !entityEntry.getPersister().getIdentifierType().isEqual(requestedId, entityEntry.getId(), factory);
if (isEqual) {
throw new PersistentObjectException("object passed to save() was already persistent: " + MessageHelper.infoString(entityEntry.getPersister(), requestedId, factory));
}
savedId = requestedId;
}
if (traceEnabled) {
LOG.tracev("Object already associated with session: {0}", MessageHelper.infoString(entityEntry.getPersister(), savedId, factory));
}
return savedId;
}
}
Aggregations