Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project fields in queries #142

Closed
sureshp31 opened this issue Feb 19, 2018 · 10 comments
Closed

Project fields in queries #142

sureshp31 opened this issue Feb 19, 2018 · 10 comments

Comments

@sureshp31
Copy link

sureshp31 commented Feb 19, 2018

Is it possible to project fields, could not find any documentation related to this:

I want to project only the "id" and "name" fields from this query.

@EnableScan
public interface ProductInfoRepository extends
  CrudRepository<ProductInfo, String> {
     
    List<ProductInfo> findById(String id);
}
@astetsa
Copy link

astetsa commented Feb 19, 2018

List<ProductInfo> findByIdAndName(String id, String name);

@sureshp31
Copy link
Author

@astetsa this is for querying based on id and name right?, i meant projection of fields in the returned result set. eg: if product_info has id,name,address,contact_details, i want the result set to have only id and name.

@astetsa
Copy link

astetsa commented Feb 19, 2018

So. You can implement two repositories. First is ProductInfo based. Second is ProductInfoShort based. First is used to save data. Second is used to find and return short data.

@derjust
Copy link
Owner

derjust commented Feb 19, 2018

Yes that's the approach I'd also chose with two repositories.
You can derive ProductInfo from ProductInfoShort to not duplicate the annotations etc.

@sureshp31
Copy link
Author

thanks got it. is it possible to have projections like its available in other spring data modules like mongo where we can pass the fields to be returned.

like in 2.2:
http://www.baeldung.com/spring-data-mongodb-projections-aggregations

@derjust
Copy link
Owner

derjust commented Feb 21, 2018

Yes totally possible and easier than expected. An implementation would be similar to MongoDB via an projection-expression that is mapped from a @Query. To stick with the example from the AWS documentation that would look like this:

@Query(fields="Title") // comma separated list as per AWS documentation above
public List<ProductCatalog> findById(Long id);

which would load only the Title (and the key) attribute.

Might be also worth to look in parallel in addressing #78.

Possible workaround until then:

  • Create a 'short' version of your object and have a second repository (as mentioned above)
  • Create a GSI with projected attributes and create query method for that

@derjust
Copy link
Owner

derjust commented Mar 22, 2018

Merged & will be available in 5.0.3

@derjust derjust closed this as completed Mar 22, 2018
5.0.3 - Maintenance & security release automation moved this from To do to Done Mar 22, 2018
@sureshp31
Copy link
Author

thanks a lot

@mr-goez
Copy link

mr-goez commented Aug 27, 2018

The annotation seems not to work properly, I'm trying to declare fields, I want to have projected but when performing a GET-Request I get the JSON-Doc with just null values.
When I let the annotation without declaring any fields, it gives me only those that are in the method name and teh rest is null again.

Any suggestions?

@arthurhenning
Copy link

I'm also having issues with the @Query annotation used in combination with @EnableScan and findAll method. @derjust is this combination not supported?

`@DynamoDBTable(tableName = "vcc-globalprovisioning-PrimaryTenant-v1")
public class PrimaryTenant {

@Id
@DynamoDBHashKey
private String tenantId;

@DynamoDBIndexHashKey(globalSecondaryIndexName = "gsi-primaryTenant-customerId")
private String customerId;

@DynamoDBIndexHashKey(globalSecondaryIndexName = "gsi-primaryTenant-siteId")
private String siteId;

@DynamoDBIndexHashKey(globalSecondaryIndexName = "gsi-primaryTenant-clusterId")
private String clusterId;

@DynamoDBTypeConvertedEnum
private TenantSourceType source;

private String customerName;
private String pbxName;
private String parentAccountId;

private boolean isDRActivated;
private DRTenant drTenant;

}`

`public interface PrimaryTenantRepository extends DynamoDBCrudRepository<PrimaryTenant, String> {

@EnableScan
@EnableScanCount
@Query(fields = "tenantId,customerId,customerName")
Page<PrimaryTenant> findAll(Pageable pageable);

}`

Calling this method returns also other fields than the enumerated ones in the @Query.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Development

No branches or pull requests

5 participants