我已在cascades
JPA
和hibernate
上阅读了几篇帖子。我尝试过所有这些,但都没有帮助。
我正在尝试保存一个Movie对象(与Actor有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表上的actor_movie(foreign column
)为空。
场景2 :将Actor与电影相关联
我也试过这个:
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();
}
}
这里我注意到在数据库中,一行在影片表中保留,但在actor表中没有持久的行。
我不明白为什么这两种情况失败了?请说明为什么会出错?
非常感谢
答案 0 :(得分:4)
由于Movie和Actor之间存在双向关联,因此必须相互设置。
情景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");
actor3.setMovie(movie1); // Missing in ur code
Actor actor4=new Actor("Joe");
actor4.setMovie(movie1); // Missing in ur code
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();
}
}
情景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);
movie.getActors.add(actor1); // missing in ur code
movie.getActors.add(actor2); // missing in ur code
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.persist(movie1);
session.getTransaction().commit();
session.close();
}
}
希望这有帮助。
有关详情,请参阅我的博客Here