jhipster / generator-jhipster

JHipster is a development platform to quickly generate, develop, & deploy modern web applications & microservice architectures.
https://www.jhipster.tech
Apache License 2.0
21.54k stars 4.02k forks source link

How to Convert Business Logic JpaRepository to ReactiveMongoRepository #22764

Closed camenduru closed 1 year ago

camenduru commented 1 year ago

Hi I am new 👋 leaning jhipster and spring

thanks for the example https://github.com/mraible/jhipster6-demo#add-business-logic

How can I convert JpaRepository Query to ReactiveMongoRepository Query

public interface BlogRepository extends JpaRepository<Blog, Long> {
    @Query("select blog from Blog blog where blog.user.login = ?#{principal.username}")
    List<Blog> findByUserIsCurrentUser();
}

I tried this but not worked

public interface JobRepository extends ReactiveMongoRepository<Job, String> {
    @Query("{'user.login': ?0}")
    Flux<Job> findByUserIsCurrentUser();
}

Job class

public class Job implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private String id;

    @NotNull(message = "must not be null")
    @Field("prompt")
    private String prompt;

    @NotNull(message = "must not be null")
    @Field("status")
    private JobStatus status;

    @Field("user")
    private User user;
...

I want just list SecurityUtils.getCurrentUserLogin() jobs

    @GetMapping(value = "/jobs", produces = MediaType.APPLICATION_JSON_VALUE)
    public Mono<List<Job>> getAllJobs(@RequestParam(required = false, defaultValue = "false") boolean eagerload) {
        log.debug("REST request to get all Jobs");
        if (eagerload) {
            // return jobRepository.findByUserIsCurrentUser().collectList(); ???
            return jobRepository.findAllWithEagerRelationships().collectList();
        } else {
            return jobRepository.findAll().collectList();
        }
    }
mraible commented 1 year ago

The easiest thing to do is probably to re-create the demo project using MongoDB instead of SQL.

camenduru commented 1 year ago

Hi @mraible 👋 yep I re-created the project using MongoDB instead of SQL and added Job entity

enum JobStatus {
    WAITING, WORKING, DONE, FAILED
}

entity Job {
    prompt String required
    status JobStatus required
}

relationship ManyToOne {
    Job{user(login)} to User with builtInEntity
}
mraible commented 1 year ago

OK, in that case, hopefully, someone with more MongoDB can answer your question on how to convert the repository.