順序付けされた子を使用して、双方向の親子設計をモデル化しようとしています。
親から子 (例: 3 つの子のうちの子 #2) を削除すると、「削除」(ターゲット) の前に「更新」(兄弟) が実行されるため、休止状態で生成された SQL で一意制約違反が発生しました。
私が使用している RDBMS (H2) は、遅延制約をサポートしていません。次のオプション以外にどのようなオプションがありますか?
- スキーマから一意の制約を削除します
- 休止状態に依存するのではなく、自分でソートを明示的に管理します
'update' の前に 'delete' を指定して休止状態で SQL を生成する方法はありますか?
フォーラムで見つかったいくつかの古い議論:
コレクション内での DELETE と INSERT - 実行された SQL の順序
DB スキーマ:
CREATE TABLE IF NOT EXISTS Sequences (
ID BIGINT NOT NULL AUTO_INCREMENT,
Name LONGVARCHAR,
Type LONGVARCHAR,
Sequence LONGVARCHAR,
ParentId BIGINT DEFAULT NULL,
Index INT,
CONSTRAINT pk_SequenceId PRIMARY KEY (ID),
CONSTRAINT uc_Sequences UNIQUE (ParentId, Index),
CONSTRAINT fk_Sequences
FOREIGN KEY (ParentId)
REFERENCES Sequences(ID)
ON UPDATE CASCADE
ON DELETE CASCADE
);
クラス:
public class Sequence {
protected Long ID;
protected String name;
protected String type;
protected String sequence;
protected Sequence parentSequence;
protected List<Sequence> childSequences = new ArrayList<Sequence>();
}
HBM マッピング:
<hibernate-mapping>
<class name="Sequence" table="Sequences">
<id name="ID" column="ID" type="long">
<generator class="native"/>
</id>
<property name="name" type="string" column="Name"/>
<property name="type" type="string" column="Type"/>
<property name="sequence" type="string" column="Sequence"/>
<many-to-one name="parentSequence" column="parentId" cascade="save-update" insert="false" update="false" class="Sequence" />
<list name="childSequences" inverse="false" lazy="true" cascade="all-delete-orphan">
<key column="parentId" not-null="true"/>
<list-index column="Index" base="0"/>
<one-to-many class="Sequence"/>
</list>
</class>