Skip to content

Add support for simple type projections #41

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

Closed
rj-hwang opened this issue Dec 28, 2018 · 10 comments
Closed

Add support for simple type projections #41

rj-hwang opened this issue Dec 28, 2018 · 10 comments
Labels
status: ideal-for-contribution An issue that a contributor can help us with type: enhancement A general enhancement
Milestone

Comments

@rj-hwang
Copy link

I'm using spring-data-r2dbc-1.0.0.M1 with r2dbc-postgresql-1.0.0.M6. See below code, failed to select single field value.

Code :

public class Dream {
  @Id private Integer id;
  private String name;
  ...
}
public interface DreamRepository extends ReactiveCrudRepository<Dream, Integer> {
  @Query("select name from dream")
  Flux<String> findAllName();
}

Error message :

org.springframework.data.mapping.MappingException: Couldn't find PersistentEntity for type class java.lang.String!

  at org.springframework.data.mapping.context.MappingContext.getRequiredPersistentEntity(MappingContext.java:79)
  at org.springframework.data.r2dbc.function.DefaultReactiveDataAccessStrategy.getRequiredPersistentEntity(DefaultReactiveDataAccessStrategy.java:242)
  at org.springframework.data.r2dbc.function.DefaultReactiveDataAccessStrategy.getRowMapper(DefaultReactiveDataAccessStrategy.java:228)
  at org.springframework.data.r2dbc.function.DefaultDatabaseClient$DefaultTypedExecuteSpec.<init>(DefaultDatabaseClient.java:476)
  at org.springframework.data.r2dbc.function.DefaultDatabaseClient.createTypedExecuteSpec(DefaultDatabaseClient.java:223)
  at org.springframework.data.r2dbc.function.DefaultDatabaseClient$DefaultGenericExecuteSpec.as(DefaultDatabaseClient.java:408)
  at org.springframework.data.r2dbc.repository.query.AbstractR2dbcQuery.execute(AbstractR2dbcQuery.java:106)
  at org.springframework.data.r2dbc.repository.query.AbstractR2dbcQuery.execute(AbstractR2dbcQuery.java:84)
  at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:605)
  ...
  at com.sun.proxy.$Proxy38.findAllName(Unknown Source)
  at tech.simter.start.r2dbc.spring.dao.FindAllNameMethodImplTest.test(FindAllNameMethodImplTest.java:40)
  ...

My unit test code is FindAllNameMethodImplTest.java

@rj-hwang
Copy link
Author

The simular code is working on spring-data-jpa. Such as DATAJPA-391

@mp911de
Copy link
Member

mp911de commented Dec 28, 2018

R2DBC repositories do not support projections yet.

@mp911de mp911de changed the title Select single field not working Add support for projections Dec 28, 2018
@mp911de mp911de added the type: enhancement A general enhancement label Dec 28, 2018
@mp911de
Copy link
Member

mp911de commented Dec 28, 2018

I repurposed this ticket to add projections. Projections should support:

  • Single field results by returning a simple type (i.e. String, Number, …)
  • DTO projections
  • Interface projections

@rj-hwang
Copy link
Author

Hope to realize it soon.

@mp911de mp911de added status: ideal-for-contribution An issue that a contributor can help us with type: bug A general bug type: enhancement A general enhancement and removed type: enhancement A general enhancement type: bug A general bug labels Feb 21, 2019
@mp911de mp911de changed the title Add support for projections Add support for simple type projections Feb 21, 2019
@mp911de
Copy link
Member

mp911de commented Feb 21, 2019

I revisited the topic and realized that we have support for DTO and interface projections. The missing bit is simple type projection (selecting plain String and such).

In Cassandra, we have solved the issue by registering Converters from Row to the desired type T. We should provide the same for Spring Data R2DBC (see https://github.com/spring-projects/spring-data-cassandra/blob/e6b5c5e7a2af5c020526e6d998d5950b74163937/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/convert/CassandraConverters.java#L57-L65 as reference). EntityRowMapper is currently being refactored via #61 so the scope of this ticket is just providing the necessary converters. Integration can happen once #61 is merged.

@uaihebert
Copy link
Contributor

uaihebert commented Feb 21, 2019

hello @mp911de

I have added something really close to the cassandra solution.

I still new in this project, so I just submitted a simple class so that you can point me the correct direction. once I get on tracks I will add the tests.

I got a few questions:

  1. how should we convert the data? I used the same converter API. but the row from casandra is different than the row that we got in the project.
  2. how should the cast e effectively done? right now I am doing simples java casts like: (Date) row.get(0). Should I convert to String and parse it later? I just need some reference, I could not find anything related.
  3. unrelated to this PR: to make this successful build of this project I had to compile: spring-data-build, spring-data-commons, spring-data-jdbc, spring-hateoas, spring-plugin. could I add this information into the README? it took some minutes to figure it out what was missing.

@mp911de
Copy link
Member

mp911de commented Feb 22, 2019

Thanks a lot for getting involved into this ticket. Here are the answers for your questions:

  1. We should use the typed R2DBC API, row.get(0, LocalDate.class) instead of the untyped one (row.get(0)) so the driver can provide native types and we do not require downcasting.
  2. See 1.
  3. Why had you to compile the dependencies? The pom.xml points to our snapshot repositories:
	<repositories>
		<repository>
			<id>spring-libs-snapshot</id>
			<url>https://repo.spring.io/libs-snapshot</url>
		</repository>
	</repositories>

	<pluginRepositories>
		<pluginRepository>
			<id>spring-plugins-snapshot</id>
			<url>https://repo.spring.io/plugins-snapshot</url>
		</pluginRepository>
	</pluginRepositories>

If downloading from repo.spring.io does not work, then we should investigate why this is and fix the cause. Can you provide some more detail information (e.g. compile error message) so we can trace the issue?

@uaihebert
Copy link
Contributor

I just updated the PR.

well, it was saying that some dependencies were not found.

my clone history to make a successful build was like this:
image

@uaihebert
Copy link
Contributor

uaihebert commented Feb 22, 2019

Regarding the installation, I found the problem.

My default maven was not a recent one.

I did a small test here, I ran the openjdk:8-alpine machine and I was able to install all the dependencies.

Thank you

@mp911de
Copy link
Member

mp911de commented Feb 22, 2019

On a related note: Please do not attach images with code/log/debug output as we cannot search for text in images.

mp911de pushed a commit that referenced this issue Mar 10, 2019
Original pull request: #65.
mp911de added a commit that referenced this issue Mar 10, 2019
Rename RowDataConverter to R2dbcConverters. Introduce R2dbcSimpleTypeHolder. Apply custom conversions check in MappingR2dbcConverter. Extend tests.

Original pull request: #65.
@mp911de mp911de added this to the 1.0 M2 milestone Mar 10, 2019
@mp911de mp911de closed this as completed Mar 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: ideal-for-contribution An issue that a contributor can help us with type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants