Reputation: 35
I debugged the code and the variable "empresasucursal" has embedded huge amounts of the same information, causing an overflow of memory. What is the correct way to relate the classes according to the database model of the image below
[![empresa_sucursal][1]][1]
Class Java Empresa
@Entity
@Table(name = "empresa")
public class Empresa implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="idempresa")
private Integer idempresa;
@javax.persistence.Temporal(TemporalType.TIMESTAMP)
private Date fechaRegistro;
@Column(name="direccionFiscal")
private String direccion;
private String nombre;
@Column(name="contactoTelefonoCelular")
private String celular;
@Column(name="regimenUnicoContribuyente")
private String ruc;
private String estado;
private String codigoEmpresa;
@OneToMany(mappedBy="empresa")
private List<EmpresaSucursal> empresaSucursal;
}
Class Java Sucursal
@Entity
@Table(name = "sucursal")
public class Sucursal implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idsucursal")
private int idsucursal;
@Column(name = "nombreSucursal")
private String nombre;
private String direccion;
@Column(name = "contactoTelefonoFijo")
private String telefonoFijo;
private String fechaRegistro;
private String estado;
@Column(name = "codigoSucursal")
private String codigoSucursal;
@OneToMany(mappedBy = "sucursal")
private List<EmpresaSucursal> empresaSucursal;
}
Class Java EmpresaSucursal
@Entity
@Table(name = "empresa_sucursal")
public class EmpresaSucursal implements Serializable {
@Id
@ManyToOne
@JoinColumn(name = "idempresa",referencedColumnName="idempresa")
private Empresa empresa;
@Id
@ManyToOne
@JoinColumn(name = "idsucursal" ,referencedColumnName="idsucursal")
private Sucursal sucursal;
@Column(name="estado")
private String estado;
}
Controller Method
@RequestMapping(method = RequestMethod.GET, value = "/{empresaId}", produces
= MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Empresa obtenerEmpresa(@PathVariable Integer empresaId) throws
EmpresaNotExistException{
Empresa empresa =this.empresaRepository.findOne(empresaId);
System.out.println(empresa.toString());
return empresa;
}
ERROR in the console
017-04-17 10:05:10.442 WARN 9788 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: Infinite recursion (StackOverflowError) (through reference chain: com.gdata.auth.bean.Sucursal["empresaSucursal"]->org.hibernate.collection.internal.PersistentSet[0]->com.gdata.auth.bean.EmpresaSucursal["sucursal"]->com.gdata.auth.bean.Sucursal["empresaSucursal"]->org.hibernate.collection.internal.PersistentSet[0]->com.gdata.auth.bean.EmpresaSucursal["sucursal"]->com.gdata.auth.bean.Sucursal["empresaSucursal"]->org.hibernate.collection.internal.PersistentSet[0]->com.gdata.auth.bean.EmpresaSucursal["sucursal"]->com.gdata.auth.bean.Sucursal["empresaSucursal"]->org.hibernate.collection.internal.PersistentSet[0]->com.gdata.auth.bean.EmpresaSucursal["sucursal"]->com.gdata.auth.bean.Sucursal["empresaSucursal"]->org.hibernate.collection.internal.PersistentSet[0]->com.gdata.auth.bean.EmpresaSucursal["sucursal"]->com.gdata.auth.bean.Sucursal["empresaSucursal"]->org.hibernate.collection.internal.PersistentSet[0]->com.gdata.auth.bean.EmpresaSucursal["sucursal"]->com.gdata.auth.bean.Sucursal["empresaSucursal"]->org.hibernate.collection.internal.PersistentSet[0]->com.gdata.auth.bean.EmpresaSucursal["sucursal"]->com.gdata.auth.bean.Sucursal["empresaSucursal"]->org.hibernate.collection.internal.PersistentSet[0]->com.gdata.auth.bean.EmpresaSucursal["sucursal"]->com.gdata.auth.bean.Sucursal["empresaSucursal"]->org.hibernate.collection.internal.PersistentSet[0]-> ..........
2017-04-17 10:05:10.447 WARN 9788 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Handling of [org.springframework.http.converter.HttpMessageNotWritableException] resulted in Exception
Upvotes: 1
Views: 1860
Reputation: 1080
@JsonIgnore
@OneToMany(mappedBy="empresa")
private List<EmpresaSucursal> empresaSucursal;
Upvotes: 1
Reputation: 172
Are you using jpa or Hibernate? In hibernate multiple @id
is allowed in same class. But it is not however JPA compliant. But actually you dont have to use @id
at each reference in EmpresaSucursal
class. Use separate id field instead like `
@Id
@GeneratedValue
private int id;
if you actually need an id field.
And you can use @UniqueConstraint(columnNames = {"", ""})
to define composite key.
Upvotes: 1
Reputation: 3466
These are used to solve the Infinite recursion (StackOverflowError Ex)
You may use @JsonIgnore
to break the cycle.
OR :
@JsonManagedReference is the forward part of reference – the one that gets serialized normally. @JsonBackReference is the back part of reference – it will be omitted from serialization.
Please check exam:
@Entity
@Table(name = "empresa_sucursal")
public class EmpresaSucursal implements Serializable {
/.../
@JsonBackReference
@Id
@ManyToOne
@JoinColumn(name = "idsucursal" ,referencedColumnName="idsucursal")
private Sucursal sucursal;
@Entity
@Table(name = "empresa")
public class Empresa implements Serializable {
/.../
@JsonManagedReference
@OneToMany(mappedBy="empresa")
private List<EmpresaSucursal> empresaSucursal;
}
Upvotes: 1
Reputation: 1974
You must isolate two different models. You have the Database-Model (entitys) and the Frontend-Model (no entitys).
You must transform the entitys into the frontend-model.
For example:
Model frontend = new Model();
frontend.setNombre(empresa.getNombre());
frontend.setTelefonoFijo(empresa.getEmpresaSucursal().getSucursal().getTelefonoFijo());
...
return frontend;
(Just an example, its not working and its not compiling but only a structural example)
Upvotes: 0