0
我已閱讀cascades
JPA
和hibernate
幾個帖子。我試了一下,但都沒有幫助。JPA級聯在雙向關聯中不使用Hibernate?
我想保存一個電影對象(其中有oneToMany
與演員關聯)。電影對象是持久的,但與電影關聯的演員對象不是。
這裏是我entity
類:
@Entity
public class Movie {
@Id
@Column(name="MOVIE_ID")
@GeneratedValue
private Long id;
@Column(name="MovieName")
private String name;
@Column(name="Date_Released")
private Date date;
@OneToMany(mappedBy="movie",fetch=FetchType.EAGER,cascade=CascadeType.PERSIST)
private Set<Actor> actors=new HashSet<>();
public Movie(){}
public Movie(String name, Date released){
this.name=name;
this.date=released;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Set<Actor> getActors() {
return actors;
}
public void setActors(Set<Actor> actors) {
this.actors = actors;
}
public String toString(){
return id + " : " + name + " : " + date;
}
public void addActor(Actor a){
getActors().add(a);
}
}
@Entity
public class Actor {
@Id
@Column(name="ACTOR_ID")
@GeneratedValue
private Long id;
@Column(name="ACTOR_NAME")
private String name;
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="ACTOR_MOVIE")
private Movie movie;
public Actor(){}
public Actor(String name){
this.name=name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Movie getMovie() {
return movie;
}
public void setMovie(Movie movie) {
this.movie = movie;
}
public String toString(){
return name + " : " + movie.getName();
}
}
方案1個關聯電影演員
public class HibernateTest {
public static void main(String[] args) {
Movie movie1 = new Movie("Mr and Mrs.Smith", new Date(2005, 6, 5));
Actor actor3=new Actor("Brad");
Actor actor4=new Actor("Joe");
movie1.getActors().add(actor3);
movie1.getActors().add(actor4);
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.persist(movie1);
session.getTransaction().commit();
session.close();
}
}
,當我看着桌子上,我看到一排中創建電影表和演員表中的兩行。但是沒有關聯。 actor表上的actor_movie(foreign column
)爲空。
場景2:與電影
相關聯的演員我也試過這樣:
public class HibernateTest {
public static void main(String[] args) {
Movie movie = new Movie("Titanic", new Date(1997,12,19));
Actor actor1=new Actor("Leo");
Actor actor2=new Actor("Kate");
actor1.setMovie(movie);
actor2.setMovie(movie);
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.persist(movie1);
session.getTransaction().commit();
session.close();
}
}
在這裏,我看到在一排在影片表堅持的數據庫,但沒有行被演員堅持表。
我不明白爲什麼這兩種情況都會失敗?請提供解釋爲什麼它會出錯?
非常感謝
感謝您的回答和博客。我正在閱讀它。我有另一個問題發佈在這裏:http://stackoverflow.com/questions/26107553/should-both-sides-of-onetomany-relationship-in-a-bi-directional-association-be-u請幫助,如果你可以 – 2014-09-29 23:59:40
我剛剛閱讀您的博客:我在這裏做的與您在博客中發佈的內容相同:在您的博客中,創建兩個地址對象並將其附加到員工對象。在我上面的場景1中,我創建了兩個actor並將其附加到Movie對象。你看到我的觀點了嗎? – 2014-09-30 00:05:49
但你沒有做反向的方式。看到我評論的行//在你的代碼中丟失。這些線路在您的方案1中缺失。 – javafan 2014-09-30 00:41:10