I'm having some trouble with inserting some data into a SQL Server table using Hibernate. I have a table, and I have a FK from the table to itself to establish that entities in this table are related to one another. The PK for the table is type uniqueidentifier and the FK is also type uniqueidentifier, represented in my Java code by UUIDs.
The problem is, when I try to insert an object into my table with one of these FKs set, I get the following error:
java.sql.SQLException: The INSERT statement conflicted with the FOREIGN KEY SAME TABLE constraint <FK_NAME>. The conflict occurred in database <DB_NAME>, table <TABLE_NAME>, column <PK_NAME>.
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:372) ~[jtds-1.3.1.jar:1.3.1]
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2988) ~[jtds-1.3.1.jar:1.3.1]
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2421) ~[jtds-1.3.1.jar:1.3.1]
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:671) ~[jtds-1.3.1.jar:1.3.1]
at net.sourceforge.jtds.jdbc.JtdsStatement.processResults(JtdsStatement.java:613) ~[jtds-1.3.1.jar:1.3.1]
at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQL(JtdsStatement.java:572) ~[jtds-1.3.1.jar:1.3.1]
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeUpdate(JtdsPreparedStatement.java:727) ~[jtds-1.3.1.jar:1.3.1]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_172]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_172]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_172]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_172]
However, I've tried running an INSERT statement on the DB directly using SQL Server Management Studio using the exact same parameters, and the INSERT statement works fine. I've also traced my code in the debugger and the debug trace shows the UUID id being parsed from String to UUID correctly. I copy-pasted the value directly from the PK field in my database as well to make sure there are no typos.
I feel like I've tried everything but I have no idea why I'm getting this error. Can anyone help me out? Thanks.