hibernate注释中的错误映射
当尝试通过 Manytomany 连接两个表时,出现此错误: 错误 [org.hibernate.tool.hbm2ddl.SchemaUpdate] 失败:更改表 PARAM_TRENDVALUE 添加约束 FK_TrendValue 外键(AreaID、PcID、DeviceID、ValueID)引用 usrIFDBMaster.tblTrdProcessValues 08:44:43,800 错误 [org.hibernate.tool.hbm2ddl.SchemaUpdate] Die 'usrIFDBMaster.tblTrdProcessValues.DeviceID'-Spalte hat nichtenselben Datentyp wie die verweisende 'PARAM_TRENDVALUE.PcID'-Spalte im 'FK_TrendValue'-Fremdschlüssel。
主要休眠正在尝试映射错误的列。
Param.java:
这是关键:
@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;
}
这是映射:
/**
* @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:
这是关键:
@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;
}
这是映射:
/**
* @return the params
*/
@ManyToMany(
mappedBy="trendValues",
targetEntity=Param.class
)
@ForeignKey(name = "FK_TrendValue")
public List<Param> getParams() {
return params;
}
这是我尝试使用的第一个多对多,它应该可以工作,我已经尝试过没有 inversejoincolumns,使用 joincolumns,准确定义“joincolumns/inversejoincolumns”中的表和数据类型,... 不知道还有什么问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
文档说:
The documentation says:
这不是它应该工作的方式,但它有效:我手动添加了连接表,两个外键也是如此,但是,如图所示,我也必须保持键的精确顺序!这不是主表 TrendValues 中显示的顺序,但它似乎是某种随机顺序...
知道,为什么这个顺序必须是这样的,以及我如何更改休眠注释,以便它采用相同的顺序,我将非常感激。
另外,我现在可以使用我的注释启动程序,但表格不再更改。
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.