Many to One Hibernate Java

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 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) ...

    
asked by anonymous 10.11.2016 / 11:48

2 answers

2

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

One solution is to overwrite your toString() method 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 getting the entire contract I'm just getting your number.

    
10.11.2016 / 13:40
1

Do not use this

  

@Fetch (FetchMode.SUBSELECT)

Why do 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 can not be both FetchType.EAGER If you only use the @Cascade JPA specification, remove it

Another problem.

This in this code block

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

Do the proper treatment ... it is generating error there as well .. use additive.toString () or something like this to solve according to your scenario

    
10.11.2016 / 13:04