使用Hibernate的子插入中MultiKey FK失败

时间:2012-10-19 16:11:08

标签: hibernate foreign-keys multikey

我得到了以下代码,问题是子Mmt,没有用mmt.setMapMovAnomes填充FK(miMapMovAnomes)如果我检查调试,对象mmt,将数据放入父属性MapMovAnomes ,那么你没有得到任何错误,但如果你检查数据库,FK字段是空的。

@Test
public void creaMmt()
{
    Transaction tx = null
    Date fecha = getFecha("2012-12-09")
    Integer anho = fecha.year + 1900
    Integer mes = fecha.month
    Integer anomes = anho * 100 + mes
    Integer idmap = 10000
    Mmt mmt = new Mmt()
    MapMovAnomes miMapMovAnomes = new MapMovAnomes()
    Session session = staticFactory.getCurrentSession();
    tx = session.beginTransaction()

    Map miMap = session.get (Map.class, idmap)
        MapMovAnomesId mId = new MapMovAnomesId()
        mId.setAnho(anho)
        mId.setMes(mes)
        mId.setIdMap(idmap)
        miMapMovAnomes.setId(mId)
        miMapMovAnomes.setMap(miMap)
        session.save miMapMovAnomes
    mmt.setMapMovAnomes(miMapMovAnomes)
    mmt.setFechMovimiento(fecha)
    mmt.setCantidad(100)
    mmt.setClaveES("E")
    session.save mmt
    miMapMovAnomes.addMmt(mmt)

    tx.commit();
}

在mmt的POJO中的关系

    @ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({
        @JoinColumn(name = "id_map", referencedColumnName = "id_map", insertable = false, updatable = false),
        @JoinColumn(name = "anho", referencedColumnName = "anho", insertable = false, updatable = false),
        @JoinColumn(name = "mes", referencedColumnName = "mes", insertable = false, updatable = false) })
public MapMovAnomes getMapMovAnomes() {
    return this.mapMovAnomes;
}

public void setMapMovAnomes(MapMovAnomes mapMovAnomes) {
    this.mapMovAnomes = mapMovAnomes;
}

我还有以下关系:map - > mapMovAnomes and map - > MMT

@OneToMany(fetch = FetchType.LAZY, mappedBy = "map")
public Set<MapMovAnomes> getMapMovAnomeses() {
    return this.mapMovAnomeses;
}

public void setMapMovAnomeses(Set<MapMovAnomes> mapMovAnomeses) {
    this.mapMovAnomeses = mapMovAnomeses;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "map")
public Set<Mmt> getMmts() {
    return this.mmts;
}

public void setMmts(Set<Mmt> mmts) {
    this.mmts = mmts;
}

我发现问题在于我有map-&gt; MapMovAnomes - &gt; mmt和map-&gt; mmt,那么如果我取消关系图 - > mmt,它工作正常,但这怎么能保持这种关系。

0 个答案:

没有答案