#330 - Polished README on JPA 2.1 examples for manual result set mappings.
This commit is contained in:
@@ -55,35 +55,35 @@ Calling `UserRepository.plus1BackedByOtherNamedStoredProcedure(…)` will execut
|
|||||||
|
|
||||||
## Support for custom SqlResultSetMapping with ConstructorResult
|
## Support for custom SqlResultSetMapping with ConstructorResult
|
||||||
|
|
||||||
Sometimes, e.g. for analytics, it is handy to be able to return a different entity result type from a Repository query method than the base Repository entity type or an interface based projection.
|
Sometimes, e.g. for analytics, it is handy to be able to return a different entity result type from a Repository query method than the base Repository entity type or an interface based projection.
|
||||||
|
|
||||||
In those cases one can leverage JPAs `SqlResultSetMapping` feature to map the columns of the result of a query to different fields.
|
In those cases one can leverage JPAs `SqlResultSetMapping` feature to map the columns of the result of a query to different fields.
|
||||||
|
|
||||||
JPA 2.1 introduced the new `SqlResultSetMapping` type `ConstructorResult` which allows to map columns of a result set row to a constructor invocation
|
JPA 2.1 introduced the new `SqlResultSetMapping` type `ConstructorResult` which allows to map columns of a result set row to a constructor invocation
|
||||||
which can be nicely used in combination with Value Objects.
|
which can be nicely used in combination with Value Objects.
|
||||||
|
|
||||||
This example shows how to define a custom `SqlResultSetMapping` for the result of an analytical native query that reports the usage summary for a set of Subscriptions.
|
This example shows how to define a custom `SqlResultSetMapping` for the result of an analytical native query that reports the usage summary for a set of Subscriptions.
|
||||||
|
|
||||||
`SqlResultSetMapping` definition on the Subscription entity class:
|
`SqlResultSetMapping` definition on the `Subscription` entity class:
|
||||||
```java
|
```java
|
||||||
@Entity
|
@Entity
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@SqlResultSetMapping(
|
@SqlResultSetMapping(
|
||||||
name="subscriptionSummary",
|
name = "subscriptionSummary",
|
||||||
classes = @ConstructorResult(
|
classes = @ConstructorResult(
|
||||||
targetClass = SubscriptionSummary.class,
|
targetClass = SubscriptionSummary.class,
|
||||||
columns={
|
columns = {
|
||||||
@ColumnResult(name="productName", type=String.class),
|
@ColumnResult(name = "productName", type = String.class),
|
||||||
@ColumnResult(name="subscriptions", type=long.class)
|
@ColumnResult(name = "subscriptions", type = long.class)
|
||||||
}))
|
}))
|
||||||
@NamedNativeQuery(
|
@NamedNativeQuery(
|
||||||
name="Subscription.findAllSubscriptionSummaries",
|
name = "Subscription.findAllSubscriptionSummaries",
|
||||||
query="select product_name as productName, count(user_id) as subscriptions from subscription group by product_name order by productName",
|
query = "select product_name as productName, count(user_id) as subscriptions from subscription group by product_name order by productName",
|
||||||
resultSetMapping = "subscriptionSummary"
|
resultSetMapping = "subscriptionSummary"
|
||||||
)
|
)
|
||||||
@Data
|
@Data
|
||||||
public class Subscription {
|
public class Subscription {
|
||||||
...
|
…
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -92,9 +92,8 @@ public class Subscription {
|
|||||||
@Value
|
@Value
|
||||||
public class SubscriptionSummary {
|
public class SubscriptionSummary {
|
||||||
|
|
||||||
private final String product;
|
String product;
|
||||||
|
Long usageCount;
|
||||||
private final Long usageCount;
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -102,6 +101,7 @@ The `SubscriptionRepository` declares the custom query method `findAllSubscripti
|
|||||||
```java
|
```java
|
||||||
interface SubscriptionRepository extends CrudRepository<Subscription,Long> {
|
interface SubscriptionRepository extends CrudRepository<Subscription,Long> {
|
||||||
|
|
||||||
List<SubscriptionSummary> findAllSubscriptionSummaries();
|
@Query(nativeQuery = true)
|
||||||
|
List<SubscriptionSummary> findAllSubscriptionSummaries();
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
Reference in New Issue
Block a user