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