Spring Webflux + JPA: Reactive Repositories are not supported by JPA
I don't know about previous support, but as of 09 June 2019, you can absolutely use WebFlux with JPA Repositories!
Your stack doesn't have to be fully reactive. I like WebFlux, but need a relational database.
I have:
- spring-boot-starter-data-redis-reactive
- spring-boot-starter-webflux
- spring-boot-starter-data-jpa
edit: (FYI) code is in Kotlin, but should still work in Java.
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = ["com.example.core.repositories"])
@EnableJpaAuditing
class PersistenceConfig
src/core/models/User
@Entity
@Table(name = "user")
class User(
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id")
var id: Long,
@Column(name = "username")
var username: String,
@Column(name = "created_date", nullable = false, updatable = false)
@CreatedDate
@Temporal(TemporalType.TIMESTAMP)
val createdDate: Date,
@Column(name = "modified_date")
@LastModifiedDate
@Temporal(TemporalType.TIMESTAMP)
val modifiedDate: Date
) : Serializable {
/**
* This constructor is not to be used. This is for hibernate,
* which requires an empty constructor.
*/
constructor() : this(1L, "", "", Date(), Date())
companion object {
private const val serialVersionUID = 2398467923L
}
}
I got the same JPA: Reactive Repositories are not supported by JPA.
error when I was still returning mono objects from the Spring Data query like Mono<User>
. However, if you remove the Mono
wrapper, it should work just fine.
src/core/repositories/UserRepository
@Repository
interface UserRepository: CrudRepository<User, Long> {
fun findUserByUsername(username: String): User?
}
If you want all the benefits of reactive, async / non-blocking, you'll need to make the whole stack async / non-blocking. JDBC is indeed inherently a blocking API, so you can't build a fully reactive / non-blocking app if you need to access the database through JDBC.
But you still you need relational database then will recommend to use rxjava2-jdbc
and here is full example of using RxJava and RxJava jdbc spring-webflux-async-jdbc-sample
Seems currently Spring webflux support Mongodb, Redis, etc nosql reactive so instead of JPA use spring-boot-starter-data-mongodb-reactive
.