1
私はJPAを初めて使いました。 私はどのようなアシスタンスを使用してJPAをデータベースに保存するのかを理解したいと思います。 私の例:PersonクラスはAddressクラスに関連しています。各人は複数の住所を持つことができます。 私はこのように、この事実をモデル:JPA多くの側のクラスの鍵生成
@Entity public class Person implements Serializable { @Id int id; String firstName; @OneToMany(mappedBy="id", cascade={CascadeType.PERSIST, CascadeType.REMOVE}) List<EMailAddress> emailList; //of course i have all my getters
私のEmailAddressクラスは似ています
@Entity public class EMailAddress implements Serializable { @Id @GeneratedValue (strategy=GenerationType.TABLE) Integer id; String address; //here i have all my getters }
[OK]をクリックします。最初の問題は、私は、次の、非常に単純なコードを持つ人のオブジェクトを永続化しようとすると、私は外部キーエラーを取得することです:
Person p0 = new Person(); p0.setFirstName("Alan"); p0.setId(99); //I create an EMailAddress object, and add it to the Person adresses list EMailAddress ma = new EMailAddress(); ma.setAddress("[email protected]"); p0.setEmailList(new ArrayList()); p0.getEmailList().add(ma); em.getTransaction().begin(); em.persist(p0); em.getTransaction().commit();
例外:
10924 [main] ERROR org.hibernate.util.JDBCExceptionReporter - INSERT on table 'EMAILADRESS' has caused a violation of foreign key constraint 'FK95CEBD60BE95C400' for key (327680). The instruction has been rollbacked.