gpt4 book ai didi

java - 在 JPA 中使用两个或多个参数的搜索查询

转载 作者:行者123 更新时间:2023-12-02 13:29:18 26 4
gpt4 key购买 nike

我对 Hibernate/JPA 世界还是个新手,我被困在这里试图以与按 id 搜索相同的方式创建一个函数,但我正在尝试使用两个参数从数据库中搜索并返回字段,我也用基本的 CRUD 操作加上这个特殊的操作对我的项目做了一种 DAO 方法。在进行搜索时,它给了我这个返回堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Provided id of the wrong type for class com.exemplo.model.Pedido. Expected: class java.lang.Long, got class java.sql.Date
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:1135)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:1068)
at com.exemplo.repositorio.MySQLPedidoDAO.pesquisarPorData(MySQLPedidoDAO.java:82)
at com.exemplo.view.ViewPedidos.btBuscarActionPerformed(ViewPedidos.java:322)
at com.exemplo.view.ViewPedidos.access$000(ViewPedidos.java:23)
at com.exemplo.view.ViewPedidos$1.actionPerformed(ViewPedidos.java:106)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: org.hibernate.TypeMismatchException: Provided id of the wrong type for class com.exemplo.model.Pedido. Expected: class java.lang.Long, got class java.sql.Date
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:133)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1066)
at org.hibernate.internal.SessionImpl.access$2000(SessionImpl.java:176)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2540)
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:951)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:1110)
... 41 more

我的方法是这样的,DAO类:

public interface DAO <T, K> {

public void inserir(T o);

public void alterar(T o);

public void excluir(T o);

public T pesquisar(K id);

public List<T> listar();

}

PedidoDAO:

public interface PedidoDAO extends DAO <Pedido, Long> {

public Pedido pesquisarPorData(Date dataCadastro);

}

MySQLPedidoDAO:(PesquisarPorData 函数出错)

public class MySQLPedidoDAO implements PedidoDAO {

public ConnectionFactory cf = null;

public MySQLPedidoDAO(ConnectionFactory cf) {
this.cf = cf;
}

@Override
public Pedido pesquisar(Long id) {
cf.createEm().getTransaction().begin();
Pedido pedido = cf.createEm().find(Pedido.class, id);
cf.createEm().getTransaction().commit();
//erro ao deletar pois o factory já está fechado
//emf.close();
return pedido;
}

@Override
public Pedido pesquisarPorData(Date dataCadastro) {
cf.createEm().getTransaction().begin();
// ERROR AT THE LINE UNDER!!!!!
Pedido pedido = cf.createEm().find(Pedido.class, dataCadastro);
cf.createEm().getTransaction().commit();
//erro ao deletar pois o factory já está fechado
//emf.close();
return pedido;
}

}

查看Pedidos:

public class ViewPedidos extends javax.swing.JInternalFrame {

private MySQLPedidoDAO mspdao;
private Pedido ped;
private boolean editavel;

/* Getters e Setters from editavel and ped */

private void btPesquisaActionPerformed(java.awt.event.ActionEvent evt) {
habilitarApenasOSeData();
limparCampos();
tfPedido.setEditable(true);
tfPedido.requestFocus();
btBuscar.setEnabled(true);
}



private void btBuscarActionPerformed(java.awt.event.ActionEvent evt) {
ConnectionFactory cf = new ConnectionFactory();
MySQLPedidoDAO mspdao = new MySQLPedidoDAO(cf);
Pedido ped = new Pedido();

if(tfPedido.getText().length() == 0 && tfDataCad.getText().length() == 0){
JOptionPane.showMessageDialog(rootPane, "Search not succeed, please fill the fields above", "Search", JOptionPane.INFORMATION_MESSAGE);
} else {
ped = mspdao.pesquisar(Long.parseLong(tfPedido.getText()));
ped = mspdao.pesquisarPorData(new java.sql.Date(((java.util.Date)tfDataCad.getValue()).getTime()));
setPed(ped);
setEditable(true);
carregarCampos();
habilitarTextFieldEBotoes();
}

}


}

我已经测试过注释涉及日期字段的代码,只允许搜索 id,并且运行得很好,我尝试做一个快速修复来搜索 id 并注册日期,执行上述两种不同的方法,我尝试执行 createQuery 但没有任何反应,我想知道我哪里做错了?或使用两个或更多参数在 JPA 中创建搜索功能的任何提示? (观察:我的代码是葡萄牙语,因为它是一个巴西项目,对于它或任何错误感到抱歉)并提前致谢!!

最佳答案

问题在于 EntityManager#find 要求您提供实体的主键(Long)。如果您想使用其他字段进行查询,则需要使用createQuery

您尚未在问题中提供实体定义。但是查询将类似于:

TypedQuery<Pedido> query = cf.createEm().createQuery("id = :id and registerDate = :date", Pedido.class);
query.setParameter("date", dataCadastro);
query.setParameter("id", id);
List<Pedido> results = query.getResultList();

如果您只需要一个结果,您可以调用 query.getSingleResult() 而不是 getResultList()。如果没有单个结果,这将抛出 NonUniqueResultException

关于java - 在 JPA 中使用两个或多个参数的搜索查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43244753/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com