简体   繁体   English

休眠注释中的映射错误

[英]Wrong mapping in hibernate annotations

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. 当尝试通过多方连接两个表时出现此错误:错误[org.hibernate.tool.hbm2ddl.SchemaUpdate]不成功:更改表PARAM_TRENDVALUE添加约束FK_TrendValue外键(AreaID,PcID,DeviceID,ValueID)引用usrIFDBMaster.tblTrdProcessValues 08 :44:43,800错误[org.hibernate.tool.hbm2ddl.SchemaUpdate]死于'usrIFDBMaster.tblTrdProcessValues.DeviceID'-不能正确显示'PARAM_TRENDVALUE.PcID'-Spalte im'FK_TrendValue'-Fre

Principaly hibernate is trying to map the wrong columns. 原则上,冬眠试图映射错误的列。

Param.java: 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: 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. 这是我尝试使用的第一个多语言方法,它应该可以工作,我已经尝试过不使用inversejoincolumns,使用joincolumns,在“ joincolumns / inversejoincolumns”中精确定义表和数据类型的情况,……不知道还有什么可能。问题。

The documentation says: 该文件说:

You can override the constraint name by use @ForeignKey. 您可以使用@ForeignKey覆盖约束名称。 Note that this annotation has to be placed on the owning side of the relationship, inverseName referencing to the other side constraint. 注意,该注释必须放置在关系的拥有侧,inverseName引用另一侧约束。

@Entity
public class Woman {
    ...
    @ManyToMany(cascade = {CascadeType.ALL})
    @ForeignKey(name = "TO_WOMAN_FK", inverseName = "TO_MAN_FK")
    public Set<Man> getMens() {
        return mens;
    }
}

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... 这不是主表TrendValues中显示的顺序,但似乎是一些随机顺序... 在此处输入图片说明

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. 另外,我现在可以使用我的注释启动程序,但是表格不再更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM