英文:
Micronaut Unable to implement Repository method: Repository.update(Object entity). No possible implementations found
问题
以下是翻译好的内容:
这是我的技术栈:
- Micronaut 1.3.2
- Java 8
- Eclipse STS 4
Pom.xml摘录:
<dependency>
<groupId>io.micronaut.configuration</groupId>
<artifactId>micronaut-jdbc-hikari</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.data</groupId>
<artifactId>micronaut-data-hibernate-jpa</artifactId>
</dependency>
<dependency>
<groupId>io.micronaut.configuration</groupId>
<artifactId>micronaut-hibernate-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<path>
<groupId>io.micronaut.data</groupId>
<artifactId>micronaut-data-processor</artifactId>
<version>1.0.0.M1</version>
</path>
我的Repository:
import io.micronaut.data.annotation.Repository;
import io.micronaut.data.repository.CrudRepository;
@Repository
public interface UsuarioRepository extends CrudRepository<UsuarioModel, Long> { }
我的Service:
private UsuarioDTO atualizar(UsuarioDTO usuario) {
UsuarioModel modelo = new UsuarioModel();
modelo.setIdade(usuario.idade);
modelo.setNick(usuario.nick);
modelo.setNome(usuario.nome);
modelo.setPassword(usuario.password);
this.usuarioRepository.update(modelo);
return usuario;
}
application.yml:
micronaut:
application:
name: micronaut-chatroom
server:
cors:
enabled: true
datasources:
default:
url: jdbc:h2:mem:devDb
driverClassName: org.h2.Driver
username: sa
password: ''
schema-generate: CREATE_DROP
dialect: H2
jpa:
default:
entity-scan:
packages: 'com.cr.model'
当我在我的Micronaut项目上运行安装Maven命令时,我遇到了错误:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project micronaut-chatroom: Compilation failure
[ERROR] Unable to implement Repository method: UsuarioRepository.update(Object entity). No possible implementations found.
有人经历过类似的情况吗?
谢谢您提前的帮助!
编辑:
这是我的UsuarioModel类
@Entity(name = "Usuario")
@Table(name = "usuario")
public class UsuarioModel implements Serializable {
private static final long serialVersionUID = -7468837154917949190L;
@Id
@GeneratedValue
private Long id;
private String nome;
private String nick;
private String idade;
private String password;
@OneToMany
private List<MensagemModel> mensagens;
//Getter/Setters ommited
}
还有我的MensagemModel类:
@Entity(name = "Mensagem")
@Table(name = "mensagem")
public class MensagemModel implements Serializable {
private static final long serialVersionUID = 7634793331670289187L;
@Id
@GeneratedValue
private Long id;
@CreationTimestamp
private LocalDateTime data;
private String conteudo;
@ManyToOne
@JoinColumn(name = "usuario_id")
private UsuarioModel usuario;
//Getter/Setter ommited
}
英文:
Here's my stack:
- Micronaut 1.3.2
- Java 8
- Eclipse STS 4
Pom.xml excerpt:
<dependency>
<groupId>io.micronaut.configuration</groupId>
<artifactId>micronaut-jdbc-hikari</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.data</groupId>
<artifactId>micronaut-data-hibernate-jpa</artifactId>
</dependency>
<dependency>
<groupId>io.micronaut.configuration</groupId>
<artifactId>micronaut-hibernate-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<path>
<groupId>io.micronaut.data</groupId>
<artifactId>micronaut-data-processor</artifactId>
<version>1.0.0.M1</version>
</path>
My Repository:
import io.micronaut.data.annotation.Repository;
import io.micronaut.data.repository.CrudRepository;
@Repository
public interface UsuarioRepository extends CrudRepository<UsuarioModel, Long> { }
My Service:
private UsuarioDTO atualizar(UsuarioDTO usuario) {
UsuarioModel modelo = new UsuarioModel();
modelo.setIdade(usuario.idade);
modelo.setNick(usuario.nick);
modelo.setNome(usuario.nome);
modelo.setPassword(usuario.password);
this.usuarioRepository.update(modelo);
return usuario;
}
application.yml:
micronaut:
application:
name: micronaut-chatroom
server:
cors:
enabled: true
datasources:
default:
url: jdbc:h2:mem:devDb
driverClassName: org.h2.Driver
username: sa
password: ''
schema-generate: CREATE_DROP
dialect: H2
jpa:
default:
entity-scan:
packages: 'com.cr.model'
When I'm running Install maven command on my Micronaut project I get the error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project micronaut-chatroom: Compilation failure
[ERROR] Unable to implement Repository method: UsuarioRepository.update(Object entity). No possible implementations found.
Has someone been through it?
Thanks in advance!
EDIT
Here's my UsuarioModel class
@Entity(name = "Usuario")
@Table(name = "usuario")
public class UsuarioModel implements Serializable {
/**
*
*/
private static final long serialVersionUID = -7468837154917949190L;
@Id
@GeneratedValue
private Long id;
private String nome;
private String nick;
private String idade;
private String password;
@OneToMany
private List<MensagemModel> mensagens;
//Getter/Setters ommited
}
And also my MensagemModel class:
@Entity(name = "Mensagem")
@Table(name = "mensagem")
public class MensagemModel implements Serializable {
/**
*
*/
private static final long serialVersionUID = 7634793331670289187L;
@Id
@GeneratedValue
private Long id;
@CreationTimestamp
private LocalDateTime data;
private String conteudo;
@ManyToOne
@JoinColumn(name = "usuario_id")
private UsuarioModel usuario;
//Getter/Setter ommited
}
答案1
得分: 1
编辑:
在 Micronaut 中,@Id
装饰器有两个可能的来源:
- "native" 注解包:
io.micronaut.data.annotation
- 作为 Maven 依赖项中的
jakarta.persistence-api
而提供的 jpa 持久化包
由于您正在使用 @Entity
装饰类,我想您可能正在使用后者一组装饰器,或者至少是两个来源的混合。
作为基本的调试步骤,您可以尝试删除与 javax.persistence
相关的任何引用,而仅使用本地的装饰器。例如,不使用 @Entity
,而改为使用 @MappedEntity
等。
只需注释掉从 javax.persistence.*
导入的任何内容,然后尝试使用本地的 io.micronaut.data.annotation*
。
希望这样可以更清楚地指出问题是否出现在注解处理的某个地方。
旧的废话已被删除 - 与 Kotlin 相关
英文:
EDIT:
There are 2 possible sources for the @Id
decorator in micronaut:
- the "native" annotation package:
io.micronaut.data.annotation
- the jpa persistance package available as:
jakarta.persistence-api
dependency in maven
As you are decorating the class with @Entity
I suppose you are using the latter set of decorators. or at least a mix of the 2 sources.
As basic debug step you could try removing any reference to javax.persistance and use native only decorators. So, as instance, do not use @Entity
but rather @MappedEntity
and so on.
simply comment out any import from javax.persistence.*
and try with native io.micronaut.data.annotation*
.
this would make clearer - I hope- if the issue lies in some twist of the annotation process.
OLD RUBBISH HAS BEEN DELETED - was Kotlin related
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论