【发布时间】:2018-03-22 13:43:26
【问题描述】:
当我尝试构建项目时,@Autowired 和我的 JpaRepository 类出现问题,问题出现在这里。
这是我的 Jpa Repository 类。
public interface IJpaRepositoryLivrariaDAO extends JpaRepository<EntidadeDominio, Long> {
List<Cliente> findByCliente(Cliente cliente); // whem I put this line of code I've get the exeption below
}
DAO 实现
@Repository
public class ClienteDAO implements IDAO {
@Autowired
List<Cliente> cliente;
@Autowired
IJpaRepositoryLivrariaDAO jpaRepositoryLivraria;
@Override
public void consultar(ModelMap modelMap) {
Cliente c = (Cliente) modelMap.get("object");
modelMap.addAttribute("object", jpaRepositoryLivraria
.findByCliente(c));
}
例外
2017-10-10 21:18:58.524 WARN 4000 --- [restartedMain] ationConfigEmbeddedWebApplicationContext:上下文初始化期间遇到异常 - 取消刷新尝试:
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“alterarCommand”的bean时出错:通过字段“fachada”表达的依赖关系不满足;
嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“fachada”的 bean 时出错:资源依赖注入失败;
嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为 'clienteDAO' 的 bean 时出错:通过字段 'jpaRepositoryLivraria' 表达的依赖关系不满足;
嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“IJpaRepositoryLivrariaDAO”的 bean 时出错:调用 init 方法失败;
嵌套异常是 org.springframework.data.mapping.PropertyReferenceException: No property cliente found for type Cliente!
如果我错了,请纠正我,问题是我的客户列表中没有客户或类似的东西
列出 findByCliente(Cliente cliente);
提前感谢您的帮助。
更新 1。
好的,根据答案,我做了一些更改,试图使代码正常工作。
首先,如果我在 EntidadeDominio 中放置一个 Cliente 属性,它会弄乱我的数据库,所以我创建了一个新的 JPARepository,试图让代码更易于理解和执行
public interface IJpaRepositoryClienteDAO extends JpaRepository<Cliente, Long> {
// I don't know the best way to use @Query, what I'm trying to do here
// is to select * from Cliente where any of the clients
// atributes matches with their corresponding columns
@Query("select c from Cliente c where c like %?1")
List<Cliente> findByCliente(Cliente cliente);
}
然后在
中调用这个方法public class ClienteDAO
@Autowired
IJpaRepositoryClienteDAO jpaRepositoryCliente;
public void consultar(ModelMap modelMap) {
Cliente c = (Cliente) modelMap.get("object");
modelMap.remove("object");
modelMap.remove("cliente");
modelMap.addAttribute("object", jpaRepositoryLivraria.findByCliente(c));
}
}
这是我的 Cliente 类和他的绑定类,以便更好地理解
@Entity
@Component
@DiscriminatorValue("Cliente")
public class Cliente extends EntidadeDominio {
@Embedded
private Genero genero;
private boolean ativo;
@OneToMany(mappedBy = "cliente", targetEntity = CartaoCredito.class, cascade = CascadeType.ALL, orphanRemoval = true)
private List<CartaoCredito> cartaoCredito = new ArrayList<>();
@OneToMany(mappedBy = "cliente", targetEntity = Endereco.class, cascade = CascadeType.ALL, orphanRemoval = true)
private List<Endereco> endereco = new ArrayList<>();
}
@Entity
@Component
public class CartaoCredito {
@Id
@GeneratedValue( strategy=GenerationType.AUTO )
private long id_cartao_credito;
private String numero;
private String nomeCartao;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_cliente")
private Cliente cliente;
}
@Entity
@Component
public class Endereco {
@Id
@GeneratedValue
private Long id_endereco;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_cliente")
private Cliente cliente;
}
这样做会改变代码的工作原理,现在的问题是我收到了这个错误
java.lang.IllegalArgumentException:参数值 [%com.les.livraria.dominio.cliente.Cliente@1a1147e] 与预期类型不匹配 [com.les.livraria.dominio.cliente.Cliente (n/a)]
对象在我的 ClienteDAO 类中被正确发送
modelMap.addAttribute("object", jpaRepositoryLivraria.findByCliente(c));
好吧,现在我要跟踪这个错误,看看是什么问题的根源,如果有人对如何处理这个问题有任何想法,我将非常感激,现在非常感谢。
【问题讨论】:
-
添加
EntidadeDominio的代码。
标签: hibernate spring-mvc spring-boot spring-data-jpa autowired