我在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();
}
&#13;
PedidoDAO:
public interface PedidoDAO extends DAO <Pedido, Long> {
public Pedido pesquisarPorData(Date dataCadastro);
}
&#13;
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;
}
}
&#13;
ViewPedidos:
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();
}
}
}
&#13;
我已经测试过评论涉及日期字段的代码,只允许搜索ID并且运行得非常好,我已经尝试快速修复以搜索ID并注册日期做两个如上所述的不同方法,我尝试过做一个createQuery,没有任何事情继续下去,我想知道我在哪里做错了?或者使用两个或更多参数在JPA中进行搜索功能的任何提示? (Obs:我的代码是葡萄牙语,因为它是一个巴西项目,对不起或任何错误)并提前感谢!!
答案 0 :(得分:1)
问题是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
。
答案 1 :(得分:0)
引起:org.hibernate.TypeMismatchException:为类com.exemplo.model.Pedido提供了错误类型的id。预期:类java.lang.Long,得到类java.sql.Date
从异常报告的表面看。这是如此明显,异常告诉你,期望一个Long类型,但你把日期类型而不是Long。