我正在尝试使用删除具有一对多父子关系的父实体entityManager.remove(parent)
。但是,从 SQL 日志中,我看到 OpenJPAUPDATE
首先调用并尝试为NOT NULL
字段设置 null。
executing prepstmnt 773394836 UPDATE child SET parent_id = ? WHERE parent_id = ? [params=(null) null, (long) 16]
executing prepstmnt 1127292223 DELETE FROM parent WHERE id = ? [params=(long) 16]
executing prepstmnt 1297966852 DELETE FROM child WHERE lookup_id = ? AND parent_id = ? [params=(int) 1, (long) 16]
如何配置我的类以防止第一次UPDATE
调用?
我的课程:
@Entity
@Table(name = "parent")
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
private String name;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER,
orphanRemoval = true)
@JoinColumn(name = "parent_id")
private List<Child> children;
// getters, etc.
}
@Entity
@Table(name = "child")
@IdClass(ChildPrimaryKey.class)
public class Child {
@Id
@Column(name = "parent_id", nullable = false, updatable = false)
private long parentId;
@Id
@Column(name = "lookup_id")
private int lookupId;
@ManyToOne
@JoinColumn(name = "parent_id", referencedColumnName = "id",
nullable = false, insertable = false, updatable = false)
private Parent parent;
// getters, etc.
}
我的数据库表(在 MySQL 中):
CREATE TABLE IF NOT EXISTS `parent` (
`id` BIGINT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(50) NOT NULL ,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `child` (
`lookup_id` INT NOT NULL ,
`parent_id` BIGINT NOT NULL ,
PRIMARY KEY (`parent_id`, `lookup_id`) ,
INDEX `fk_library_code_idx` (`parent_id` ASC) ,
CONSTRAINT `fk_library_code`
FOREIGN KEY (`parent_id` )
REFERENCES `parent` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;