添加到manyToMany抛出stackoverflower

时间:2018-04-04 08:44:05

标签: java hibernate jpa

我在JPA中有一个manyToMany关系,当我尝试插入数据时,它会在我添加的第二个元素上抛出stackoverflow错误。我假设一个循环引用,但无法弄清楚如何解决它。

@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() {

}
在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>

1 个答案:

答案 0 :(得分:0)

你能给我们整个映射文件吗?

将注释与xml配置混合没有问题:In Hibernate: is it possible to mix Annotations and XML configuration for an Entity?

该关系的所有者MaterialServer

我没有看到你的整个地图文件,但你应该有:

<强> 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>

我没有测试它。