晚安,
我在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)
我做错了什么? 谢谢和最好的问候
答案 0 :(得分:0)
我认为您的代码中存在错误。您似乎多次设置相同的字段。我假设您要使用JSON设置多个字段?
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"));
这应该设置我假设的不同字段。
我猜你要设置的其中一个字段是你实体的@Id。
顺便说一句,我会认真建议您重新考虑在Android应用程序中使用Hibernate。有许多轻量级框架几乎可以满足您的要求。