【问题标题】:hibernate does not recognize an already defined constraint in databasehibernate 无法识别数据库中已定义的约束
【发布时间】:2019-10-23 16:53:28
【问题描述】:

我在数据库级别映射了一个关系,关系的名称是 FK_CLASSIFICATION_FRUITS 使用 db 级别的 sql server 关系映射定义的。 在我的 Java 应用程序中,我在 Fruits 类中有一个实体映射,位于已定义的关系上,如下所示:

@ManyToOne
    @JoinColumn(name = "CLASSIFICATION_FK", referencedColumnName = "id")
    private Classification clasiFk;

上面的实体映射是建立Classification和Fruits之间的关系,这在数据库层面已经定义好了。

当我尝试运行我的应用程序时,我收到此错误提示

     ... 90 more
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Unable to ex
ecute schema management to JDBC target [alter table [FRUITS] add
 constraint FKdctshrn0ebh90up9sh5125nbb foreign key ([CLASSIFICATION_FK]) references
 [CLASSIFICATION]]

更多的堆栈跟踪

    ... 88 more
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The ALTER TABLE stat
ement conflicted with the FOREIGN KEY constraint "FKdctshrn0ebh90up9sh5125nbb ".
The conflict occurred in database "cyz", table "dbo.CLASSIFICATION", column '
ID'.
        at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError
(SQLServerException.java:232)
        at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServ
erStatement.java:1672)
        at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQ
LServerStatement.java:903)
        at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute
(SQLServerStatement.java:796)
        at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7535)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLSe
rverConnection.java:2438)
        at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLSer
verStatement.java:208)
        at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLS
erverStatement.java:183)
        at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeUpdate(SQLServ
erStatement.java:721)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(Sta
tementFacade.java:114)
        at com.sun.proxy.$Proxy113.executeUpdate(Unknown Source)
        at org.hibernate.tool.schema.internal.TargetDatabaseImpl.accept(TargetDa
tabaseImpl.java:56)
        ... 98 more

以上是因为,它没有识别已经定义的约束

请问如何通知hibernate已经在db中定义了关系

【问题讨论】:

  • 请发布模式当前状态的 DDL 并提供完整的异常堆栈跟踪。
  • 你的意思是在数据库级别吗?
  • 如果你的代码像 - @ManyToOne @JoinColumn(name = "id") private Classification clasiFk;它应该可以工作。
  • 我应该删除这个 >>> referencedColumnName @HasanuzzamanRana
  • 请尝试使用该代码@user10445503

标签: java sql sql-server hibernate


【解决方案1】:

你能不能试试,通过提供获取类型和可以为空的 false

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "CLASSIFICATION_FK", nullable = false)
Classification clasiFk;

【讨论】:

    猜你喜欢
    • 2017-07-17
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 2019-06-13
    • 2017-05-21
    • 2020-10-20
    • 1970-01-01
    相关资源
    最近更新 更多