Announcement Announcement Module
Collapse
No announcement yet.
Spring Data JPA - Return Multiple entities or user defined object Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Data JPA - Return Multiple entities or user defined object

    Using Spring Data JPA repositories is it possible to return a user defined object that encapsulates 2 entities. In other words is there a way to return a Tuple which is part of the JPA criteria API using a repository by passing in a Specification.

    Or for scenarios like this, is it better to work with the entity manager directly?

  • #2
    Currently it's not possible without defining a DTO containing the two entities and then setting up a query something like this:

    @Query("select new com.acme.MyDto(f, b) from Foo f, Bar b where …")
    List<MyDto>myQueryMethod(…);

    There's a ticket [0] open in Spring Data Commons actually requesting that feature. In the course of the fix for [1] we will introduce a similar way to do projections when using the Specification abstraction.

    [0] https://jira.springsource.org/browse/DATACMNS-89
    [1] https://jira.springsource.org/browse/DATAJPA-51

    Comment


    • #3
      But what if MtDto is not a managed type? spring data jpa would not understand MtDto.

      I put com.company.domain as the scanned package (i.e. MtDto is scanned), but somehow jpa still doesn't like it.

      IllegalArgumentException: Not an managed type: com.company.domain.MtDto
      at org.hibernate.ejb.metamodel.MetamodelImpl.managedT ype(MetamodelImpl.java:200)
      at org.springframework.data.jpa.repository.support.Jp aMetamodelEntityInformation.<init>(JpaMetamodelEnt ityInformation.java:61)
      at org.springframework.data.jpa.repository.support.Jp aEntityInformationSupport.getMetadata(JpaEntityInf ormationSupport.java:65)
      at org.springframework.data.jpa.repository.support.Jp aRepositoryFactory.getEntityInformation(JpaReposit oryFactory.java:146)
      at org.springframework.data.jpa.repository.support.Jp aRepositoryFactory.getTargetRepository(JpaReposito ryFactory.java:84)

      Comment

      Working...
      X