1

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

Isako
  • 35
  • 8
  • Copy the error message as text and add it to the question (for the error message formatting you can use block quote style) in order to make it more readable and indexable by search engines. Moreover, reduce the class properties in order to create a https://stackoverflow.com/help/mcve – perissf Apr 13 '17 at 07:14
  • Possible duplicate of [Mapping many-to-many association table with extra column(s)](http://stackoverflow.com/questions/5127129/mapping-many-to-many-association-table-with-extra-columns) – XiCoN JFS Apr 13 '17 at 11:42
  • take a look at [many to many with additional columns](http://stackoverflow.com/a/5127262/5909679) – XiCoN JFS Apr 13 '17 at 11:44
  • Please check answers. @Isako – ooozguuur Apr 14 '17 at 12:42
  • Hi, excuse me for not responding, I'm new to jpa I usually use jdbc templates, I'm trying to make the relationship "many to many". I am attaching my source code at https://github.com/IsaacVe/auth so they can see my settings, I am trying with all the answers. Sorry if it is duplicated. – Isako Apr 17 '17 at 14:32

4 Answers4

1

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.

Shafin Mahmud
  • 3,831
  • 1
  • 23
  • 35
Subrata nath
  • 172
  • 10
1

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;
}
ooozguuur
  • 3,396
  • 2
  • 24
  • 42
  • thanks it works. Which is the correct phrase to look for like this type of results in json{ empresa :{ "id" : 1 "nombre" : "EMP 1" sucursales :{ id : "1", nombre: "SUC 1" } } } – Isako Apr 18 '17 at 14:17
1
@JsonIgnore
@OneToMany(mappedBy="empresa")
private List<EmpresaSucursal> empresaSucursal;
Ady Junior
  • 1,040
  • 2
  • 10
  • 18
0

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)

Grim
  • 1,938
  • 10
  • 56
  • 123