use of org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn in project hibernate-orm by hibernate.
the class ResultsetMappingSecondPass method doSecondPass.
@Override
public void doSecondPass(Map persistentClasses) throws MappingException {
//TODO add parameters checkings
if (ann == null)
return;
ResultSetMappingDefinition definition = new ResultSetMappingDefinition(ann.name());
LOG.debugf("Binding result set mapping: %s", definition.getName());
int entityAliasIndex = 0;
for (EntityResult entity : ann.entities()) {
//TODO parameterize lock mode?
List<FieldResult> properties = new ArrayList<FieldResult>();
List<String> propertyNames = new ArrayList<String>();
for (FieldResult field : entity.fields()) {
//use an ArrayList cause we might have several columns per root property
String name = field.name();
if (name.indexOf('.') == -1) {
//regular property
properties.add(field);
propertyNames.add(name);
} else {
/**
* Reorder properties
* 1. get the parent property
* 2. list all the properties following the expected one in the parent property
* 3. calculate the lowest index and insert the property
*/
PersistentClass pc = context.getMetadataCollector().getEntityBinding(entity.entityClass().getName());
if (pc == null) {
throw new MappingException(String.format(Locale.ENGLISH, "Could not resolve entity [%s] referenced in SqlResultSetMapping [%s]", entity.entityClass().getName(), ann.name()));
}
int dotIndex = name.lastIndexOf('.');
String reducedName = name.substring(0, dotIndex);
Iterator parentPropItr = getSubPropertyIterator(pc, reducedName);
List<String> followers = getFollowers(parentPropItr, reducedName, name);
int index = propertyNames.size();
for (String follower : followers) {
int currentIndex = getIndexOfFirstMatchingProperty(propertyNames, follower);
index = currentIndex != -1 && currentIndex < index ? currentIndex : index;
}
propertyNames.add(index, name);
properties.add(index, field);
}
}
Set<String> uniqueReturnProperty = new HashSet<String>();
Map<String, ArrayList<String>> propertyResultsTmp = new HashMap<String, ArrayList<String>>();
for (Object property : properties) {
final FieldResult propertyresult = (FieldResult) property;
final String name = propertyresult.name();
if ("class".equals(name)) {
throw new MappingException("class is not a valid property name to use in a @FieldResult, use @Entity(discriminatorColumn) instead");
}
if (uniqueReturnProperty.contains(name)) {
throw new MappingException("duplicate @FieldResult for property " + name + " on @Entity " + entity.entityClass().getName() + " in " + ann.name());
}
uniqueReturnProperty.add(name);
final String quotingNormalizedColumnName = normalizeColumnQuoting(propertyresult.column());
String key = StringHelper.root(name);
ArrayList<String> intermediateResults = propertyResultsTmp.get(key);
if (intermediateResults == null) {
intermediateResults = new ArrayList<String>();
propertyResultsTmp.put(key, intermediateResults);
}
intermediateResults.add(quotingNormalizedColumnName);
}
Map<String, String[]> propertyResults = new HashMap<String, String[]>();
for (Map.Entry<String, ArrayList<String>> entry : propertyResultsTmp.entrySet()) {
propertyResults.put(entry.getKey(), entry.getValue().toArray(new String[entry.getValue().size()]));
}
if (!BinderHelper.isEmptyAnnotationValue(entity.discriminatorColumn())) {
final String quotingNormalizedName = normalizeColumnQuoting(entity.discriminatorColumn());
propertyResults.put("class", new String[] { quotingNormalizedName });
}
if (propertyResults.isEmpty()) {
propertyResults = java.util.Collections.emptyMap();
}
NativeSQLQueryRootReturn result = new NativeSQLQueryRootReturn("alias" + entityAliasIndex++, entity.entityClass().getName(), propertyResults, LockMode.READ);
definition.addQueryReturn(result);
}
for (ColumnResult column : ann.columns()) {
definition.addQueryReturn(new NativeSQLQueryScalarReturn(normalizeColumnQuoting(column.name()), column.type() != null ? context.getMetadataCollector().getTypeResolver().heuristicType(column.type().getName()) : null));
}
for (ConstructorResult constructorResult : ann.classes()) {
List<NativeSQLQueryScalarReturn> columnReturns = new ArrayList<NativeSQLQueryScalarReturn>();
for (ColumnResult columnResult : constructorResult.columns()) {
columnReturns.add(new NativeSQLQueryScalarReturn(normalizeColumnQuoting(columnResult.name()), columnResult.type() != null ? context.getMetadataCollector().getTypeResolver().heuristicType(columnResult.type().getName()) : null));
}
definition.addQueryReturn(new NativeSQLQueryConstructorReturn(constructorResult.targetClass(), columnReturns));
}
if (isDefault) {
context.getMetadataCollector().addDefaultResultSetMapping(definition);
} else {
context.getMetadataCollector().addResultSetMapping(definition);
}
}
use of org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn in project hibernate-orm by hibernate.
the class QueryBinder method bindNativeQuery.
public static void bindNativeQuery(org.hibernate.annotations.NamedNativeQuery queryAnn, MetadataBuildingContext context) {
if (queryAnn == null) {
return;
}
//ResultSetMappingDefinition mappingDefinition = mappings.getResultSetMapping( queryAnn.resultSetMapping() );
if (BinderHelper.isEmptyAnnotationValue(queryAnn.name())) {
throw new AnnotationException("A named query must have a name when used in class or package level");
}
NamedSQLQueryDefinition query;
String resultSetMapping = queryAnn.resultSetMapping();
if (!BinderHelper.isEmptyAnnotationValue(resultSetMapping)) {
//sql result set usage
query = new NamedSQLQueryDefinitionBuilder().setName(queryAnn.name()).setQuery(queryAnn.query()).setResultSetRef(resultSetMapping).setQuerySpaces(null).setCacheable(queryAnn.cacheable()).setCacheRegion(BinderHelper.isEmptyAnnotationValue(queryAnn.cacheRegion()) ? null : queryAnn.cacheRegion()).setTimeout(queryAnn.timeout() < 0 ? null : queryAnn.timeout()).setFetchSize(queryAnn.fetchSize() < 0 ? null : queryAnn.fetchSize()).setFlushMode(getFlushMode(queryAnn.flushMode())).setCacheMode(getCacheMode(queryAnn.cacheMode())).setReadOnly(queryAnn.readOnly()).setComment(BinderHelper.isEmptyAnnotationValue(queryAnn.comment()) ? null : queryAnn.comment()).setParameterTypes(null).setCallable(queryAnn.callable()).createNamedQueryDefinition();
} else if (!void.class.equals(queryAnn.resultClass())) {
//class mapping usage
//FIXME should be done in a second pass due to entity name?
final NativeSQLQueryRootReturn entityQueryReturn = new NativeSQLQueryRootReturn("alias1", queryAnn.resultClass().getName(), new HashMap(), LockMode.READ);
query = new NamedSQLQueryDefinitionBuilder().setName(queryAnn.name()).setQuery(queryAnn.query()).setQueryReturns(new NativeSQLQueryReturn[] { entityQueryReturn }).setQuerySpaces(null).setCacheable(queryAnn.cacheable()).setCacheRegion(BinderHelper.isEmptyAnnotationValue(queryAnn.cacheRegion()) ? null : queryAnn.cacheRegion()).setTimeout(queryAnn.timeout() < 0 ? null : queryAnn.timeout()).setFetchSize(queryAnn.fetchSize() < 0 ? null : queryAnn.fetchSize()).setFlushMode(getFlushMode(queryAnn.flushMode())).setCacheMode(getCacheMode(queryAnn.cacheMode())).setReadOnly(queryAnn.readOnly()).setComment(BinderHelper.isEmptyAnnotationValue(queryAnn.comment()) ? null : queryAnn.comment()).setParameterTypes(null).setCallable(queryAnn.callable()).createNamedQueryDefinition();
} else {
throw new NotYetImplementedException("Pure native scalar queries are not yet supported");
}
context.getMetadataCollector().addNamedNativeQuery(query);
if (LOG.isDebugEnabled()) {
LOG.debugf("Binding named native query: %s => %s", query.getName(), queryAnn.query());
}
}
use of org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn in project hibernate-orm by hibernate.
the class QueryBinder method bindNativeQuery.
public static void bindNativeQuery(NamedNativeQuery queryAnn, MetadataBuildingContext context, boolean isDefault) {
if (queryAnn == null)
return;
//ResultSetMappingDefinition mappingDefinition = mappings.getResultSetMapping( queryAnn.resultSetMapping() );
if (BinderHelper.isEmptyAnnotationValue(queryAnn.name())) {
throw new AnnotationException("A named query must have a name when used in class or package level");
}
String resultSetMapping = queryAnn.resultSetMapping();
QueryHintDefinition hints = new QueryHintDefinition(queryAnn.hints());
String queryName = queryAnn.query();
NamedSQLQueryDefinitionBuilder builder = new NamedSQLQueryDefinitionBuilder(queryAnn.name()).setQuery(queryName).setQuerySpaces(null).setCacheable(hints.getBoolean(queryName, QueryHints.CACHEABLE)).setCacheRegion(hints.getString(queryName, QueryHints.CACHE_REGION)).setTimeout(hints.getTimeout(queryName)).setFetchSize(hints.getInteger(queryName, QueryHints.FETCH_SIZE)).setFlushMode(hints.getFlushMode(queryName)).setCacheMode(hints.getCacheMode(queryName)).setReadOnly(hints.getBoolean(queryName, QueryHints.READ_ONLY)).setComment(hints.getString(queryName, QueryHints.COMMENT)).setParameterTypes(null).setCallable(hints.getBoolean(queryName, QueryHints.CALLABLE));
if (!BinderHelper.isEmptyAnnotationValue(resultSetMapping)) {
//sql result set usage
builder.setResultSetRef(resultSetMapping).createNamedQueryDefinition();
} else if (!void.class.equals(queryAnn.resultClass())) {
//class mapping usage
//FIXME should be done in a second pass due to entity name?
final NativeSQLQueryRootReturn entityQueryReturn = new NativeSQLQueryRootReturn("alias1", queryAnn.resultClass().getName(), new HashMap(), LockMode.READ);
builder.setQueryReturns(new NativeSQLQueryReturn[] { entityQueryReturn });
} else {
builder.setQueryReturns(new NativeSQLQueryReturn[0]);
}
NamedSQLQueryDefinition query = builder.createNamedQueryDefinition();
if (isDefault) {
context.getMetadataCollector().addDefaultNamedNativeQuery(query);
} else {
context.getMetadataCollector().addNamedNativeQuery(query);
}
if (LOG.isDebugEnabled()) {
LOG.debugf("Binding named native query: %s => %s", queryAnn.name(), queryAnn.query());
}
}
use of org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn in project hibernate-orm by hibernate.
the class NativeSQLQueryReturnEqualsAndHashCodeTest method testNativeSQLQueryReturnTypes.
@Test
public void testNativeSQLQueryReturnTypes() {
NativeSQLQueryScalarReturn r1 = new NativeSQLQueryScalarReturn("a", sessionFactory().getTypeResolver().basic("int"));
NativeSQLQueryRootReturn r2 = new NativeSQLQueryRootReturn("a", "b", LockMode.NONE);
NativeSQLQueryJoinReturn r3 = new NativeSQLQueryJoinReturn("a", "b", "c", Collections.singletonMap("key", "value"), LockMode.NONE);
NativeSQLQueryCollectionReturn r4 = new NativeSQLQueryCollectionReturn("a", "b", "c", Collections.singletonMap("key", "value"), LockMode.NONE);
check(false, r1, r2);
check(false, r1, r3);
check(false, r1, r4);
check(false, r2, r3);
check(false, r2, r4);
check(false, r3, r4);
}
use of org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn in project hibernate-orm by hibernate.
the class NativeSQLQueryReturnEqualsAndHashCodeTest method testNativeSQLQueryRootReturn.
@Test
public void testNativeSQLQueryRootReturn() {
NativeSQLQueryRootReturn alias = new NativeSQLQueryRootReturn("abc", null, null);
NativeSQLQueryRootReturn diffAlias = new NativeSQLQueryRootReturn("def", null, null);
NativeSQLQueryRootReturn aliasEntityName = new NativeSQLQueryRootReturn("abc", "Person", null);
NativeSQLQueryRootReturn aliasDiffEntityName = new NativeSQLQueryRootReturn("abc", "Customer", null);
NativeSQLQueryRootReturn aliasEntityNameLockMode = new NativeSQLQueryRootReturn("abc", "Person", LockMode.NONE);
NativeSQLQueryRootReturn aliasEntityNameDiffLockMode = new NativeSQLQueryRootReturn("abc", "Person", LockMode.OPTIMISTIC);
check(false, alias, diffAlias);
check(false, alias, aliasEntityName);
check(false, alias, aliasDiffEntityName);
check(false, alias, aliasEntityNameLockMode);
check(false, alias, aliasEntityNameDiffLockMode);
check(false, diffAlias, aliasEntityName);
check(false, diffAlias, aliasDiffEntityName);
check(false, diffAlias, aliasEntityNameLockMode);
check(false, diffAlias, aliasEntityNameDiffLockMode);
check(false, aliasEntityName, aliasDiffEntityName);
check(false, aliasEntityName, aliasEntityNameLockMode);
check(false, aliasEntityName, aliasEntityNameDiffLockMode);
check(false, aliasDiffEntityName, aliasEntityNameLockMode);
check(false, aliasDiffEntityName, aliasEntityNameDiffLockMode);
check(false, aliasEntityNameLockMode, aliasEntityNameDiffLockMode);
check(true, alias, new NativeSQLQueryRootReturn("abc", null, null));
check(true, diffAlias, new NativeSQLQueryRootReturn("def", null, null));
check(true, aliasEntityName, new NativeSQLQueryRootReturn("abc", "Person", null));
check(true, aliasDiffEntityName, new NativeSQLQueryRootReturn("abc", "Customer", null));
check(true, aliasEntityNameLockMode, new NativeSQLQueryRootReturn("abc", "Person", LockMode.NONE));
check(true, aliasEntityNameDiffLockMode, new NativeSQLQueryRootReturn("abc", "Person", LockMode.OPTIMISTIC));
}
Aggregations