2012-06-18 47 views
1

晚安,Servlet与休眠沟通

在我的应用程序中我通过Hibernate访问我的数据库。我想从一个用户发送JSON数据

代码参与休眠表(用户)如下:

<hibernate-mapping> 
<class name="modelos.Usuario" table="usuario" catalog="mydb"> 
    <id name="login" type="string"> 
     <column name="login" length="20" /> 
     <generator class="assigned" /> 
    </id> 
    <many-to-one name="rol" class="modelos.Rol" fetch="join"> 
     <column name="Rol_idRol" not-null="true" /> 
    </many-to-one> 
    <property name="password" type="string"> 
     <column name="password" length="32" /> 
    </property> 
    <property name="nombre" type="string"> 
     <column name="nombre" length="45" /> 
    </property> 
    <property name="apellidos" type="string"> 
     <column name="apellidos" length="45" /> 
    </property> 
    <property name="dni" type="string"> 
     <column name="dni" length="10" /> 
    </property> 
    <property name="direccion" type="string"> 
     <column name="direccion" length="60" /> 
    </property> 
    <property name="numero" type="string"> 
     <column name="numero" length="5" /> 
    </property> 
    <property name="poblacion" type="string"> 
     <column name="poblacion" length="45" /> 
    </property> 
    <property name="cp" type="string"> 
     <column name="cp" length="5" /> 
    </property> 
    <property name="provincia" type="string"> 
     <column name="provincia" length="30" /> 
    </property> 
    <property name="email" type="string"> 
     <column name="email" length="60" /> 
    </property> 
    <property name="telefono" type="string"> 
     <column name="telefono" length="15" /> 
    </property> 
    <set name="reunions" table="reunion" inverse="true" lazy="false" fetch="join"> 
     <key> 
      <column name="Perfil_login" length="20" not-null="true" /> 
     </key> 
     <one-to-many class="modelos.Reunion" /> 
    </set> 
    <set name="cuentas" table="cuenta" inverse="true" lazy="false" fetch="join"> 
     <key> 
      <column name="usuario_login" length="20" not-null="true" /> 
     </key> 
     <one-to-many class="modelos.Cuenta" /> 
    </set> 
    <set name="sesions" table="sesion" inverse="true" lazy="false" fetch="join"> 
     <key> 
      <column name="usuario_login" length="20" not-null="true" /> 
     </key> 
     <one-to-many class="modelos.Sesion" /> 
    </set> 
</class> 

,并生成Java代码:

package modelos; 

//由Hibernate Tools生成19-abr-2012 18:48:54 3.4.0.CR1

import j ava.util.HashSet; import java.util.Set;

/** * 通过Usuario生成调用:hbm2java */ 公共类Usuario实现java.io.Serializable {

private String login; 
private Rol rol; 
private String password; 
private String nombre; 
private String apellidos; 
private String dni; 
private String direccion; 
private String numero; 
private String poblacion; 
private String cp; 
private String provincia; 
private String email; 
private String telefono; 
private Set<Reunion> reunions = new HashSet<Reunion>(0); 
private Set<Cuenta> cuentas = new HashSet<Cuenta>(0); 
private Set<Sesion> sesions = new HashSet<Sesion>(0); 

public Usuario() { 
} 


public Usuario(String login, Rol rol) { 
    this.login = login; 
    this.rol = rol; 
} 
public Usuario(String login, Rol rol, String password, String nombre, String apellidos, String dni, String direccion, String numero, String poblacion, String cp, String provincia, String email, String telefono, Set<Reunion> reunions, Set<Cuenta> cuentas, Set<Sesion> sesions) { 
    this.login = login; 
    this.rol = rol; 
    this.password = password; 
    this.nombre = nombre; 
    this.apellidos = apellidos; 
    this.dni = dni; 
    this.direccion = direccion; 
    this.numero = numero; 
    this.poblacion = poblacion; 
    this.cp = cp; 
    this.provincia = provincia; 
    this.email = email; 
    this.telefono = telefono; 
    this.reunions = reunions; 
    this.cuentas = cuentas; 
    this.sesions = sesions; 
} 

public String getLogin() { 
    return this.login; 
} 

public void setLogin(String login) { 
    this.login = login; 
} 
public Rol getRol() { 
    return this.rol; 
} 

public void setRol(Rol rol) { 
    this.rol = rol; 
} 
public String getPassword() { 
    return this.password; 
} 

public void setPassword(String password) { 
    this.password = password; 
} 
public String getNombre() { 
    return this.nombre; 
} 

public void setNombre(String nombre) { 
    this.nombre = nombre; 
} 
public String getApellidos() { 
    return this.apellidos; 
} 

public void setApellidos(String apellidos) { 
    this.apellidos = apellidos; 
} 
public String getDni() { 
    return this.dni; 
} 

public void setDni(String dni) { 
    this.dni = dni; 
} 
public String getDireccion() { 
    return this.direccion; 
} 

public void setDireccion(String direccion) { 
    this.direccion = direccion; 
} 
public String getNumero() { 
    return this.numero; 
} 

public void setNumero(String numero) { 
    this.numero = numero; 
} 
public String getPoblacion() { 
    return this.poblacion; 
} 

public void setPoblacion(String poblacion) { 
    this.poblacion = poblacion; 
} 
public String getCp() { 
    return this.cp; 
} 

public void setCp(String cp) { 
    this.cp = cp; 
} 
public String getProvincia() { 
    return this.provincia; 
} 

public void setProvincia(String provincia) { 
    this.provincia = provincia; 
} 
public String getEmail() { 
    return this.email; 
} 

public void setEmail(String email) { 
    this.email = email; 
} 
public String getTelefono() { 
    return this.telefono; 
} 

public void setTelefono(String telefono) { 
    this.telefono = telefono; 
} 
public Set<Reunion> getReunions() { 
    return this.reunions; 
} 

public void setReunions(Set<Reunion> reunions) { 
    this.reunions = reunions; 
} 
public Set<Cuenta> getCuentas() { 
    return this.cuentas; 
} 

public void setCuentas(Set<Cuenta> cuentas) { 
    this.cuentas = cuentas; 
} 
public Set<Sesion> getSesions() { 
    return this.sesions; 
} 

public void setSesions(Set<Sesion> sesions) { 
    this.sesions = sesions; 
} 

}

执行的用户更新该servlet具有以下代码:

 if (opcion.equals("4")){ 

     response.setContentType("text/html"); 

     Rol rol = new Rol(); 

     String usuario = request.getParameter("login"); 
     Usuario user = facade.getUsuarioByLogin(usuario); 

     String nombre= user.getNombre(); 
     String apellidos = user.getApellidos(); 
     String dni = user.getDni(); 
     String telefono = user.getTelefono(); 
     String email = user.getEmail(); 
     String direccion = user.getDireccion(); 
     String numero = user.getNumero(); 
     String poblacion = user.getPoblacion(); 
     String cp = user.getCp(); 
     String provincia = user.getProvincia(); 
     String login = user.getLogin(); 
     String contraseña = user.getPassword(); 
     rol = user.getRol(); 


     System.out.println("El valor de Nombre es"+nombre); 

     //System.out.println("Opcion 4 El valor de rol es"+rol); 

     JSONObject jsonObject1 = new JSONObject(); 
     jsonObject1.put("name", nombre); 
     jsonObject1.put("apellidos", apellidos); 
     jsonObject1.put("dni", dni); 
     jsonObject1.put("telefono", telefono); 
     jsonObject1.put("email", email); 
     jsonObject1.put("direccion", direccion); 
     jsonObject1.put("numero",numero); 
     jsonObject1.put("poblacion", poblacion); 
     jsonObject1.put("cp", cp); 
     jsonObject1.put("provincia", provincia); 
     jsonObject1.put("login", login); 
     jsonObject1.put("pass", contraseña); 

     jsonObject1.put("rol", rol); 

当您运行应用程序时出现以下错误:

net.sf.json.JSONException:层次结构中有一个循环! 在net.sf.json.util.CycleDetectionStrategy $ StrictCycleDetectionStrategy.handleRepeatedReferenceAsObject(CycleDetectionStrategy.java:97) 在net.sf.json.JSONObject._fromBean(JSONObject.java:857) 在net.sf.json.JSONObject。在net.sf.net.sf.json.JSONObject._setInternal(JSONObject.java:2798) 处使用fromObject(JSONObject.java:192) at net.sf.json.JSONObject._processValue(JSONObject.java:2774) 。 json.JSONObject.setValue(JSONObject.java:1507) at net.sf.json.JSONObject._fromBean(JSONObject.java:940) at net.sf.json.JSONObject.fromObject(JSONObject.java:192) at net.sf.json.JSONArray._processValue(JSONArray.java:2557) at net.sf.json.JSONArray.processValue(JSONArray.java:2588)在net.sf.json.JSONArray.addValue(JSONArray.java:2575) at net.sf.json.JSONArray._fromCollection(JSONArray.java:1082) at net.sf.json.JSONArray.fromObject(JSONArray。 java:145) at net.sf.json.JSONObject._processValue(JSONObject.java:2749) at net.sf.json.JSONObject._setInternal(JSONObject.java:2798) at net.sf.json.JSONObject。的setValue(JSONObject.java:1507) 在net.sf.json.JSONObject._fromBean(JSONObject.java:940) 在net.sf.json.JSONObject.fromObject(JSONObject.java:192) 在net.sf. json.JSONObject._processValue(JSONObject.java:2774) at net.sf.json.JSONObject.processValue(JSONObject.java:2833) at net.sf.json.JSONObject.element(JSONObject.java:1 871) 在net.sf.json.JSONObject.element(JSONObject.java:1849) 在net.sf.json.JSONObject.put(JSONObject.java:2466) 在servlet.UsuarioServlet.doPost(UsuarioServlet。java:195)

以粗体显示的错误行是出现在上面粘贴的servlet代码中的最后一行。 “jsonObject1.put('rol',rol);”

我在做什么错?

谢谢!

编辑:

ROL类的代码如下:

package modelos; 

//生成19-ABR-2012 18时48分54秒由休眠的工具3.4.0.CR1

import java.util.HashSet; import java.util.Set;

/** * 通过调用:hbm2java 产生ROL */ 公共类ROL实现java.io.Serializable {

private int idRol; 
private String nombre; 
private Boolean isAdmin; 
private Set<Usuario> usuarios = new HashSet<Usuario>(0); 

public Rol() { 
} 


public Rol(int idRol) { 
    this.idRol = idRol; 
} 
public Rol(int idRol, String nombre, Boolean isAdmin, Set<Usuario> usuarios) { 
    this.idRol = idRol; 
    this.nombre = nombre; 
    this.isAdmin = isAdmin; 
    this.usuarios = usuarios; 
} 

public int getIdRol() { 
    return this.idRol; 
} 

public void setIdRol(int idRol) { 
    this.idRol = idRol; 
} 
public String getNombre() { 
    return this.nombre; 
} 

public void setNombre(String nombre) { 
    this.nombre = nombre; 
} 
public Boolean getIsAdmin() { 
    return this.isAdmin; 
} 

public void setIsAdmin(Boolean isAdmin) { 
    this.isAdmin = isAdmin; 
} 
public Set<Usuario> getUsuarios() { 
    return this.usuarios; 
} 

public void setUsuarios(Set<Usuario> usuarios) { 
    this.usuarios = usuarios; 
} 

}

由Hibenate生成作为Usuario代码的代码。

的XML如下:

<hibernate-mapping> 
<class name="modelos.Rol" table="rol" catalog="mydb"> 
    <id name="idRol" type="int"> 
     <column name="idRol" /> 
     <generator class="assigned" /> 
    </id> 
    <property name="nombre" type="string"> 
     <column name="Nombre" length="45" /> 
    </property> 
    <property name="isAdmin" type="java.lang.Boolean"> 
     <column name="isAdmin" /> 
    </property> 
    <set name="usuarios" table="usuario" inverse="true" lazy="false" fetch="select"> 
     <key> 
      <column name="Rol_idRol" not-null="true" /> 
     </key> 
     <one-to-many class="modelos.Usuario" /> 
    </set> 
</class> 

回答

1

在你粘贴代码,没有 “ROL” 类或表定义?

+0

Role类也是由Hibernate生成的。 它有一个类似于Usuario的代码 – F3RN1

+0

我问你这个问题,因为可能存在链接Rol到usuario的错误,所以net.sf.json.JSONObject.fromObject()不能序列化它,创建某种对象引用中的无限循环 – Arcadien