JPA/HibernateとJavaの両方に新しく、永続オブジェクトを管理するためにEntityManagerクラスを使用する際の基本にいくつか問題があります。誰かが非常に基本的なことを私に説明すると、私はそれをドキュメントから理解できないので、私は非常に感謝しています。JPA EntityManagerの質問
JSE環境におけるJPA 2/Hibernate/Postgresql。
*
*以下のクラス定義、次の作品を、私はそれを期待通り:
em.getTransaction().begin();
Car corolla = new Car();
Part clutch = new Part();
clutch.setCar(corolla);
Part bumper = new Part();
bumper.setCar(corolla);
em.persist(corolla);
em.persist(clutch);
em.persist(bumper);
em.getTransAction().commit();
しかし、これはデータベースに車への部品からのリンクを削除しません。
tx.begin();
corolla.getParts().clear();
tx.commit();
なぜこの場合ですか?
ありがとうございました。申し訳ありませんが、これはばかげた質問です。
マイク。
Carクラス:
@Entity
public class Car {
private Long id;
private Set<Part> parts;
....
public Car() { parts = new HashSet<Part>(); }
@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy = "increment")
public Long getId() { return id; }
private void setId(Long id) { this.id = id; }
@OneToMany(mappedBy="car", cascade=CascadeType.ALL)
public Set<Part> getParts() { return this.parts; }
public void setParts(Set<Part> parts) { this.parts = parts; }
....
}
パートクラス:
EntityManager.remove
axtavt(下記)からの回答に加え、このポストhttp://stackoverflow.com/questionsにduffymoの答えにオブジェクト設計における関係を埋め込むことについて少し/ 949427/this-it-to-call-a-flush-jpa-interface-in-this-situationは、これを整理するのに役立ちました。 – mdtsandman