7

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.

Md. Najmul Hasan
  • 605
  • 1
  • 6
  • 19

2 Answers2

0

Possibly, duplicated with Java QueryDsl code generation does not generate Q class

And maybe it's because of Gradle version: try to check this https://github.com/ewerk/gradle-plugins/issues/112

Or you forget to include

compile group: 'org.mongodb.morphia', name: 'morphia', version: '1.3.2'

Meanwhile, I'll try to reproduce using @viktorgt repository

Fahim Bagar
  • 798
  • 7
  • 17
  • As in https://github.com/querydsl/querydsl/issues/2617, the query-dsl gradle plugin is deprecated and unnecessary for gradle > 5. The morphia dependency is also not required, since spring annotations are used and the spring annotation processor should be picked up (org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor). Still, something is missing. – viktorgt Jun 28 '20 at 17:37
-1

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

Md. Najmul Hasan
  • 605
  • 1
  • 6
  • 19