休眠状态无法识别数据库中已经定义的约束

时间:2019-06-08 13:33:29

标签: java sql sql-server hibernate

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

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

上述实体映射是为了在分类和水果之间建立关系,并且该关系已经在数据库级别定义。

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

     ... 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

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

请问我如何通知休眠关系已在db中定义

1 个答案:

答案 0 :(得分:0)

您可以尝试使用,通过提供访存类型和可以为null的false来

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