本文介绍了接收org.hibernate.TypeMismatchException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 我正在使用SpringMVC处理Spring应用程序,我遇到了以下错误,我不知道如何处理它。现在 我在控制台中有这个消息: org.hibernate.TypeMismatchException:提供了错误类型的ID。预期:类java.lang.Integer,类org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:86)$ b $在org.hibernate.impl类java.lang.String 。 SessionImpl.fireLoad(SessionImpl.java:878)在org.hibernate.impl.SessionImpl.load(SessionImpl.java:795)在org.hibernate.impl.SessionImpl.load(SessionImpl.java:在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)在sun.reflect.NativeMethodAccessorImpl.invoke(来源不明)在sun.reflect.DelegatingMethodAccessorImpl.invoke(来源不明788)) at java.lang.reflect.Method.invoke(Unknown Source) at org.hibernate.context.ThreadLocalSessionContext $ TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301) at com.sun。代理$ Proxy11.load(来源不明)在com.my.dao.DepartementImplDB.getDepartementByNom(DepartementImplDB.java:54)在com.my.dao.DepartementImplDB.addDepartement(DepartementImplDB.java: 29)在com.my.service.Departem entImplMetier.create(DepartementImplMetier.java:57)在com.my.controller.ImportController.Read(ImportController.java:279)在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source)在org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167)处使用org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710)在org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod)(AnnotationMethodHandlerAdapter.java:414)在org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter上.java:402)在org.springframework.web.servlet.DispatcherServlet.doDispatch(Dispatch erServlet.java:771)在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716 )在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java: at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563) at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)在javax.servlet.http.HttpServlet.service(HttpServlet.java:729)在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) 在org.apache.catalina .core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)在org.apache.catalina.core .ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)在org.apache.catalina.core.StandardWrapperValve.invoke (标准WrapperValve.java:219)在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106 )在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142 502))。在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)$ b在org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617) $ b在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)。在组织.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)在org.apache.coyote .AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:668) at org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1521) at org.apache.tomcat .util.net.NioEndpoint $ Socke tProcessor.run(NioEndpoint.java:1478)在java.util.concurrent.ThreadPoolExecutor.runWorker(未知源)在java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知源) at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)$ b $ at java.lang.Thread.run(Unknown Source) 这是我的ImportController类 / ** * @author Ilias * / package com.my.controller; //导入... 导入com.my.dao.Departement; @Controller public class ImportController { DepartementImplMetier dbD = new DepartementImplMetier(); $ b @RequestMapping(value =/ read) public String Read(Model model,@ RequestParam CommonsMultipartFile [] fileUpload) throws IOException,EncryptedDocumentException,InvalidFormatException ,java.text.ParseException { liste = extraire(modelnom); for(int m = 0,i = 29; i< liste.size(); i = i + 29){// i = i + 29 Employe employe = new Employe(); 部门部门=新部门(); if(i%29 == 0)m ++; // ...这里的一些代码 String dep =(String)liste.get(29 * m + 13).toString(); 部门d =新部门(); departement.setNomDepartement(dep); 布尔值bool = true; 列表< Departement> departements = dbD.getAll(); 布尔型depbool = true; for(int j = 0; j< departements.size(); j ++){ if(departements.get(j).getNomDepartement()== dep) { depbool = false; } if(depbool){ try { dbD.create(departement); } catch(Throwable e){ e.printStackTrace(); $ b for(int n = 0; n if(employes.get (n).getMatriculeMY()==(int)mat) { bool = false; if(bool){ try { dbD.create(departement); dbE.create(employe); } catch(Throwable e){ e.printStackTrace(); } } } } 返回重定向; }} } 这是我的Employe.hbm.xml p> <?xml version =1.0?> <!DOCTYPE hibernate-mapping PUBLIC - // Hibernate / Hibernate映射DTD 3.0 // ENhttp://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\"> ; <! - 由Hibernate Tools 3.4.0.CR1生成的5 ao?t 2015 11:05:44 - > < hibernate-mapping> < class name =com.my.dao.Employetable =EMPLOYE> < id name =idtype =int> < column name =ID/> < generator class =native/> < / id> < property name =nomEmployetype =java.lang.String> < column name =NOMEMPLOYE/> < / property> < property name =prenomEmployetype =java.lang.String> < column name =PRENOMEMPLOYE/> < / property> < property name =matriculeMYtype =int> < column name =MATRICULEMY/> < / property> < property name =adressetype =java.lang.String> < column name =ADRESSE/> < / property> < property name =sexetype =java.lang.String> < column name =SEXE/> < / property> < property name =cintype =java.lang.String> < column name =CIN/> < / property> < property name =dateNaissancetype =java.lang.String> < column name =DATENAISSANCE/> < / property> < property name =situationFamilialetype =java.lang.String> < column name =SITUATIONFAMILIALE/> < / property> < property name =dateEntreetype =java.lang.String> < column name =DATEENTREE/> < / property> < property name =dateSortietype =java.lang.String> < column name =DATESORTIE/> < / property> < property name =numCIMRtype =java.lang.String> < column name =NUMCIMR/> < / property> < property name =numCNSStype =java.lang.String> < column name =NUMCNSS/> < / property> < property name =numMUTtype =java.lang.String> < column name =NUMMUT/> < / property> < property name =profiletype =java.lang.String> < column name =PROFILE/> < / property> < property name =resteCongetype =java.lang.String> < column name =RESTECONGE/> < / property> < property name =banquetype =java.lang.String> < column name =BANQUE/> < / property> < property name =numCpteBanctype =java.lang.String> < column name =NUMCPTEBANC/> < / property> <! - < property name =fonctiontype =java.lang.String> < column name =FONCTION/> < / property> - > < property name =salairetype =float> < column name =SALAIRE/> < / property> < property name =indTransporttype =float> < column name =INDTRANSPORT/> < / property> < property name =indRepresenttype =float> < column name =INDREPRESENT/> < / property> < property name =indPaniertype =float> < column name =INDPANIER/> < / property> < column name =EDEPARTEMENT/> < /多对一> < column name =EFONCTION/> < /多对一> < column name =ESERVICE/> < /多对一> < column name =ETYPEPAIEMENT/> < /多对一> < column name =EMODEPAIEMENT/> < /多对一> < / class> < / hibernate-mapping> 这是我的Departement.hbm.xml <?xml version =1.0?> <!DOCTYPE hibernate-mapping PUBLIC - // Hibernate / Hibernate映射DTD 3.0 // ENhttp://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\"> ; <! - 由Hibernate Tools 3.4.0.CR1生成的5 ao?t 2015 11:05:44 - > < hibernate-mapping> < class name =com.my.dao.Departementtable =DEPARTEMENT> < id name =idtype =int> < column name =ID/> < generator class =native/> < / id> < property name =nomDepartementtype =java.lang.String> < column name =NOMDEPARTEMENT/> < / property> < set name =Employeinverse =true> < key column =IDnot-null =true/> < / set> < / class> < / hibernate-mapping> 调试完成后,问题出现在这里: 布尔型depbool = true; for(int j = 0; j< departements.size(); j ++){ if(departements.get(j).getNomDepartement()== dep) { depbool = false; } if(depbool){ try { dbD.create(departement); } catch(Throwable e){ e.printStackTrace(); } } } exactely here: dbD.create(departement); b $ b $ p package com.my.dao; import java.util.List; import org.hibernate.Session; import com.my.util.HibernateUtil; / ** * @author Ilias * * / public class DepartementImplDB implements DepartementDao { / * * * @see com.my.dao.DepartementDao#addDepartement(com.my.dao.Departement) * / @Override public int addDepartement(Departement D){ // TODO自动生成的方法存根 Session session = HibernateUtil.getInstance()。getSessionFactory()。getCurrentSession(); session.beginTransaction(); System.out.println(********); session.save(D); System.out.println(departement implement metier); session.getTransaction()。commit(); D = this.getDepartementByNom(D.getNomDepartement()); return D.getId(); } / ** * @ com.my.dao.DepartementDao#getDepartementById(int) * / @Override public Departement getDepartementById(int id){ // TODO自动生成的方法存根 Session session = HibernateUtil.getInstance()。getSessionFactory()。getCurrentSession(); session.beginTransaction(); Departement dept =(Departement)session.load(Departement.class,id); return dept; } / ** * @ com.my.dao.DepartementDao#getDepartementByNom(java.lang.String) * / @ 。倍率公共DEPARTEMENT getDepartementByNom(字符串nomDepartement){ // TODO自动生成方法存根会话会话= HibernateUtil.getInstance()提供了getSessionFactory()的getCurrentSession(); session.beginTransaction(); Departement dept =(Departement)session.load(Departement.class,nomDepartement); return dept; } / ** * @ com.my.dao.DepartementDao#deleteDepartement(int) * / @Override public void deleteDepartement(int id){ // TODO自动生成的方法存根 Session session = HibernateUtil.getInstance()。getSessionFactory()。getCurrentSession(); session.beginTransaction(); Departement dept =(Departement)session.load(Departement.class,id); session.delete(dept); session.getTransaction()。commit(); } / ** * @ com.my.dao.DepartementDao#updateDepartement(com.my.dao.Departement) * / @Override public void updateDepartement(Departement D){ // TODO自动生成方法存根} / ** * com.my.dao.DepartementDao#getAllDepartement() * / @Override public List< Departement> getAllDepartement(){ // TODO自动生成的方法存根 Session session = HibernateUtil.getInstance()。getSessionFactory()。getCurrentSession(); session.beginTransaction(); return session.createQuery(from Departement)。list(); $ b $ p $ b 这是我最后一次错误的链接, :链接 有哪些帮助? 解决方案 b org.hibernate.TypeMismatchException:提供了错误类型的ID。预期:class java.lang.Integer,得到类java.lang.String 在你的stacktrace中,我们可以请参阅dbd.create调用其他方法 at com.my.dao.DepartementImplDB.getDepartementByNom(DepartementImplDB.java:54) at com.my.dao.DepartementImplDB.addDepartement(DepartementImplDB.java:29) 在你的方法 public Departement getDepartementByNom(String nomDepartement) 您尝试使用名称加载部门,但您使用的是由Id加载的session.load。 Departement dept =(Departement)session.load(Departement.class,nomDepartement); 您必须按照名称加载标准请求。像 session.beginTransaction(); Criteria criteria = session.createCriteria(Departement.class); criteria.add(Restrictions.eq(nomDepartement,nomDepartement).ignoreCase()); result =(Departement)criteria.uniqueResult(); session.getTransaction()。commit(); ps:在getDepartementById和getDepartementByNom中,您永远不会结束事务。请使用以下模式: try { session.beginTransaction(); //你的代码 session.getTransaction()。commit(); } catch(HibernateException e){ LOGGER.error(e); if(session.getTransaction()。isActive()){ session.getTransaction()。rollback(); } } I'm working on an application with Spring using SpringMVC, i'm encountering the following error and i don't know how to deal with it .now i have this message in Console :org.hibernate.TypeMismatchException: Provided id of the wrong type. Expected: class java.lang.Integer, got class java.lang.Stringat org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:86)at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)at org.hibernate.impl.SessionImpl.load(SessionImpl.java:795)at org.hibernate.impl.SessionImpl.load(SessionImpl.java:788)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at java.lang.reflect.Method.invoke(Unknown Source)at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)at com.sun.proxy.$Proxy11.load(Unknown Source)at com.my.dao.DepartementImplDB.getDepartementByNom(DepartementImplDB.java:54)at com.my.dao.DepartementImplDB.addDepartement(DepartementImplDB.java:29)at com.my.service.DepartementImplMetier.create(DepartementImplMetier.java:57)at com.my.controller.ImportController.Read(ImportController.java:279)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at java.lang.reflect.Method.invoke(Unknown Source)at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710)at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167)at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563)at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)at java.lang.Thread.run(Unknown Source)this is my ImportController class/** * @author Ilias */package com.my.controller;//import ...import com.my.dao.Departement;@Controllerpublic class ImportController {DepartementImplMetier dbD = new DepartementImplMetier();@RequestMapping(value="/read")public String Read(Model model,@RequestParam CommonsMultipartFile[] fileUpload) throws IOException, EncryptedDocumentException, InvalidFormatException, java.text.ParseException{ liste = extraire(modelnom);for(int m=0, i=29;i<liste.size();i=i+29){//i=i+29 Employe employe= new Employe(); Departement departement = new Departement(); if(i % 29 == 0) m++; //... Some code here String dep = (String)liste.get(29*m+13).toString(); Departement d = new Departement(); departement.setNomDepartement(dep); boolean bool=true; List<Departement> departements = dbD.getAll(); boolean depbool = true; for(int j=0;j< departements.size();j++){ if(departements.get(j).getNomDepartement() == dep ) { depbool = false; } if(depbool){ try { dbD.create(departement); } catch (Throwable e) { e.printStackTrace(); } } } for(int n=0;n<employes.size();n++){ if(employes.get(n).getMatriculeMY() == (int)mat ) { bool= false; } } if(bool){ try { dbD.create(departement); dbE.create(employe); } catch (Throwable e) { e.printStackTrace(); } } }}return "redirect";}}}this is my Employe.hbm.xml<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Generated 5 ao?t 2015 11:05:44 by Hibernate Tools 3.4.0.CR1 --><hibernate-mapping> <class name="com.my.dao.Employe" table="EMPLOYE"> <id name="id" type="int"> <column name="ID" /> <generator class="native" /> </id> <property name="nomEmploye" type="java.lang.String"> <column name="NOMEMPLOYE" /> </property> <property name="prenomEmploye" type="java.lang.String"> <column name="PRENOMEMPLOYE" /> </property> <property name="matriculeMY" type="int"> <column name="MATRICULEMY" /> </property> <property name="adresse" type="java.lang.String"> <column name="ADRESSE" /> </property> <property name="sexe" type="java.lang.String"> <column name="SEXE" /> </property> <property name="cin" type="java.lang.String"> <column name="CIN" /> </property> <property name="dateNaissance" type="java.lang.String"> <column name="DATENAISSANCE" /> </property> <property name="situationFamiliale" type="java.lang.String"> <column name="SITUATIONFAMILIALE" /> </property> <property name="dateEntree" type="java.lang.String"> <column name="DATEENTREE" /> </property> <property name="dateSortie" type="java.lang.String"> <column name="DATESORTIE" /> </property> <property name="numCIMR" type="java.lang.String"> <column name="NUMCIMR" /> </property> <property name="numCNSS" type="java.lang.String"> <column name="NUMCNSS" /> </property> <property name="numMUT" type="java.lang.String"> <column name="NUMMUT" /> </property> <property name="profile" type="java.lang.String"> <column name="PROFILE" /> </property> <property name="resteConge" type="java.lang.String"> <column name="RESTECONGE" /> </property> <property name="banque" type="java.lang.String"> <column name="BANQUE" /> </property> <property name="numCpteBanc" type="java.lang.String"> <column name="NUMCPTEBANC" /> </property> <!-- <property name="fonction" type="java.lang.String"> <column name="FONCTION" /> </property> --> <property name="salaire" type="float"> <column name="SALAIRE" /> </property> <property name="indTransport" type="float"> <column name="INDTRANSPORT" /> </property> <property name="indRepresent" type="float"> <column name="INDREPRESENT" /> </property> <property name="indPanier" type="float"> <column name="INDPANIER" /> </property> <many-to-one name="eDepartement" class="com.my.dao.Departement" access="field" fetch="join"> <column name="EDEPARTEMENT" /> </many-to-one> <many-to-one name="eFonction" class="com.my.dao.Fonction" access="field" fetch="join"> <column name="EFONCTION" /> </many-to-one> <many-to-one name="eService" class="com.my.dao.Service" access="field" fetch="join"> <column name="ESERVICE" /> </many-to-one> <many-to-one name="eTypePaiement" class="com.my.dao.TypePaiement" access="field" fetch="join"> <column name="ETYPEPAIEMENT" /> </many-to-one> <many-to-one name="eModePaiement" class="com.my.dao.ModePaiement" access="field" fetch="join"> <column name="EMODEPAIEMENT" /> </many-to-one> </class></hibernate-mapping>this is my Departement.hbm.xml<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Generated 5 ao?t 2015 11:05:44 by Hibernate Tools 3.4.0.CR1 --><hibernate-mapping> <class name="com.my.dao.Departement" table="DEPARTEMENT"> <id name="id" type="int"> <column name="ID" /> <generator class="native" /> </id> <property name="nomDepartement" type="java.lang.String"> <column name="NOMDEPARTEMENT" /> </property> <set name="Employe" inverse="true"> <key column="ID" not-null="true"/> <one-to-many class="com.my.dao.Employe" /> </set> </class></hibernate-mapping>after debuging, the problem is here :boolean depbool = true; for(int j=0;j< departements.size();j++){ if(departements.get(j).getNomDepartement() == dep ) { depbool = false; } if(depbool){ try { dbD.create(departement); } catch (Throwable e) { e.printStackTrace(); } } }exactely here :dbD.create(departement);this DepartementImplDB classpackage com.my.dao;import java.util.List;import org.hibernate.Session;import com.my.util.HibernateUtil;/** * @author Ilias * */public class DepartementImplDB implements DepartementDao { /** * @see com.my.dao.DepartementDao#addDepartement(com.my.dao.Departement) */ @Override public int addDepartement(Departement D) { // TODO Auto-generated method stub Session session = HibernateUtil.getInstance().getSessionFactory().getCurrentSession(); session.beginTransaction();System.out.println("********"); session.save(D);System.out.println("departement implement metier"); session.getTransaction().commit(); D=this.getDepartementByNom(D.getNomDepartement()); return D.getId(); } /** * @see com.my.dao.DepartementDao#getDepartementById(int) */ @Override public Departement getDepartementById(int id) { // TODO Auto-generated method stub Session session = HibernateUtil.getInstance().getSessionFactory().getCurrentSession(); session.beginTransaction(); Departement dept = (Departement) session.load(Departement.class, id); return dept; } /** * @see com.my.dao.DepartementDao#getDepartementByNom(java.lang.String) */ @Override public Departement getDepartementByNom(String nomDepartement) { // TODO Auto-generated method stub Session session = HibernateUtil.getInstance().getSessionFactory().getCurrentSession(); session.beginTransaction(); Departement dept = (Departement) session.load(Departement.class, nomDepartement); return dept; } /** * @see com.my.dao.DepartementDao#deleteDepartement(int) */ @Override public void deleteDepartement(int id) { // TODO Auto-generated method stub Session session = HibernateUtil.getInstance().getSessionFactory().getCurrentSession(); session.beginTransaction(); Departement dept = (Departement) session.load(Departement.class, id); session.delete(dept); session.getTransaction().commit(); } /** * @see com.my.dao.DepartementDao#updateDepartement(com.my.dao.Departement) */ @Override public void updateDepartement(Departement D) { // TODO Auto-generated method stub } /** * @see com.my.dao.DepartementDao#getAllDepartement() */ @Override public List<Departement> getAllDepartement() { // TODO Auto-generated method stub Session session = HibernateUtil.getInstance().getSessionFactory().getCurrentSession(); session.beginTransaction(); return session.createQuery("from Departement").list(); }}this is the link of my last error for more infos : Linkcan some one help ? 解决方案 Your error is org.hibernate.TypeMismatchException: Provided id of the wrong type. Expected: class java.lang.Integer, got class java.lang.StringIn your stacktrace we can see that dbd.create call others methods at com.my.dao.DepartementImplDB.getDepartementByNom(DepartementImplDB.java:54)at com.my.dao.DepartementImplDB.addDepartement(DepartementImplDB.java:29)In your methodpublic Departement getDepartementByNom(String nomDepartement)You try to load a departement with a name but you use session.load wich load by Id. Departement dept = (Departement) session.load(Departement.class, nomDepartement);You have to make a Criteria request to load by name. Likesession.beginTransaction();Criteria criteria = session.createCriteria(Departement.class);criteria.add(Restrictions.eq("nomDepartement", nomDepartement).ignoreCase()); result = (Departement) criteria.uniqueResult(); session.getTransaction().commit();ps: In getDepartementById and getDepartementByNom you never end your transaction. Please use pattern like: try { session.beginTransaction(); // your code session.getTransaction().commit(); } catch (HibernateException e) { LOGGER.error(e); if (session.getTransaction().isActive()) { session.getTransaction().rollback(); } } 这篇关于接收org.hibernate.TypeMismatchException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-08 07:56