1
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 is working fine
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 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 + "]";
}
Only when I’m gonna get one getContratos
, he gives me a StackOverflowError
in this error he says a lot, I’ll put a few 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.Contractos.toString(Contracts.java:491) java.lang.String.valueOf(Unknown Source) ...
You overwrote the Tostring?
– DiegoAugusto
yes, I have now overwritten tostring and hashcode and equals now and continues the same error
– Adriano
You can edit the question and put how is your toString and also put how you are doing the
getContratos()
?– DiegoAugusto
editei, the tostring I did automatically, in getcontratos, I have in Contratosmb a getlista that works normally, only accuses me the error of stackoverflow when I give a System.out.println(list.get(0).getAditivo()); q is for me to see the list of additives q it would return me
– Adriano
Try taking @Fetch(Fetchmode.SUBSELECT) to take a test
– DiegoAugusto
same error except fetch, but it is some error in tostring itself, I did the test and gives error when running System.out.println(list.get(0).getAditive().get(0)); --- but if I give a list.get(0). getAditive(). size() it returns the array size right, and if I give it a list.get(0). getAditive(). get(0). getId() it brings me the additive id correctly, it’s only time to display the whole array
– Adriano
If you just use
System.out.println(lista.get(0).getAditivo().getId())
of error?– DiegoAugusto
yes gives the same error, he can not write the tostring, only direct to variable
– Adriano
Direct the variable works?
– DiegoAugusto
yes, if I give a list.get(0). getAditive(). get(0). getId() it brings the id of the first additive
– Adriano
Let’s go continue this discussion in chat.
– DiegoAugusto
I put an answer that solves your problem. See if it fits you.
– DiegoAugusto