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)

我究竟做错了什么?谢谢和最好的问候

4

1 回答 1

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。有许多轻量级框架几乎可以肯定满足您的要求。

于 2012-06-17T23:17:51.653 回答