use of io.micronaut.data.runtime.mapper.BeanIntrospectionMapper in project micronaut-data by micronaut-projects.
the class HibernateJpaOperations method findOne.
@Nullable
@Override
public <T, R> R findOne(@NonNull PreparedQuery<T, R> preparedQuery) {
return transactionOperations.executeRead(status -> {
Class<R> resultType = preparedQuery.getResultType();
String query = preparedQuery.getQuery();
Session currentSession = sessionFactory.getCurrentSession();
if (preparedQuery.isDtoProjection()) {
Query<Tuple> q;
if (preparedQuery.isNative()) {
q = currentSession.createNativeQuery(query, Tuple.class);
} else if (query.toLowerCase(Locale.ENGLISH).startsWith("select new ")) {
Query<R> dtoQuery = currentSession.createQuery(query, resultType);
bindParameters(dtoQuery, preparedQuery, query);
bindQueryHints(dtoQuery, preparedQuery, currentSession);
return dtoQuery.uniqueResult();
} else {
q = currentSession.createQuery(query, Tuple.class);
}
bindParameters(q, preparedQuery, query);
bindQueryHints(q, preparedQuery, currentSession);
Tuple tuple = first(q);
if (tuple != null) {
return (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);
}
return null;
} else {
Query<R> q;
if (preparedQuery.isNative()) {
if (DataType.ENTITY.equals(preparedQuery.getResultDataType())) {
q = currentSession.createNativeQuery(query, resultType);
} else {
q = currentSession.createNativeQuery(query);
}
} else {
q = currentSession.createQuery(query, resultType);
}
bindParameters(q, preparedQuery, query);
bindQueryHints(q, preparedQuery, currentSession);
return first(q);
}
});
}
use of io.micronaut.data.runtime.mapper.BeanIntrospectionMapper 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