1
晚安,休眠與數據庫通訊
我在Eclipse中使用hibernate與數據庫進行通訊。
我創建了以下方法門面:
public class UsuarioFacade {
public UsuarioFacade(){
System.out.println("Dentro de constructor -> UsuarioFacade");
}
public void altaUsuario(Usuario per){
try {
System.out.println("Alta Usuario");
Session session = Singleton.getInstance().openSession();
org.hibernate.Transaction tx = session.beginTransaction();
session.save(per);
tx.commit();
session.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void modificacionUsuario(Usuario per){
try {
Session session = Singleton.getInstance().openSession();
org.hibernate.Transaction tx = session.beginTransaction();
session.merge(per);
tx.commit();
session.close();
} catch (Exception e) {
e.printStackTrace();
}
}
當我修改用戶數據,並調用方法來修改用戶將看到以下異常
if(opcion.equals("5")){
response.setContentType("text/html");
String datos = request.getParameter("datos_perfil");
System.out.println("Datos en la opcion 5 contiene "+datos);
JSONObject json = new JSONObject();
json = (JSONObject) JSONSerializer.toJSON(datos);
Usuario user = new Usuario();
user.setNombre(json.getString("name"));
user.setNombre(json.getString("apellidos"));
user.setNombre(json.getString("dni"));
user.setNombre(json.getString("telefono"));
user.setNombre(json.getString("email"));
user.setNombre(json.getString("direccion"));
user.setNombre(json.getString("numero"));
user.setNombre(json.getString("poblacion"));
user.setNombre(json.getString("cp"));
user.setNombre(json.getString("provincia"));
user.setNombre(json.getString("login"));
facade.modificacionUsuario(user);
}
錯誤日誌:
org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): modelos.Usuario
No entre en ninguno
at org.hibernate.id.Assigned.generate(Assigned.java:53)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.event.def.DefaultMergeEventListener.saveTransientEntity(DefaultMergeEventListener.java:415)
at org.hibernate.event.def.DefaultMergeEventListener.mergeTransientEntity(DefaultMergeEventListener.java:341)
at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:303)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:258)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:84)
at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:867)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:851)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:855)
at facade.UsuarioFacade.modificacionUsuario(UsuarioFacade.java:51)
at servlet.UsuarioServlet.doPost(UsuarioServlet.java:223)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
我在做什麼錯? 感謝和問候