0

I have a manyToMany relationship in JPA and when I try to insert data it throws a stackoverflow error on the second element I add. I assume a circular reference but can't figure out how to solve it.

@ManyToMany(targetEntity = Material.class, fetch = FetchType.LAZY)
@JoinTable(
        name="MAT_MATSERV",
        joinColumns=@JoinColumn(name="materialserver_id", referencedColumnName="id"),
        inverseJoinColumns=@JoinColumn(name="material_id", referencedColumnName="id"))
private Collection<IMaterial> materials= new ArrayList<>();

public MaterialServer() {

Material.java

private Collection<IMaterialServer> materialServers= new ArrayList<>();

public Material() {

}

relationship defined in Material.xml

        </many-to-one>
        <many-to-many name="materialServers" mapped-by="materials"  fetch="LAZY" target-entity="dst.ass1.jpa.model.impl.MaterialServer">
            <cascade>
                <cascade-all/>
            </cascade>
        </many-to-many>
user2147674
  • 2,319
  • 2
  • 14
  • 17

1 Answers1

0

Can you give us the entire mapping file ?

There is no problem to mix annotation with xml configuration : In Hibernate: is it possible to mix Annotations and XML configuration for an Entity?.

The owner of the relationship is MaterialServer.

I don't see you entire mapping file but you should have something like :

Material.hbm.xml

    <set name="materialServers" table="MAT_MATSERV" inverse="true" lazy="true" fetch="select" cascade="all">
        <key>
            <column name="material_id" not-null="true" />
        </key>
        <many-to-many entity-name="MaterialServer">
            <column name="materialserver_id" not-null="true" />
        </many-to-many>
    </set>

I did not test it.

marojbor
  • 191
  • 10