Many to One Hibernate Java

Asked

Viewed 153 times

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?

  • yes, I have now overwritten tostring and hashcode and equals now and continues the same error

  • You can edit the question and put how is your toString and also put how you are doing the getContratos()?

  • 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

  • Try taking @Fetch(Fetchmode.SUBSELECT) to take a test

  • 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

  • If you just use System.out.println(lista.get(0).getAditivo().getId()) of error?

  • yes gives the same error, he can not write the tostring, only direct to variable

  • Direct the variable works?

  • yes, if I give a list.get(0). getAditive(). get(0). getId() it brings the id of the first additive

  • I put an answer that solves your problem. See if it fits you.

Show 7 more comments

2 answers

2


The mistake StackOverflowError happens because you are entering an infinite loop. Think about me, you want to display the additives, but within additive you have a contract that in turn also has a list of additives, ie will always stay in this cycle until giving a StackOverflow.

One solution is to override your method toString() so that only some data is displayed. Example:

@Override
public String toString() {
    return "Aditivo [id=" + id + ", numero=" + numero + ", contrato="
            + contrato.getNumero() + "]";
}

Notice that instead of taking the whole contract I’m only taking your number.

  • exactly worked thanks dear you are the maximum, in the contract I took the additive from the to string and when I need I give a getcontrato.getaditivo worked here, this way you said tb works thanks even, was in infinite loop

  • Yes, both ways it works. For nothing and good studies.

1

Don’t use that

@Fetch(Fetchmode.SUBSELECT)

because you need to use it that way? remove this and resolve.

@OneToMany(targetEntity = Aditivo.class, mappedBy="contrato", fetch=FetchType.EAGER)
@Cascade(value = { CascadeType.ALL })
private List<Aditivo> aditivo;

If you have two collections in the same object they cannot be both Fetchtype.EAGER If you only use the @Cascade JPA specification, remove it

Another problem.

It’s in that code pad

@Override
public String toString() {
    return "Contratos [id=" + id + ", aditivo=" + aditivo + ", numero="
            + numero + "]";
}

Do the proper treatment... it’s generating error there too.. use additive.toString() or something, to solve according to your scenario

  • I’ve taken and remains the same, I posted a comment up there, it does not give error if I give a size, but not time to give a get it gives error

  • I updated the response,

  • If you don’t solve put there, what two more tips

  • did not solve so, continued the same mistake

  • I updated the answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.