每当我的EQUIGNORE语句由于重复值而失败时,Hibernate都会返回“The database returned no natively generated identity value”错误。然而,在其他插入上一切都很好,即使它失败并出错,内部id也会增加(例如,第一次插入得到id 1,然后我得到一个失败,然后我尝试插入其他东西,它得到id 3)。
下面是Java实体:
@Entity
@Getter
@AllArgsConstructor
@NoArgsConstructor(force = true)
@With
@ToString
@Table(name = "commission_warning", uniqueConstraints = {@UniqueConstraint(columnNames = {"commission_id", "type"})})
@SQLInsert(sql = "INSERT IGNORE INTO commission_warning (commission_id, type) VALUES (?, ?)")
public class CommissionWarning {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private final Long id;
@ManyToOne
private final Commission commission;
@Enumerated(EnumType.STRING)
private final CommissionWarningType type;
}
MySQL MySQL会生成下面的CREATE语句来复制表,在我看来一切正常:
CREATE TABLE `commission_warning` (
`id` bigint NOT NULL AUTO_INCREMENT,
`type` varchar(255) DEFAULT NULL,
`commission_id` bigint DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UKbffm7pdogka4homn1e47fasw` (`commission_id`,`type`),
CONSTRAINT `FKtg3yujdt6esjrwmer7cdsyalw` FOREIGN KEY (`commission_id`) REFERENCES `commission` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
你知道是什么原因导致忽略的语句导致这样的错误吗?Thanks in advance
1条答案
按热度按时间4xy9mtcn1#
经过更多的测试后,我发现REPLACE而不是EQUIPIGNORE确实有效。我更希望避免与REPLACE相关的额外查询,但它应该是我的情况下可以接受的解决方案。