use of io.micronaut.data.runtime.convert.DataConversionService in project micronaut-data by micronaut-projects.
the class HibernateJpaOperations method findStream.
@NonNull
@Override
public <T, R> Stream<R> findStream(@NonNull PreparedQuery<T, R> preparedQuery) {
// noinspection ConstantConditions
return transactionOperations.executeRead(status -> {
String query = preparedQuery.getQuery();
Pageable pageable = preparedQuery.getPageable();
Session currentSession = getCurrentSession();
Class<R> resultType = preparedQuery.getResultType();
boolean isNativeQuery = preparedQuery.isNative();
if (preparedQuery.isDtoProjection()) {
Query<Tuple> q;
if (isNativeQuery) {
q = currentSession.createNativeQuery(query, Tuple.class);
} else {
q = currentSession.createQuery(query, Tuple.class);
}
bindParameters(q, preparedQuery, query);
bindPageable(q, pageable);
return q.stream().map(tuple -> (new BeanIntrospectionMapper<Tuple, R>() {
@Override
public Object read(Tuple tuple1, String alias) {
return tuple1.get(alias);
}
@Override
public ConversionService<?> getConversionService() {
return dataConversionService;
}
}).map(tuple, resultType));
} else {
Query<R> q;
@SuppressWarnings("unchecked") Class<R> wrapperType = ReflectionUtils.getWrapperType(resultType);
if (isNativeQuery) {
Class<T> rootEntity = preparedQuery.getRootEntity();
if (wrapperType != rootEntity) {
NativeQuery<Tuple> nativeQuery = currentSession.createNativeQuery(query, Tuple.class);
bindParameters(nativeQuery, preparedQuery, query);
bindPageable(nativeQuery, pageable);
return nativeQuery.stream().map(tuple -> {
Object o = tuple.get(0);
if (wrapperType.isInstance(o)) {
return (R) o;
} else {
return dataConversionService.convertRequired(o, wrapperType);
}
});
} else {
q = currentSession.createNativeQuery(query, wrapperType);
}
} else {
q = currentSession.createQuery(query, wrapperType);
}
bindParameters(q, preparedQuery, query);
bindPageable(q, pageable);
return q.stream();
}
});
}
use of io.micronaut.data.runtime.convert.DataConversionService in project micronaut-data by micronaut-projects.
the class HibernateJpaOperations method findAll.
@NonNull
@Override
public <T, R> Iterable<R> findAll(@NonNull PreparedQuery<T, R> preparedQuery) {
return transactionOperations.executeRead(status -> {
Session entityManager = sessionFactory.getCurrentSession();
String queryStr = preparedQuery.getQuery();
Pageable pageable = preparedQuery.getPageable();
if (pageable != Pageable.UNPAGED) {
Sort sort = pageable.getSort();
if (sort.isSorted()) {
queryStr += QUERY_BUILDER.buildOrderBy(queryStr, getEntity(preparedQuery.getRootEntity()), sort).getQuery();
}
}
if (preparedQuery.isDtoProjection()) {
Query<Tuple> q;
if (preparedQuery.isNative()) {
q = entityManager.createNativeQuery(queryStr, Tuple.class);
} else if (queryStr.toLowerCase(Locale.ENGLISH).startsWith("select new ")) {
Class<R> wrapperType = ReflectionUtils.getWrapperType(preparedQuery.getResultType());
Query<R> query = entityManager.createQuery(queryStr, wrapperType);
bindPreparedQuery(query, preparedQuery, entityManager, queryStr);
return query.list();
} else {
q = entityManager.createQuery(queryStr, Tuple.class);
}
bindPreparedQuery(q, preparedQuery, entityManager, queryStr);
return q.stream().map(tuple -> {
Set<String> properties = tuple.getElements().stream().map(TupleElement::getAlias).collect(Collectors.toCollection(() -> new TreeSet<>(String.CASE_INSENSITIVE_ORDER)));
return (new BeanIntrospectionMapper<Tuple, R>() {
@Override
public Object read(Tuple tuple1, String alias) {
if (!properties.contains(alias)) {
return null;
}
return tuple1.get(alias);
}
@Override
public ConversionService<?> getConversionService() {
return dataConversionService;
}
}).map(tuple, preparedQuery.getResultType());
}).collect(Collectors.toList());
} else {
Class<R> wrapperType = ReflectionUtils.getWrapperType(preparedQuery.getResultType());
Query<R> q;
if (preparedQuery.isNative()) {
if (DataType.ENTITY.equals(preparedQuery.getResultDataType())) {
q = entityManager.createNativeQuery(queryStr, wrapperType);
} else {
q = entityManager.createNativeQuery(queryStr);
}
} else {
q = entityManager.createQuery(queryStr, wrapperType);
}
bindPreparedQuery(q, preparedQuery, entityManager, queryStr);
return q.list();
}
});
}
Aggregations