3

I have an issue with a very simple application I'm writing.

The persistence is handled with micronaut-data /w JPA, the DB is H2 for now.

I have a Comment model which looks like this:

package cloud.intepreting.models

import io.micronaut.data.annotation.Id
import io.micronaut.data.annotation.MappedEntity

@MappedEntity
class Comment {

    @Id
    Long id

    String projectId

    Integer topLevelCommentId

    Integer timeAdded


}

Then, I have a repository like the one below:

package cloud.intepreting.repositories

import cloud.intepreting.models.Comment
import io.micronaut.data.annotation.Repository
import io.micronaut.data.repository.CrudRepository

@Repository
interface CommentRepository extends CrudRepository<Comment, Long>{
}

Some simple test to run it:

package cloud.interpreting

import cloud.interpreting.models.Comment
import cloud.interpreting.repositories.CommentRepository
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Specification

@MicronautTest
class ModelsSpec extends Specification {

    @Inject
    CommentRepository commentRepository
    

    def "Can create entities in the database"() {
        given:
        Comment comment = new Comment()

        when:
        commentRepository.save(comment)

        then:
        commentRepository.findAll().size() == 0

    }
}

And what I get when I try to run it is:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project communicationlogsapp: Compilation failure
[ERROR] Unable to implement Repository method: CommentRepository.delete(Object entity). Delete all not supported for entities with no ID

Even if I simplify the Comment model to only consist of ID, no matter how I create it, if I explicitly add getters and setters - I always get this error.

Same with changing annotations to use the ones from Jakarta, @Entity and not @MappedEntity - nothing seems to help.

The only ref I found was this issue but it's not very helpful in my case.

Here's my application.yml:

micronaut:
  application:
    name: communicationlogsapp
  router:
    static-resources:
      frontend:
        paths: classpath:public
        mapping: /**
      swagger:
        paths: classpath:META-INF/swagger
        mapping: /swagger/**
      swagger-ui:
        paths: classpath:META-INF/swagger/views/swagger-ui
        mapping: /swagger-ui/**
datasources:
  default:
    url: jdbc:h2:mem:devDb
    driverClassName: org.h2.Driver
    username: sa
    password: ''
    dialect: H2
netty:
  default:
    allocator:
      max-order: 3
jpa:
  default:
    entity-scan:
      packages: 'cloud.interpreting.models'

and the pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>communicationlogsapp</artifactId>
    <version>0.1-SNAPSHOT</version>
    <packaging>${packaging}</packaging>

    <parent>
        <artifactId>micronaut-parent</artifactId>
        <groupId>io.micronaut</groupId>
        <version>3.4.2</version>
    </parent>

    <properties>
        <packaging>war</packaging>
        <jdk.version>1.8</jdk.version>
        <!--     If you are building with JDK 9 or higher, you can uncomment the lines below to set the release version -->
        <release.version>8</release.version>
        <micronaut.version>3.4.2</micronaut.version>
        <exec.mainClass>cloud.interpreting.commlogs.Application</exec.mainClass>
        <micronaut.data.version>3.3.0</micronaut.data.version>
        <groovyVersion>3.0.9</groovyVersion>
        <micronaut.runtime>tomcat</micronaut.runtime>
    </properties>

    <repositories>
        <repository>
            <id>central</id>
            <url>https://repo.maven.apache.org/maven2</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>micronaut-inject-groovy</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy</artifactId>
            <version>${groovyVersion}</version>
        </dependency>
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>micronaut-http-validation</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut.data</groupId>
            <artifactId>micronaut-data-hibernate-jpa</artifactId>
            <version>${micronaut.data.version}</version>
        </dependency>
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>micronaut-inject</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>micronaut-validation</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut.openapi</groupId>
            <artifactId>micronaut-openapi</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-core</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy-all</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.micronaut.test</groupId>
            <artifactId>micronaut-test-spock</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>micronaut-http-client</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut.servlet</groupId>
            <artifactId>micronaut-http-server-tomcat</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut.sql</groupId>
            <artifactId>micronaut-jdbc-tomcat</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut.groovy</groupId>
            <artifactId>micronaut-runtime-groovy</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-annotations</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut.data</groupId>
            <artifactId>micronaut-data-processor</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.17.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.17.2</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.17.2</version>
            <scope>runtime</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths combine.children="append">
                        <path>
                            <groupId>io.micronaut.data</groupId>
                            <artifactId>micronaut-data-processor</artifactId>
                            <version>3.4.2</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
            <plugin>
                <groupId>io.micronaut.build</groupId>
                <artifactId>micronaut-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>**/*Spec.*</include>
                        <include>**/*Test.*</include>
                    </includes>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>1.13.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>addSources</goal>
                            <goal>generateStubs</goal>
                            <goal>compile</goal>
                            <goal>removeStubs</goal>
                            <goal>addTestSources</goal>
                            <goal>generateTestStubs</goal>
                            <goal>compileTests</goal>
                            <goal>removeTestStubs</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <version>1.0.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>set-system-properties</goal>
                        </goals>
                        <configuration>
                            <properties>
                                <property>
                                    <name>groovy.target.directory</name>
                                    <value>${project.build.directory}/classes</value>
                                </property>
                                <property>
                                    <name>groovy.parameters</name>
                                    <value>true</value>
                                </property>
                                <property>
                                    <name>micronaut.openapi.views.spec</name>
                                    <value>rapidoc.enabled=true,swagger-ui.enabled=true,swagger-ui.theme=flattop</value>
                                </property>
                            </properties>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>copy props resources</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF
                            </outputDirectory>
                            <overwrite>true</overwrite>
                            <resources>
                                <resource>
                                    <directory>${basedir}/src/main/resources/WEB-INF</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <pluginRepositories>
    </pluginRepositories>
</project>

One more thing: If I change the Long id to something else, like Long comId I get: [ERROR] Unable to implement Repository method: CommentRepository.findById(Object id). Cannot query entity [Comment] on non-existent property: Id

Can you please take a look and tell me, what am I doing wrong here?

(Just a sidenote, using javax.peristance, adding @GeneratedValue doesn't help)

0 Answers0