当我尝试构建项目时,我遇到了@Autowired和我的JpaReposytory类的问题,问题出现在这里。
这是我的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:找不到类型为Cliente的属性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));
好吧,现在我要跟踪这个错误,看看源代码是什么问题,如果有人有任何关于如何处理这个问题的想法,我将非常感激,现在非常感谢你。
答案 0 :(得分:1)
那里的事情错了......
1-异常告诉您spring不能找到cliente字段/属性,特别是:findByCliente(Cliente cliente)
表示您需要在类Cliente cliente
中指定的字段EntidadeDominio
JpaRepository
正在延长。
2-此外,由于JpaRepository
属于EntidadeDominio
,我希望findByCliente
返回类型应为List<EntidadeDominio>
而不是
我不确定你的目标是什么
答案 1 :(得分:1)
我认为在您的EntidadeDominio类中你需要一个字段类型的Cliente然后你可以使用findByCliente
class EntidadeDominio {
private Cliente cliente ;
// getter && setter
}
并且你的dao findByCliente应该撤回EntidadeDominio List<EntidadeDominio>
列表而不是cliente列表
public interface IJpaRepositoryLivrariaDAO extends
JpaRepository<EntidadeDominio, Long> {
List<EntidadeDominio> findByCliente(Cliente cliente);
}
答案 2 :(得分:1)
您IJpaRepositoryLivrariaDAO
正在管理EntidadeDominio
实体的持久性。
像这样更新您的存储库
public interface IJpaRepositoryLivrariaDAO extends JpaRepository<EntidadeDominio, Long> {
List<EntidadeDominio> findByCliente(Cliente cliente); // This will return a list of EntidadeDominio
}
以后你可以这样做
List<EntidadeDominio> entidadeDominios = repository.findByC...;
List<Cliente> clients = entidadeDominios.getCliente(); //Given that you have Cliente as composition in your EntidadeDominio entity.
答案 3 :(得分:0)
谢谢@Zeromus,我认为可以从我的pojo类生成,现在很明显这是不可能的,我使用反射来进行我的通用查询,方法仍然没有完成,它需要异常治疗。我不知道这是否是解决这个问题的最佳解决方案,但我在这里发帖,也许对某人有帮助。
public class ReflectionQueryCreator {
private String sql = "";
private String nomeAtributo;
private Object metodoValor;
public String queryBuilderCliente(Cliente object, Class<Cliente> classe) {
Method[] classMethods = classe.getMethods();
Field[] classFields = classe.getDeclaredFields();
for (Method method : classMethods) {
try {
if (method.getName().substring(0, 3).equals("get")) {
metodoValor = method.invoke(object);
for (Field field : classFields) {
nomeAtributo = field.getName();
if (method.getName().toLowerCase().endsWith(nomeAtributo.toLowerCase()))
if (!(metodoValor.equals("") || metodoValor.equals(0) || method.toString().contains("Id"))) {
if (method.getReturnType().getSimpleName().equals("String"))
sql = sql + object.getClass().getSimpleName().toLowerCase() + "." + nomeAtributo + " like %" + metodoValor + "% and ";
else if (method.getReturnType().getSimpleName().equals("long") && metodoValor.equals(0))
sql = sql + object.getClass().getSimpleName().toLowerCase() + "." + nomeAtributo + " = " + metodoValor + " and ";
} else
break;
}
}
} catch (IllegalAccessException ex) {ex.printStackTrace();}
catch (IllegalArgumentException ex) {ex.printStackTrace();}
catch (InvocationTargetException ex) {ex.printStackTrace();}
}
return sql;
}