JPA Criteria API group_concat usage - jpa

I am currently working on a report which needs a group_concat for one of the fields.
CriteriaQuery<GameDetailsDto> criteriaQuery = criteriaBuilder
.createQuery(GameDetailsDto.class);
Root<BetDetails> betDetails = criteriaQuery.from(BetDetails.class);
Expression<String> betSelection = betDetails.get("winningOutcome");
criteriaQuery.multiselect(
// other fields to select
criteriaBuilder.function("group_concat", String.class, betSelection),
// other fields to select
);
//predicate, where clause and other filters
TypedQuery<GameDetailsDto> typedQuery = entityManager.createQuery(criteriaQuery);
this throws a null pointer exception on the line:
TypedQuery<GameDetailsDto> typedQuery = entityManager.createQuery(criteriaQuery);
did i incorrectly use the function method of the criteriaBuilder?
the documentations says:
function(String name, Class<T> type, Expression<?>... args);

I figured out how to do this with Hibernate-jpa-mysql:
1.) created a GroupConcatFunction class extending org.hibernate.dialect.function.SQLFunction (this is for single column group_concat for now)
public class GroupConcatFunction implements SQLFunction {
#Override
public boolean hasArguments() {
return true;
}
#Override
public boolean hasParenthesesIfNoArguments() {
return true;
}
#Override
public Type getReturnType(Type firstArgumentType, Mapping mapping)
throws QueryException {
return StandardBasicTypes.STRING;
}
#Override
public String render(Type firstArgumentType, List arguments,
SessionFactoryImplementor factory) throws QueryException {
if (arguments.size() != 1) {
throw new QueryException(new IllegalArgumentException(
"group_concat shoudl have one arg"));
}
return "group_concat(" + arguments.get(0) + ")";
}
}
2.) i created the CustomMySql5Dialect class extending org.hibernate.dialect.MySQL5Dialect and registered the group_concat class created in step 1
3.) On the app context, i updated the jpaVendorAdapter to use the CustomMySql5Dialect as the databasePlatform
4.) Finally to use it
criteriaBuilder.function("group_concat", String.class,
sampleRoot.get("sampleColumnName"))

Simple solution: instead of creating the whole class, just use SQLFunctionTemplate.
new SQLFunctionTemplate(StandardBasicTypes.STRING, "group_concat(?1)")
and then register this function in your own SQL dialect (eg. in constructor)
public class MyOwnSQLDialect extends MySQL5Dialect {
public MyOwnSQLDialect() {
super();
this.registerFunction("group_concat", new SQLFunctionTemplate(StandardBasicTypes.STRING, "group_concat(?1)"));
}
}

Suggested property:
spring.jpa.properties.hibernate.metadata_builder_contributor = com.inn.core.generic.utils.SqlFunctionsMetadataBuilderContributor
and class:
import org.hibernate.boot.MetadataBuilder;
import org.hibernate.boot.spi.MetadataBuilderContributor;
import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Component;
#Component
public class SqlFunctionsMetadataBuilderContributor implements MetadataBuilderContributor {
#Override
public void contribute(MetadataBuilder metadataBuilder) {
metadataBuilder.applySqlFunction("config_json_extract",
new StandardSQLFunction("json_extract", StandardBasicTypes.STRING));
metadataBuilder.applySqlFunction("JSON_UNQUOTE",
new StandardSQLFunction("JSON_UNQUOTE", StandardBasicTypes.STRING));
metadataBuilder.applySqlFunction("group_concat",
new StandardSQLFunction("group_concat", StandardBasicTypes.STRING));
}
}

Related

JPA Criteria Group Concat Function Multiple Results

I need to group_concat with criteria builder, it's actually working but it's multiplying with the number of results in this group_concat... Here's the idea:
If the join result gives me 3 results, the string that the group_concat multiply the group_concat total three times...
code:
cb.builder.function("group_concat", String.class, cb.joinDocumentWithSigners.get("email")),
The join select with DocumentSigners:
The criteria result:
marinafake#contratos.com,testemunha_fake#contratos.com,silvzz234#erp.net,marinafake#contratos.com,testemunha_fake#contratos.com,silvzz234#erp.net,marinafake#contratos.com,testemunha_fake#contratos.com,silvzz234#erp.net
The relation between these guys is 1 to many. The criteriaBuilder is on a CrontractDocument.class that has a List of DocumentSigner, this DocumentSigner has an email field. So I need to concat all of these emails in the ContractDocumentView build...
GroupConcatFunction:
public class GroupConcatFunction implements SQLFunction {
#Override
public boolean hasArguments() {
return true;
}
#Override
public boolean hasParenthesesIfNoArguments() {
return true;
}
#Override
public Type getReturnType(Type firstArgumentType, Mapping mapping)
throws QueryException {
return StandardBasicTypes.STRING;
}
#Override
public String render(Type firstArgumentType, List arguments,
SessionFactoryImplementor factory) throws QueryException {
if (arguments.size() != 1) {
throw new QueryException(new IllegalArgumentException(
"group_concat shoudl have one arg"));
}
return "group_concat(" + arguments.get(0) + ")";
}
}
CustomMySQlDialect:
public class CustomMySQLDialect extends MySQL5Dialect {
public CustomMySQLDialect() {
super();
registerFunction("group_concat", new GroupConcatFunction());
}
}

Mapping a field using existing target value (Mapstruct)

i have a custom case that some of my dto's have a field of type X, and i need to map this class to Y by using a spring service method call(i do a transactional db operation and return an instance of Y). But in this scenario i need to use existing value of Y field. Let me explain it by example.
// DTO
public class AnnualLeaveRequest {
private FileInfoDTO annualLeaveFile;
}
//ENTITY
public class AnnualLeave {
#OneToOne
private FileReference annualLeaveFile;
}
#Mapper
public abstract class FileMapper {
#Autowired
private FileReferenceService fileReferenceService;
public FileReference toFileReference(#MappingTarget FileReference fileReference, FileInfoDTO fileInfoDTO) {
return fileReferenceService.updateFile(fileInfoDTO, fileReference);
}
}
//ACTUAL ENTITY MAPPER
#Mapper(uses = {FileMapper.class})
public interface AnnualLeaveMapper {
void updateEntity(#MappingTarget AnnualLeave entity, AnnualLeaveRequest dto);
}
// WHAT IM TRYING TO ACHIEVE
#Component
public class MazeretIzinMapperImpl implements tr.gov.hmb.ikys.personel.izinbilgisi.mazeretizin.mapper.MazeretIzinMapper {
#Autowired
private FileMapper fileMapper;
#Override
public void updateEntity(AnnualLeave entity, AnnualLeaveUpdateRequest dto) {
entity.setAnnualLeaveFile(fileMapper.toFileReference(dto.getAnnualLeaveFile(), entity.getAnnualLeaveFile()));
}
}
But mapstruct ignores the result of "FileReference toFileReference(#MappingTarget FileReference fileReference, FileInfoDTO fileInfoDTO) " and does not map the result of it to the actual entity's FileReference field. Do you have any idea for resolving this problem?
Question
How do I replace the annualLeaveFile property while updating the AnnualLeave entity?
Answer
You can use expression to get this result. For example:
#Autowired
FileMapper fileMapper;
#Mapping( target = "annualLeaveFile", expression = "java(fileMapper.toFileReference(entity.getAnnualLeaveFile(), dto.getAnnualLeaveFile()))" )
abstract void updateEntity(#MappingTarget AnnualLeave entity, AnnualLeaveRequest dto);
MapStruct does not support this without expression usage. See the end of the Old analysis for why.
Alternative without expression
Instead of fixing it in the location where FileMapper is used, we fix it inside the FileMapper itself.
#Mapper
public abstract class FileMapper {
#Autowired
private FileReferenceService fileReferenceService;
public void toFileReference(#MappingTarget FileReference fileReference, FileInfoDTO fileInfoDTO) {
FileReference wanted = fileReferenceService.updateFile(fileInfoDTO, fileReference);
updateFileReference(fileReference, wanted);
}
// used to copy the content of the service one to the mapstruct one.
abstract void updateFileReference(#MappingTarget FileReference fileReferenceTarget, FileReference fileReferenceFromService);
}
Old analysis
The following is what I notice:
(Optional) your FileMapper class is not a MapStruct mapper. This can just be a normal class annotated with #Component, since it does not have any unimplemented abstract methods. (Does not affect code generation of the MazeretIzinMapper implementation)
(Optional, since you have this project wide configured) you do not have componentModel="spring" in your #Mapper definition, maybe you have this configured project wide, but that is not mentioned. (required for the #Autowired annotation, and #Component on implementations)
Without changing anything I already get a working result as you want it to be, but for non-update methods (not listed in your question, but was visible on the gitter page where you also requested help) the FileMapper as is will not be used. It requires an additional method that takes only 1 argument: public FileReference toFileReference(FileInfoDTO fileInfoDTO)
(Edit) to get rid of the else statement with null value handling you can add nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE to the #Mapper annotation.
I've run a test and with 1.5.0.Beta2 and 1.4.2.Final I get the following result with the thereafter listed FileMapper and MazeretIzinMapper classes.
Generated mapper implementation
#Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2022-03-11T18:01:30+0100",
comments = "version: 1.4.2.Final, compiler: Eclipse JDT (IDE) 1.4.50.v20210914-1429, environment: Java 17.0.1 (Azul Systems, Inc.)"
)
#Component
public class MazeretIzinMapperImpl implements MazeretIzinMapper {
#Autowired
private FileMapper fileMapper;
#Override
public AnnualLeave toEntity(AnnualLeaveRequest dto) {
if ( dto == null ) {
return null;
}
AnnualLeave annualLeave = new AnnualLeave();
annualLeave.setAnnualLeaveFile( fileMapper.toFileReference( dto.getAnnualLeaveFile() ) );
return annualLeave;
}
#Override
public void updateEntity(AnnualLeave entity, AnnualLeaveRequest dto) {
if ( dto == null ) {
return;
}
if ( dto.getAnnualLeaveFile() != null ) {
if ( entity.getAnnualLeaveFile() == null ) {
entity.setAnnualLeaveFile( new FileReference() );
}
fileMapper.toFileReference( entity.getAnnualLeaveFile(), dto.getAnnualLeaveFile() );
}
}
}
Source classes
Mapper
#Mapper( componentModel = "spring", uses = { FileMapper.class }, nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE )
public interface MazeretIzinMapper {
AnnualLeave toEntity(AnnualLeaveRequest dto);
void updateEntity(#MappingTarget AnnualLeave entity, AnnualLeaveRequest dto);
}
FileMapper component
#Mapper
public abstract class FileMapper {
#Autowired
private FileReferenceService fileReferenceService;
public FileReference toFileReference(#MappingTarget FileReference fileReference, FileInfoDTO fileInfoDTO) {
return fileReferenceService.updateFile( fileInfoDTO, fileReference );
}
public FileReference toFileReference(FileInfoDTO fileInfoDTO) {
return toFileReference( new FileReference(), fileInfoDTO );
}
// other abstract methods for MapStruct mapper generation.
}
Why the exact wanted code will not be generated
When generating the mapping code MapStruct will use the most generic way to do this.
An update mapper has the following criteria:
The #MappingTarget annotated argument will always be updated.
It is allowed to have no return type.
the generic way to update a field is then as follows:
// check if source has the value.
if (source.getProperty() != null) {
// Since it is allowed to have a void method for update mappings the following steps are needed:
// check if the property exists in the target.
if (target.getProperty() == null) {
// if it does not have the value then create it.
target.setProperty( new TypeOfProperty() );
}
// now we know that target has the property so we can call the update method.
propertyUpdateMappingMethod( target.getProperty(), source.getProperty() );
// The arguments will match the order as specified in the other update method. in this case the #MappingTarget annotated argument is the first one.
} else {
// default behavior is to set the target property to null, you can influence this with nullValuePropertyMappingStrategy.
target.setProperty( null );
}

Springboot Generic JPA AttributeConverter

I have below sample code, i am trying to write generic JPA converter which could convert,
Collection of user defined objects to Json
vice versa
Below is sample code I was trying to achieve the result but looks like it's not correct.
Please take a look.
To be more clear i need like below
List To string
Json String to List
Please suggest
#Converter(autoApply = true)
public class SetJsonConverter<E extends Collections> implements AttributeConverter<E, Object> {
#Override
public Object convertToDatabaseColumn(E e) {
return null;
}
#Override
public E convertToEntityAttribute(Object o) {
ObjectMapper objectMapper=new ObjectMapper();
return null;
}
}
JPA will not automatically handle generic converters. Each collection type and element type will require subclassing. You will need to define the base converter the following way:
public class AbstractJsonConverter<T, C extends Collection<T>> implements AttributeConverter<C, String> {
private final ObjectMapper objectMapper;
private final TypeReference<C> collectionType;
public AbstractJsonConverter(ObjectMapper objectMapper, Class<T> elementType, TypeReference<C> collectionType) {
this.objectMapper = objectMapper;
this.collectionType = collectionType;
}
#Override
public String convertToDatabaseColumn(C collection) {
try {
return objectMapper.writeValueAsString(collection);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
#Override
public C convertToEntityAttribute(String jsonString) {
try {
return objectMapper.readValue(jsonString, collectionType);
} catch (IOException e) {
throw new RuntimeException();
}
}
}
You then define specific converters as:
#Converter(autoApply = true)
public class UserSetConverter extends AbstractJsonConverter<User, Set<User>> {
public UserSetConverter(ObjectMapper objectMapper) {
super(objectMapper, User.class, new TypeReference<Set<User>>() {});
}
}

How to work with PGpoint for Geolocation using PostgreSQL?

I found a lot of answers that suggest to use spatial data with Hibernate spatial data geolocation but I want to know if that is the best because I found that PostgreSQL works with PGpoint for GeoLocation. I implemented but it doesn't work because doesn't save.
ERROR: column "location" is of type point but expression is of type character varying
I have the same question but nobody answered him. So let me add other question below if nobody knows about this question.
As suggestion I'd want to know what is the best way to use Geo data on Spring Boot Context
Thanks! have a good day.
There is no way to save/update/get/ PGpoint object directly,
Then you have to create your own user type for supporting PGpoint in order to convert it, before this is saved, UserType is a class of Hibernate which allows to create custom type in order to convert it before to save on database.
Here is code that you need to implement:
First: Need to create a class that implements of UserType:
public class PGPointType implements UserType {
#Override
public int[] sqlTypes() {
return new int[]
{
Types.VARCHAR
};
}
#SuppressWarnings("rawtypes")
#Override
public Class<PGpoint> returnedClass() {
return PGpoint.class;
}
#Override
public boolean equals(Object obj, Object obj1) {
return ObjectUtils.equals(obj, obj1);
}
#Override
public int hashCode(Object obj) {
return obj.hashCode();
}
#Override
public Object nullSafeGet(ResultSet resultSet, String[] names, SharedSessionContractImplementor sharedSessionContractImplementor, Object o) throws SQLException {
if (names.length == 1) {
if (resultSet.wasNull() || resultSet.getObject(names[0]) == null) {
return null;
} else {
return new PGpoint(resultSet.getObject(names[0]).toString());
}
}
return null;
}
#Override
public void nullSafeSet(PreparedStatement statement, Object value, int index, SharedSessionContractImplementor sharedSessionContractImplementor) throws SQLException {
if (value == null) {
statement.setNull(index, Types.OTHER);
} else {
statement.setObject(index, value, Types.OTHER);
}
}
#Override
public Object deepCopy(Object obj) {
return obj;
}
#Override
public boolean isMutable() {
return Boolean.FALSE;
}
#Override
public Serializable disassemble(Object obj) {
return (Serializable) obj;
}
#Override
public Object assemble(Serializable serializable, Object obj) {
return serializable;
}
#Override
public Object replace(Object obj, Object obj1, Object obj2) {
return obj;
}
}
Second: Need to add on entity header #TypeDef annotation, add a name and the PGPointType that you created it and on some field header of type PGpoint, add #Type annotation with the name that you created it:
#TypeDef(name = "type", typeClass = PGPointType.class)
#Entity
public class Entity {
#Type(type = "type")
private PGpoint pgPoint;
// Getters and setters
}
Kind regards.

Simplest way to migrate from older JPA to newer with version with Query.getResultList() returning List<Object[]> instead of List<Vector>

We're currently researching the best way to upgrade from Toplink 2.1-60f to EclipseLink 2.6. The project is somewhat large and most of the manual work would have to be done in parts of the code where we are using NativeQuery. Query.getResultList() result differs between the two JPA-implementations as TopLink returns a List<Vector> and EclipseLink on the other hand returns a List<Object[]>. The code is unfortunately therefore littered with List<Vector> references.
Part of the solution would be to convert the result from list array to a list of vectors. Instead of doing this in all the numerous places manually, I was thinking we could use AspectJ to intercept the getResultList() calls and convert the return values. Is this a viable solution? Has anyone implemented similar solutions? We're using Maven as our build tool.
Thanks in advance!
My suggestion is: Use a good IDE and refactor your code!
But because you asked for an AOP solution, here is a self-consistent AspectJ example. As I have never used JPA, I will just recreate your situation as a little abstraction.
Abstract Query base implementation with lots of dummy methods:
package de.scrum_master.persistence;
import java.util.*;
import javax.persistence.*;
public abstract class MyBaseQueryImpl implements Query {
#Override public int executeUpdate() { return 0; }
#Override public int getFirstResult() { return 0; }
#Override public FlushModeType getFlushMode() { return null; }
#Override public Map<String, Object> getHints() { return null; }
#Override public LockModeType getLockMode() { return null; }
#Override public int getMaxResults() { return 0; }
#Override public Parameter<?> getParameter(String arg0) { return null; }
#Override public Parameter<?> getParameter(int arg0) { return null; }
#Override public <T> Parameter<T> getParameter(String arg0, Class<T> arg1) { return null; }
#Override public <T> Parameter<T> getParameter(int arg0, Class<T> arg1) { return null; }
#Override public <T> T getParameterValue(Parameter<T> arg0) { return null; }
#Override public Object getParameterValue(String arg0) { return null; }
#Override public Object getParameterValue(int arg0) { return null; }
#Override public Set<Parameter<?>> getParameters() { return null; }
#Override public Object getSingleResult() { return null; }
#Override public boolean isBound(Parameter<?> arg0) { return false; }
#Override public Query setFirstResult(int arg0) { return null; }
#Override public Query setFlushMode(FlushModeType arg0) { return null; }
#Override public Query setHint(String arg0, Object arg1) { return null; }
#Override public Query setLockMode(LockModeType arg0) { return null; }
#Override public Query setMaxResults(int arg0) { return null; }
#Override public <T> Query setParameter(Parameter<T> arg0, T arg1) { return null; }
#Override public Query setParameter(String arg0, Object arg1) { return null; }
#Override public Query setParameter(int arg0, Object arg1) { return null; }
#Override public Query setParameter(Parameter<Calendar> arg0, Calendar arg1, TemporalType arg2) { return null; }
#Override public Query setParameter(Parameter<Date> arg0, Date arg1, TemporalType arg2) { return null; }
#Override public Query setParameter(String arg0, Calendar arg1, TemporalType arg2) { return null; }
#Override public Query setParameter(String arg0, Date arg1, TemporalType arg2) { return null; }
#Override public Query setParameter(int arg0, Calendar arg1, TemporalType arg2) { return null; }
#Override public Query setParameter(int arg0, Date arg1, TemporalType arg2) { return null; }
#Override public <T> T unwrap(Class<T> arg0) { return null; }
}
The only method missing is getResultList(), so now let us provide two different implementations for it, extending the abstract base implementation:
Concrete Query implementation returning List<Vector>:
This emulates your TopLink class.
package de.scrum_master.persistence;
import java.util.*;
public class VectorQuery extends MyBaseQueryImpl {
#Override
public List getResultList() {
List<Vector<String>> resultList = new ArrayList<>();
Vector<String> result = new Vector<>();
result.add("foo"); result.add("bar");
resultList.add(result);
result = new Vector<>();
result.add("one"); result.add("two");
resultList.add(result);
return resultList;
}
}
Concrete Query implementation returning List<Object[]>:
This emulates your EclipseLink class.
package de.scrum_master.persistence;
import java.util.*;
public class ArrayQuery extends MyBaseQueryImpl {
#Override
public List getResultList() {
List<Object[]> resultList = new ArrayList<>();
Object[] result = new Object[] { "foo", "bar" };
resultList.add(result);
result = new Object[] { "one", "two" };
resultList.add(result);
return resultList;
}
}
Driver application:
The application creates queries of both concrete subtypes, each time assuming that the list elements will be vectors.
package de.scrum_master.app;
import java.util.*;
import de.scrum_master.persistence.*;
public class Application {
public static void main(String[] args) {
List<Vector<?>> resultList;
resultList = new VectorQuery().getResultList();
for (Vector<?> result : resultList)
System.out.println(result);
resultList = new ArrayQuery().getResultList();
for (Vector<?> result : resultList)
System.out.println(result);
}
}
Console log without aspect:
[foo, bar]
[one, two]
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.Vector
at de.scrum_master.app.Application.main(Application.java:15)
Uh-oh! This is exactly your problem, right? Now what can we do about it if we absolutely refuse to refactor? We abuse AOP for patching up the legacy code. (Please don't do it, but you can if you absolutely want to.)
AspectJ query result adapter:
Disregarding usage of raw types and other ugly stuff, here is my proof of concept:
package de.scrum_master.aspect;
import java.util.*;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
#Aspect
public class QueryResultAdapter {
#Around("call(* javax.persistence.Query.getResultList())")
public List<Vector> transformQueryResult(ProceedingJoinPoint thisJoinPoint) throws Throwable {
System.out.println(thisJoinPoint);
List result = (List) thisJoinPoint.proceed();
if (result != null && result.size() > 0 && result.get(0) instanceof Vector)
return result;
System.out.println("Transforming arrays to vectors");
List<Vector> transformedResult = new ArrayList<Vector>();
for (Object[] arrayItem : (List<Object[]>) result)
transformedResult.add(new Vector(Arrays.asList(arrayItem)));
return transformedResult;
}
}
Console log with aspect:
call(List de.scrum_master.persistence.VectorQuery.getResultList())
[foo, bar]
[one, two]
call(List de.scrum_master.persistence.ArrayQuery.getResultList())
Transforming arrays to vectors
[foo, bar]
[one, two]
Et voilĂ  - you can do ugly stuff and other things it was not invented for with AOP. ;-)