Home Wrong mapping in hibernate annotations
Reply: 2

Wrong mapping in hibernate annotations

Charles
1#
Charles Published in 2012-02-06 07:58:39Z

I get this error, when trying to connect two table via manytomany: ERROR [org.hibernate.tool.hbm2ddl.SchemaUpdate] Unsuccessful: alter table PARAM_TRENDVALUE add constraint FK_TrendValue foreign key (AreaID, PcID, DeviceID, ValueID) references usrIFDBMaster.tblTrdProcessValues 08:44:43,800 ERROR [org.hibernate.tool.hbm2ddl.SchemaUpdate] Die 'usrIFDBMaster.tblTrdProcessValues.DeviceID'-Spalte hat nicht denselben Datentyp wie die verweisende 'PARAM_TRENDVALUE.PcID'-Spalte im 'FK_TrendValue'-Fremdschlüssel.

Principaly hibernate is trying to map the wrong columns.

Param.java:

This is the key:

@EmbeddedId
@AttributeOverrides( {
        @AttributeOverride(name = "pcId", column = @Column(name = "PcID", nullable = false)),
        @AttributeOverride(name = "unitId", column = @Column(name = "UnitID", nullable = false)),
        @AttributeOverride(name = "paramId", column = @Column(name = "ParamID", nullable = false)) })
public ParamId getId() {
    return this.id;
}

This is the mapping:

/**
 * @return the connection
 */
@ManyToMany
@ForeignKey(name = "FK_Param")
@JoinTable(
        name="PARAM_TRENDVALUE",
        inverseJoinColumns={    
                @JoinColumn(name = "PcID", referencedColumnName = "PcID"),
                @JoinColumn(name = "AreaID", referencedColumnName = "AreaID"),
                @JoinColumn(name = "DeviceID", referencedColumnName = "DeviceID"),
                @JoinColumn(name = "ValueID", referencedColumnName = "ValueID")
        }
)
public List<TrendValue> getTrendValues() {
    return trendValues; 
}

Trendvalue.java:

This is the key:

@EmbeddedId
@AttributeOverrides( {
        @AttributeOverride(name = "pcId", column = @Column(name = "PcID", nullable = false)),   
        @AttributeOverride(name = "areaId", column = @Column(name = "AreaID", nullable = false)),
        @AttributeOverride(name = "deviceId", column = @Column(name = "DeviceID", nullable = false)),
        @AttributeOverride(name = "valueId", column = @Column(name = "ValueID", nullable = false))
         })
public TrendValueId getId() {
    return this.id;
}

This is the mapping:

/**
 * @return the params
 */
@ManyToMany(
        mappedBy="trendValues",
        targetEntity=Param.class
    )
@ForeignKey(name = "FK_TrendValue")
public List<Param> getParams() {
    return params;
}

It´s the first manytomany I try to use, and it should work, I already tried without inversejoincolumns, with joincolumns, defining exactly the tables and datatypes in "joincolumns/inversejoincolumns", ... Don't know what else could be the problem.

JB Nizet
2#
JB Nizet Reply to 2012-02-06 08:05:34Z

The documentation says:

You can override the constraint name by use @ForeignKey. Note that this annotation has to be placed on the owning side of the relationship, inverseName referencing to the other side constraint.

@Entity
public class Woman {
    ...
    @ManyToMany(cascade = {CascadeType.ALL})
    @ForeignKey(name = "TO_WOMAN_FK", inverseName = "TO_MAN_FK")
    public Set<Man> getMens() {
        return mens;
    }
}
Charles
3#
Charles Reply to 2012-02-06 08:55:59Z

This is not the way it should work, but it worked: I added the connection table manually, both the foreign keys too, but, as shown in the picture I had too keep this exact order of the keys! Thats not the order as it is shown in the main table TrendValues, but it seems to be some random order...

If somebody knows, why this order has to be this way, and how I can change hibernate annotations, so that it takes this same order, I'd be very grateful.

Also, I can now start the program, with the my annotations but the tables aren't changed anymore.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.2984 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO