hibernate注释中的错误映射

发布于 2025-01-02 14:24:49 字数 2427 浏览 0 评论 0原文

当尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明媚如初 2025-01-09 14:24:49

文档说:

您可以使用@ForeignKey覆盖约束名称。注意
该注释必须放置在
关系,inverseName 引用另一侧约束。

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

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;
    }
}
梦里兽 2025-01-09 14:24:49

这不是它应该工作的方式,但它有效:我手动添加了连接表,两个外键也是如此,但是,如图所示,我也必须保持键的精确顺序!这不是主表 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...enter image description here

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文