How to add querydsl-mongodb to Spring Boot Gradle 5.6.1 project

Tags: , , , ,



I am trying to create dynamic query to a mongo database from spring boot gradle project. My gradle version: 5.6.1

Here is my build.gradle file:

plugins {
    id 'org.springframework.boot' version '2.2.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}

group = 'com.onssoftware'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {

    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'

    // https://mvnrepository.com/artifact/com.querydsl/querydsl-mongodb
    compile group: 'com.querydsl', name: 'querydsl-mongodb', version: '4.2.2'

    // https://mvnrepository.com/artifact/com.querydsl/querydsl-apt
    compile group: 'com.querydsl', name: 'querydsl-apt', version: '4.2.2'
    //annotationProcessor group: 'com.querydsl', name: 'querydsl-apt', version: '4.2.2'

    annotationProcessor "com.querydsl:querydsl-apt:4.2.2"
    //annotationProcessor("org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor")

    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}

My application.properties file:

spring.data.mongodb.username = user
spring.data.mongodb.password = pass
spring.data.mongodb.host = 172.17.0.2
spring.data.mongodb.port = 27017
spring.data.mongodb.database = test_db

Problem is: Q classes are not generating for my Documents. Any suggestion welcome. Thanks.

Answer

I made it working by adding both @Document and @Entity annotation.

import org.springframework.data.mongodb.core.mapping.Document;
import javax.persistence.Entity;

@Document
@Entity
public class MCQ {}

My build.gradle file is like:

// https://mvnrepository.com/artifact/org.hibernate/hibernate-annotations
compile group: 'org.hibernate', name: 'hibernate-annotations', version: '3.5.6-Final'
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
// https://mvnrepository.com/artifact/com.querydsl/querydsl-mongodb
compile group: 'com.querydsl', name: 'querydsl-mongodb', version: '4.2.2'
// https://mvnrepository.com/artifact/com.querydsl/querydsl-apt
annotationProcessor "com.querydsl:querydsl-apt:4.2.2:jpa", "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final"

// https://mvnrepository.com/artifact/com.google.code.morphia/morphia
//annotationProcessor group: 'com.google.code.morphia', name: 'morphia', version: '0.104'

// https://mvnrepository.com/artifact/org.mongodb.morphia/morphia
//annotationProcessor group: 'org.mongodb.morphia', name: 'morphia', version: '1.3.2'
//annotationProcessor("org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor")

My gradle version: 5.6.2



Source: stackoverflow