我正在尝试使用JpaRepository在Spring中保存我的实体(和子项),然后休眠到SQL框中。我在TransactionLog项上收到FK异常,没有首先保存。
父实体看起来像:
@Entity
@Table(name="ExceptionLogs")
public class ExceptionLog {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@JsonProperty(access=Access.READ_ONLY)
private int exceptionLogId = 0;
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="TransactionLogId", referencedColumnName="TransactionLogId")
private TransactionLog transactionLog;
子实体看起来像:
@Entity
@Table(name="TransactionLogs")
public class TransactionLog {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@JsonProperty(access=Access.READ_ONLY)
private int transactionLogId = 0;
当我保存父实体时,出现异常。我的保存代码很简单:
TransactionLog transactionLog = new TransactionLog();
transactionLog.setBlah(…);
ExceptionLog exceptionLog = new ExceptionLog();
exceptionLog.setBlah(…);
exceptionLog.setTransactionLog(transactionLog);
exceptionLogRepository.save(exceptionLog);
两个对象的仓库都是JpaRepositories:
@Repository
public interface ExceptionLogRepository extends JpaRepository<ExceptionLog, Integer> {
}
@Repository
public interface TransactionLogRepository extends JpaRepository<TransactionLog, Integer> {
}
我想念什么?
编辑:
例外是FK约束:
com.microsoft.sqlserver.jdbc.SQLServerException:INSERT语句与FOREIGN KEY约束“ FK_ExceptionLogs_TransactionLogs”冲突。在数据库“ xxx”的表“ dbo.TransactionLogs”的列“ TransactionLogId”中发生了冲突。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:262) ~[mssql-jdbc-8.1.0.jre13-preview.jar:na]
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1632) ~[mssql-jdbc-8.1.0.jre13-preview.jar:na]
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:600) ~[mssql-jdbc-8.1.0.jre13-preview.jar:na]
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:522) ~[mssql-jdbc-8.1.0.jre13-preview.jar:na]
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7218) ~[mssql-jdbc-8.1.0.jre13-preview.jar:na]
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:3051) ~[mssql-jdbc-8.1.0.jre13-preview.jar:na]
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:247) ~[mssql-jdbc-8.1.0.jre13-preview.jar:na]
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:222) ~[mssql-jdbc-8.1.0.jre13-preview.jar:na]
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:471) ~[mssql-jdbc-8.1.0.jre13-preview.jar:na]
at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~[HikariCP-3.4.1.jar:na]
at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) ~[HikariCP-3.4.1.jar:na]
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:197) ~[hibernate-core-5.4.8.Final.jar:5.4.8.Final]
at org.hibernate.dialect.identity.GetGeneratedKeysDelegate.executeAndExtract(GetGeneratedKeysDelegate.java:57) ~[hibernate-core-5.4.8.Final.jar:5.4.8.Final]
谢谢!