JPA Repository and constructor with varargs

642
April 21, 2018, at 4:12 PM

It is possible to use constructor of varargs in @Query (org.springframework.data.jpa.repository). What I mean is class like this:

public class EntityDTO {
  public EntityDTO(Long id, LocalDate... dates) {
    // some code here where dates 
    //which are converted to List<Pair<LocalDate, LocalDate> datesList
  }
}
public interface CustomRepository extends JpaRepository<Entity, Long> {
  @Query("SELECT new package.path.EntityDTO(id, date1, date2, date2) FROM Entity")
  List<EntityDTO> findEntityList();
}

Now there is an error like this:

Unsatisfied dependency expressed through constructor parameter 3
 Validation failed for query for method public abstract java.util.List CustomRepository.findEntityList(java.lang.Long,java.time.LocalDate,java.time.LocalDate)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: 
 Unable to locate appropriate constructor on class [EntityDTO]. Expected arguments are: long, java.time.LocalDate, java.time.LocalDate, java.time.LocalDate, java.time.LocalDate

It is only query example because dto can have id and LocalDate parameter from 2 to 12-14 in constructor, dates can be from different tables (entities). It depends from query. I want class more generic for this purpose. Query is much more complicated than in this example but I'm interested in some kind of constructor which give me possibility to create something similar that I described in EntityDTO constructor in comment. It is possible to use varargs in @Query?

Rent Charter Buses Company
READ ALSO
implementation of template method pattern in the real world scenario

implementation of template method pattern in the real world scenario

I have requirement as below to build report building system

186
Data in the NatTable is not displayed

Data in the NatTable is not displayed

Objects are added, but they aren't displayed in the tableAnd the place for these two added objects in the table is allocated, but all cells are empty

157
Java - How to remove a page in jasper reports that has only headers and no data available?

Java - How to remove a page in jasper reports that has only headers and no data available?

I have been seeing a strange issue in jasperIt is when any of my columns are overflowing to the next page

173
How to compare two object instances

How to compare two object instances

I have two similar json filesI created a java object to hold this type of json when received as response

205