Micronaut Unable to implement Repository method: Repository.update(Object entity). No possible implementations found

huangapple go评论118阅读模式
英文:

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:

&lt;dependency&gt;
			&lt;groupId&gt;io.micronaut.configuration&lt;/groupId&gt;
			&lt;artifactId&gt;micronaut-jdbc-hikari&lt;/artifactId&gt;
			&lt;scope&gt;compile&lt;/scope&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;io.micronaut.data&lt;/groupId&gt;
			&lt;artifactId&gt;micronaut-data-hibernate-jpa&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;io.micronaut.configuration&lt;/groupId&gt;
			&lt;artifactId&gt;micronaut-hibernate-jpa&lt;/artifactId&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;com.h2database&lt;/groupId&gt;
			&lt;artifactId&gt;h2&lt;/artifactId&gt;
			&lt;scope&gt;runtime&lt;/scope&gt;
		&lt;/dependency&gt;
&lt;path&gt;
	&lt;groupId&gt;io.micronaut.data&lt;/groupId&gt;
	&lt;artifactId&gt;micronaut-data-processor&lt;/artifactId&gt;
	&lt;version&gt;1.0.0.M1&lt;/version&gt;
&lt;/path&gt;

My Repository:

import io.micronaut.data.annotation.Repository;
import io.micronaut.data.repository.CrudRepository;

@Repository
public interface UsuarioRepository extends CrudRepository&lt;UsuarioModel, Long&gt; { }

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: &#39;&#39;
    schema-generate: CREATE_DROP
    dialect: H2
jpa:
  default:
    entity-scan:
        packages: &#39;com.cr.model&#39;

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 = &quot;Usuario&quot;)
@Table(name = &quot;usuario&quot;)
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&lt;MensagemModel&gt; mensagens;

    //Getter/Setters ommited

}

And also my MensagemModel class:

@Entity(name = &quot;Mensagem&quot;)
@Table(name = &quot;mensagem&quot;)
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 = &quot;usuario_id&quot;)
	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.

huangapple
  • 本文由 发表于 2020年3月15日 07:18:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/60688225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定