Exception StackOverflowError occurring in @ManyToMany relationship with JPA

2

I'm having a problem in a @ManyToMany entities relationship. When I try to do a simple query on the entity that contains the @ManyToMany stackOverflow occurs.

My rule is simple: My entity ApplicationClientVO can access N WebServiceVO and a WebServiceVO can be accessed by N ApplicationClientVO. The classes are mapped as follows:

ApplicationClientVO

@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 that makes stackoverflow occur is the simplest: select app from AplicacaoClienteVO app where app.id = :id for example.

The generated intermediate table (generated by Hibernate itself) looks like this:

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?

    
asked by anonymous 09.06.2014 / 18:40

1 answer

1

Ricardo, did you generate 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 the stackoverflow. In my view the mapping is correct. just confirm this additional detail please!

Hugs

    
09.06.2014 / 21:46