java:无法实现存储库方法:PersonRepository.save(对象实体)。找不到可能的实现
我正在创建一个小端点,但它对我不起作用。这非常简单,为此我使用 micronaut
我的 pom.xml
<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>
<groupId>com.autentia</groupId>
<artifactId>autentia</artifactId>
<version>0.1</version>
<packaging>${packaging}</packaging>
<parent>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-parent</artifactId>
<version>3.3.4</version>
</parent>
<properties>
<packaging>jar</packaging>
<jdk.version>11</jdk.version>
<release.version>11</release.version>
<micronaut.version>3.3.4</micronaut.version>
<exec.mainClass>com.autentia.Application</exec.mainClass>
<micronaut.runtime>netty</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</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-validation</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micronaut.test</groupId>
<artifactId>micronaut-test-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-client</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-server-netty</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-jackson-databind</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-runtime</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.data</groupId>
<artifactId>micronaut-data-hibernate-jpa</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.micronaut.build</groupId>
<artifactId>micronaut-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- Uncomment to enable incremental compilation -->
<!-- <useIncrementalCompilation>false</useIncrementalCompilation> -->
<annotationProcessorPaths combine.children="append">
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</path>
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-validation</artifactId>
<version>${micronaut.version}</version>
</path>
<path>
<groupId>io.micronaut.data</groupId>
<artifactId>micronaut-data-processor</artifactId>
<version>3.2.2</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-Amicronaut.processing.group=com.autentia</arg>
<arg>-Amicronaut.processing.module=autentia</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
我的存储库:
import com.prueba.entity.Person;
import io.micronaut.data.annotation.Repository;
import io.micronaut.data.repository.CrudRepository;
@Repository
public interface PersonRepository extends CrudRepository<Person, Long> {
}
我的实体:
import lombok.Getter;
import lombok.Setter;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
@Getter
@Setter
public class Person {
@Id
@GeneratedValue
private Long Id;
private String name;
private String surname;
}
最后,错误
java: 无法实现存储库方法:PersonRepository.findById(Object id)。无法在不存在的属性上查询实体 [Person]:Id
我可能做错了什么?谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
理想情况下,您应该下载或创建 micronaut 项目并选择 Lombok 依赖项,这样就不会遇到此问题。
但是如果您想稍后在项目中添加 lombok,这里是指南。
您需要做的就是以正确的顺序添加依赖项 。
注释处理器的顺序似乎在这里发挥着重要作用。
如下所述使用这些依赖项。
注意:这个答案基于 gradle,但这个概念也应该适用于 Maven
Step-1: 使用下面的 lombok 注释处理器作为依赖项中的第一件事:
Step-2: strong> 然后在注释处理器之后,使用 lombok 编译时依赖项:
第 3 步: 然后您可以编写其余的依赖项:
build.gradle(依赖项部分)
Ideally you should download or create micronaut project with Lombok dependency selected, so that you won't run into this issue.
But here is the guide if you want to add lombok later on in the project.
All you need to do is, add dependencies in correct order.
The order of annotations processors seems to play important role here.
Use these dependencies as mentioned below.
Note: This answer is based on gradle, but the concept should also work with Maven
Step-1: Use this below lombok annotation processor as the first thing in dependencies:
Step-2: Then after annotation processors, use lombok compile time dependency:
Step-3: Then you can write your rest of the dependencies:
build.gradle (dependencies section)
我不久前遇到了同样的问题,
在我创建了 get 方法而不是使用 lombok 的 @Getter 之后,它对我来说工作得很好并且每次都为我工作。
发生此冲突的原因是 JPA 使用 @Column 和 @Id< /em> 将实体中的字段映射到数据库中的列。当 Lombok 为这些字段生成 getter 时,它会干扰 JPA 映射并发生错误。
I faced same issue a while ago,
It worked fine for me, after I created get methods instead of using @Getter of lombok and worked every time for me.
This conflict occurs because JPA uses @Column and @Id to map the fields in entity to columns in the database. When Lombok generates getters for these fields, it interferes with the JPA mapping and occurs error.
本指南应帮助您创建第一个 Micronaut JDBC 应用程序。
https://guides.micronaut.io/latest/ micronaut-data-jdbc-repository-maven-java.html
如果您想使用 Lombok,则在使用 Micronaut CLI 或 Micronaut Launch 时将其添加为一项功能。
还有其他指南和文档应该对您的旅程有所帮助。
This guide should help you in creating your first Micronaut JDBC application.
https://guides.micronaut.io/latest/micronaut-data-jdbc-repository-maven-java.html
If you would like to use Lombok add it as a feature when you use either Micronaut CLI or Micronaut Launch.
There are other guides and documentation that should help you on your journey.