hibernate(またはmariadb)JPAが外部キーの0値で機能しないようです。JPA外部キー0値hibernate TransientPropertyValueException
私はだから私たちは親の中で2行を持つ親クラス
class Parent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "PARENT_ID", unique = true, nullable = false)
private Integer parentId;
}
と子クラス
class Child {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "CHILD_ID", unique = true, nullable = false)
private Integer childId;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "PARENT_ID", nullable = false)
private Parent parent;
}
を持っています。
java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation : com.whatever.Child.parent -> com.whatever.Parent
しかし、次のよう:parent_id=0
とparent_id=1
私の問題は、私は
Parent p = entityManager.find(Parent.class, new Integer(0));
Child c = new Child();
c.setParent(p);
entityManager.persist(c);
がエラーで失敗します。このコード、つまりID 0で親を使用しようとエラーが出るということです正常に動作します:
Parent p = entityManager.find(Parent.class, new Integer(1));
Child c = new Child();
c.setParent(p);
entityManager.persist(c);
だから私はを想定します何らかの形でJPAが有効な親オブジェクトではないと考えることを混乱させます。
これは実際にはmariadbの問題ですか?あなたがAUTO_INCREMENT
の列に0を挿入するためにセッション設定を変更しなければならないという事実に関連しています。
この設定を行うための設定や注釈はありますか?残念ながら、JPAコードを既存のシステムに入れているため、PARENT_ID
の値を変更することは簡単な作業ではありません。 (誰もがデータ変換を嫌う)。
どのようなヒントも非常に感謝しています。