2016-12-13 23 views
1

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=0parent_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の値を変更することは簡単な作業ではありません。 (誰もがデータ変換を嫌う)。

どのようなヒントも非常に感謝しています。

答えて

0

MariaDB/MySQLハンドルAUTO_INCREMENTしたがって、数値は1以上です。 0は有効なシーケンス番号です。 JPAがこれらの(そしてその他の)制限事項で生活できない場合、JPAは壊れています。 (申し訳ありませんが、私はMySQLユーザの人生を困難にするサードパーティ製のソフトウェアに苛立つでしょう)

関連する問題