MultiKey FK fails in the child insert with Hibernate

130 Views Asked by At

I got the following code, the problem is that the child Mmt, doesn't get fill up the FK with the mmt.setMapMovAnomes(miMapMovAnomes) If I check in debug, the object mmt, have data into the parent property MapMovAnomes, then you don't get any error but If you check into database the FK fields are empty.

@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();
}

relationship in the POJO of mmt

    @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;
}

I have Also the following relationships: 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;
}

I has discover that the problem is that I have map->MapMovAnomes -> mmt and map-> mmt, then If I take off the relationship map->mmt, the it works fine, but how can this make to work keeping this relationship.

0

There are 0 best solutions below