Announcement Announcement Module
No announcement yet.
Spring Data JPA - Return Multiple entities or user defined object Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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 …")

    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.



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

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

      IllegalArgumentException: Not an managed type:
      at org.hibernate.ejb.metamodel.MetamodelImpl.managedT ype(
      at aMetamodelEntityInformation.<init>(JpaMetamodelEnt
      at aEntityInformationSupport.getMetadata(JpaEntityInf
      at aRepositoryFactory.getEntityInformation(JpaReposit
      at aRepositoryFactory.getTargetRepository(JpaReposito