Android Servlet与Hibernate通信
晚安 在我的应用程序中,我通过Hibernate访问数据库。我想从用户那里发送JSON数据 hibernate表(用户)涉及的代码如下:Android Servlet与Hibernate通信,android,hibernate,Android,Hibernate,晚安 在我的应用程序中,我通过Hibernate访问数据库。我想从用户那里发送JSON数据 hibernate表(用户)涉及的代码如下: <hibernate-mapping> <class name="modelos.Usuario" table="usuario" catalog="mydb"> <id name="login" type="string"> <column name="login" length="20" /
<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>
//由Hibernate Tools 3.4.0.CR1生成的19-abr-2012 18:48:54
导入java.util.HashSet;
导入java.util.Set
/**
*由hbm2java生成的Usuario
*/
公共类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;
}
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;
}
运行应用程序时,出现以下错误:
net.sf.json.JSONException:层次结构中有一个循环!
位于net.sf.json.util.CycleDetectionStrategy$StrictCycleDetectionStrategy.HandlerRepeatedReferenceAsObject(CycleDetectionStrategy.java:97)
位于net.sf.json.JSONObject.\u fromBean(JSONObject.java:857)
位于net.sf.json.JSONObject.fromObject(JSONObject.java:192)
位于net.sf.json.JSONObject.\u processValue(JSONObject.java:2774)
位于net.sf.json.JSONObject.\u setInternal(JSONObject.java:2798)
位于net.sf.json.JSONObject.setValue(JSONObject.java:1507)
位于net.sf.json.JSONObject.\u fromBean(JSONObject.java:940)
位于net.sf.json.JSONObject.fromObject(JSONObject.java:192)
net.sf.json.JSONArray.\u processValue(JSONArray.java:2557)
位于net.sf.json.JSONArray.processValue(JSONArray.java:2588)
位于net.sf.json.JSONArray.addValue(JSONArray.java:2575)
位于net.sf.json.JSONArray.\u fromCollection(JSONArray.java:1082)
位于net.sf.json.JSONArray.fromObject(JSONArray.java:145)
net.sf.json.JSONObject.\u processValue(JSONObject.java:2749)
位于net.sf.json.JSONObject.\u setInternal(JSONObject.java:2798)
位于net.sf.json.JSONObject.setValue(JSONObject.java:1507)
位于net.sf.json.JSONObject.\u fromBean(JSONObject.java:940)
位于net.sf.json.JSONObject.fromObject(JSONObject.java:192)
位于net.sf.json.JSONObject.\u processValue(JSONObject.java:2774)
位于net.sf.json.JSONObject.processValue(JSONObject.java:2833)
位于net.sf.json.JSONObject.element(JSONObject.java:1871)
位于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;
<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>
//由Hibernate Tools 3.4.0.CR1生成的19-abr-2012 18:48:54
导入java.util.HashSet;
导入java.util.Set
/**
*由hbm2java生成的Rol
*/
公共类Rol实现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;
}
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;
}
private int idRol;
私有字符串名称;
私有布尔isAdmin;
私有集usuarios=新哈希集(0);
公共角色{
}
公共Rol(国际idRol){
this.idRol=idRol;
}
公共Rol(整数idRol、字符串nombre、布尔值isAdmin、集合usuarios){
this.idRol=idRol;
this.nombre=nombre;
this.isAdmin=isAdmin;
this.usuarios=usuarios;
}
public int getIdRol(){
返回此.idRol;
}
公共无效设置idRol(内部idRol){
this.idRol=idRol;
}
公共字符串getNombre(){
返回这个.nombre;
}
public void setNombre(字符串nombre){
this.nombre=nombre;
}
公共布尔getIsAdmin(){
返回此.isAdmin;
}
public void setIsAdmin(布尔值isAdmin){
this.isAdmin=isAdmin;
}
公共集getUsuarios(){
把这个还给我;
}
公共无效集合usuarios(集合usuarios){
this.usuarios=usuarios;
}
}
代码由Hibenate生成为Usuario代码
xml如下所示:
package modelos;
<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>
在粘贴的代码中,没有“Rol”类或表定义?在粘贴的代码中,没有“Rol”类或表定义?角色类也是由Hibernate生成的。它有一个类似于usuario的代码。我问您这个问题,因为可能有错误将Rol链接回usuario,所以net.sf.json.JSONObject.fromObject()无法序列化它,在对象引用中创建某种无限循环该角色类也是由Hibernate生成的。它的代码类似于usuario我问您这个问题,因为可能有错误将Rol链接回usuario,因此net.sf.json.JSONObject.fromObject()是无法序列化它,在对象引用中创建某种无限循环