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

Support Kotlin's Flow [DATACMNS-1608] #2005

Closed
spring-projects-issues opened this issue Nov 4, 2019 · 4 comments
Closed

Support Kotlin's Flow [DATACMNS-1608] #2005

spring-projects-issues opened this issue Nov 4, 2019 · 4 comments
Assignees
Labels
status: duplicate type: enhancement

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Nov 4, 2019

smallufo opened DATACMNS-1608 and commented

Hi , I hope Spring Data JPA support Kotlin's Flow like this :

 interface UserDao {
   @Query("select u from User u where ....")
   @Hints("batch=:batch") // something like that
   fun findUsers(@Param(name="batch") batchSize:Int): Flow<User>
}

There should be some hint in the @Query annotation


Issue Links:

  • DATACMNS-1508 Add support for Coroutines repositories
    ("duplicates")
@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Nov 5, 2019

Jens Schauder commented

What is your question/ proposal here?

Do you want support for Kotlin sFlow?
Support for specifying the batch size when reading into streamable result types?

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Nov 5, 2019

smallufo commented

Prior to JPA 2.2 , the only way to emit Kotlin's Flow is like this :

 override fun findSomeUsers(batch: Int): Flow<User> {
    return flow {
      (em.delegate as Session).sessionFactory.openSession().use { session ->
        val query = session.createQuery("select u from User u where ...")
        query.fetchSize = batch
        query.isReadOnly = true

        query.scroll(ScrollMode.FORWARD_ONLY).use { results ->
          while (results.next()) {
            val u = results.get(0) as User
            emit(u)
          }
        }
      }
    }
  } 

It has some drawbacks : 

It downcasts entityManager to Hibernate's session , and that's a little boilerplate , which should be able to code generated. 

 

Since JPA 2.2 supports streaming result , I hope Spring-Data-JPA also support Kotlin's flow as returning type (with some batchSize hint annotations).

 

Related SO link : https://stackoverflow.com/questions/58700235/spring-data-with-jpa-2-2-resultstream-to-kotlins-flow

 

 

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Nov 7, 2019

Mark Paluch commented

Kotlin's Flow is a Coroutine type. Allowing that sort of type lets users expect Coroutine-like behavior which is clearly not available from JPA as all I/O is blocking without a true push-data structure experience.

Please instead use Stream or Spring Data R2DBC which can return Flow as part of its Coroutines support

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Nov 8, 2019

Mark Paluch commented

Closing this ticket as per previous comment above. We have a general ticket handling Kotlin Coroutine repositories that is linked now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate type: enhancement
Projects
None yet
Development

No branches or pull requests

2 participants