I have 2 entities:
Contracts, which has only id, number
Additive, which has id, number, contract
The additive contract is a foreign key, in the database it is working right
works like this: 1 contract may have several additives and 1 additive belongs to only 1 contract
I want to get a contract, he bring me the list of all the additives related to that contract, so to model I did so
Classe Aditivo:
@Id
@GeneratedValue
private Long id;
@Column(length = 300)
private String numero;
@ManyToOne
@JoinColumn(name="contrato", referencedColumnName = "id")
private Contrato contrato;
@Override
public String toString() {
return "Aditivo [id=" + id + ", numero=" + numero + ", contrato="
+ contrato + "]";
}
Classe Contrato:
@Id
@GeneratedValue
private Long id;
@Column(length = 300)
private String numero;
@OneToMany(mappedBy="contrato",fetch=FetchType.EAGER,cascade=CascadeType.ALL)
@Fetch(FetchMode.SUBSELECT)
private List<Aditivo> aditivo;
@Override
public String toString() {
return "Contratos [id=" + id + ", aditivo=" + aditivo + ", numero="
+ numero + "]";
}
But when I give a getContratos
, it gives me a StackOverflowError
In this error it says a lot, I'll put some lines:
java.lang.String.valueOf (Unknown Source) java.lang.StringBuilder.append (Unknown Source) model.Aditivo.toString (Additive.java:96) java.lang.String.valueOf (Unknown Source) java.lang.StringBuilder.append (Unknown Source) java.util.AbstractCollection.toString (Unknown Source) org.hibernate.collection.internal.PersistentBag.toString (PersistentBag.java:501)
java.lang.String.valueOf (Unknown Source) java.lang.StringBuilder.append (Unknown Source) model.Contratos.toString (Contracts.java:491) java.lang.String.valueOf (Unknown Source) ...