Stackoverflowerror exception occurring in @Manytomany relationship with JPA

Asked

Viewed 402 times

2

I’m having a problem in an entity relationship @Manytomany. When I try to make a simple query in the entity that contains @Manytomany a stackoverflow occurs.

My rule is simple: My Applicationcustomer can access N Webservicevo and a Webservicevo can be accessed by N Applicationcustomer. Classes are mapped as follows:

Applicaocclusive

@Entity
@Table(schema = "loaders_sch", name = "ws_aplicacoes_clientes")
public class AplicacaoClienteVO implements Serializable {

    private static final long serialVersionUID = 3401460653428856555L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "id", nullable = false)
    private Long id;

    @Column(name = "nome_aplicacao", nullable = false, unique = true)
    private String nome;

    @Column(name = "usuario", nullable = false, unique = true)
    private String usuarioAcesso;

    @Column(name = "descricao", nullable = true)
    private String descricao;

    @Column(name = "password", nullable = false)
    private String password;

    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.DETACH)
    @JoinTable(name = "ws_app_ws", joinColumns = { @JoinColumn(name = "app_id") }, inverseJoinColumns = { @JoinColumn(name = "webservice_id") })
    private Set<WebserviceVO> webservices;

    @Column(name = "ativo", nullable = false)
    private Boolean ativo;

    @Column(name = "data_criacao", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Calendar dataCriacao;

    @Column(name = "data_atualizacao", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Calendar dataAtualizacao;

    .. getters e setters omitidos

Webservicevo

@Entity
@Table(name = "ws_webservices", schema = "loaders_sch")
public class WebserviceVO implements Serializable {

    private static final long serialVersionUID = -2724837366463353708L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "id", nullable = false)
    private Long id;

    @Column(name = "nome", nullable = false)
    private String nome;

    @Column(name = "aplicacao", nullable = true)
    private String aplicacao;

    @Column(name = "descricao", nullable = true)
    private String descricao;

    @ManyToMany(mappedBy = "webservices", targetEntity = AplicacaoClienteVO.class, fetch = FetchType.EAGER, cascade = CascadeType.DETACH)
    private Set<AplicacaoClienteVO> appCliente;

    @Column(name = "wsdl_url", nullable = false)
    private String wsdlUrl;

    @Column(name = "data_criacao", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Calendar dataCriacao;

    @Column(name = "data_atualizacao", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Calendar dataAtualizacao;

    ...getters e setters omitidos

The query I do that stackoverflow occurs is the simplest: select app from AplicacaoClienteVO app where app.id = :id for example.

The generated intermediate table (generated by Hibernate itself) was thus:

create table WS_APP_WS
(
  app_id        NUMBER(19) not null,
  webservice_id NUMBER(19) not null
)

alter table WS_APP_WS
  add constraint FK_E8YDQBFHUYU2Y1KKYU9YCKARB foreign key (APP_ID)
  references WS_APLICACOES_CLIENTES (ID);
alter table WS_APP_WS
  add constraint FK_5E67UP74MTMPYWJQVQQENCOPN foreign key (WEBSERVICE_ID)
  references WS_WEBSERVICES (ID);

My mappings are wrong?

1 answer

1


Ricardo, you generated the methods toString(), hashCode() and equals() ? If yes, check how they are implemented, because in these cases there will be a circular reference within these methods, generating stackoverflow. In my view the mapping is correct. just confirm this additional detail please !

Hugs

  • Yes, I managed it automatically by the Eclipse generator. What you recommend?

  • @Ricardogiaviti what happens is that you have a circular reference (method of one class calls the method of another infinitely), do not know your level of knowledge, but anyway: If you remove all references from Set<WebserviceVO> webservices; within the methods hashCode(),toString() and equal() of your class AplicacaoClienteVO should work. However, it is not the right one because, until you compare your objects, you may get a wrong result, since hashCode() and equals() do not have all properties defined.

  • @Joshua better put that in the answer, it’s the most important part of it.

Browser other questions tagged

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