Hibernate在获取集合时清除@ManyToMany JoinTable

时间:2015-02-27 15:58:13

标签: java hibernate many-to-many jointable

有两个具有ManyToMany关系的anotated类。当我尝试读取链接集合时,Hibernate会返回正确的数据,但之后,删除与对象连接的joinTable中的所有行。因此,在读完对象之间的所有连接后都会丢失。

班级地区有:

private Set<UserFilter> filters = new HashSet<UserFilter>();


@ManyToMany(fetch = FetchType.LAZY, mappedBy = "regions")
public Set<UserFilter> getFilters() {
    return filters;
}

public void setFilters(Set<UserFilter> filters) {
    this.filters = filters;
}

类UserFilter具有:

private Set<Region> regions = new HashSet<Region>();


@ManyToMany(fetch = FetchType.LAZY, cascade =
        {
                javax.persistence.CascadeType.PERSIST,
                javax.persistence.CascadeType.MERGE,
                javax.persistence.CascadeType.REFRESH,
                javax.persistence.CascadeType.DETACH
        })
@JoinTable(name = "FilterAndRegion",
        joinColumns = {@JoinColumn(name = "FilterId", nullable = false, updatable = false)},
        inverseJoinColumns = {@JoinColumn(name = "RegionId", nullable = false, updatable = false)})
public Set<Region> getRegions() {
    return regions;
}

public void setRegions(Set<Region> regions) {
    this.regions = regions;
}

当我运行这样的东西时:

private void updateRegionsTable() {
        //
        regionsTable.removeAllItems();
        //
        if(filter.getRegions().size() == 0 ) {
            regionsTable.addItem(new Object[]{
                            "<no regions>",
                    },
                    0
            );
        } else {
            //
            for (Region region : filter.getRegions()) {
                //
                regionsTable.addItem(new Object[]{
                                region.getName(),
                        },
                        region.getId()
                );
            }
        }
        //
        regionsTable.setPageLength(regionsTable.size());
    }

...在控制台我看到了:

  

Hibernate:选择regions0_.FilterId作为FilterId1_4_0_,   regions0_.RegionId为RegionId2_0_0_,region1_.id为id1_2_1_,   region1_.CatalogName为CatalogN2_2_1_,region1_.Name为Name3_2_1_,   region1_。从FilterAndRegion regions0_ inner join中键入Type4_2_1_   区域region__在regions0_.RegionId = region1_.id其中   regions0_.FilterId =?

     

Hibernate:从FilterAndRegion中删除,其中FilterId =?

为什么会这样?以及如何纠正这种行为(删除joinTable的清除)?

0 个答案:

没有答案