[英]Hibernate one-to-many mapping (annotations)
I am new to hibernate, I am trying to learn it and I have run into a problem trying to make one to many relationship's work. 我是新来的冬眠者,我想学习它,而在尝试建立一对多关系的工作时遇到了一个问题。 I have tried several examples but none seem to be working.
我尝试了几个示例,但似乎都没有用。
Can someone have a look at the below code and tell me where I have gone wrong. 有人可以看看下面的代码,并告诉我我哪里出错了。 I have tried many different tutorials but it always doesn't work, so I must be missing something.
我尝试了许多不同的教程,但是它始终无法正常工作,因此我必须缺少一些东西。
There are two classes: MovieDb and Genre. 有两个类:MovieDb和Genre。 For each movie there should be many Genre's.
对于每部电影,应该有很多流派。
I believe I have included all the files and information needed, if not let me know. 我相信我已经包括了所有需要的文件和信息,如果没有让我知道的话。
Thanks for the help. 谢谢您的帮助。
MovieDb.java MovieDb.java
@Entity
@Table(name = "movies")
public class MovieDb extends IdElement {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@JsonProperty("id")
@Column(name = "movieId")
private int movieId;
@JsonProperty("title")
@Column(name = "title")
private String title;
@JsonProperty("genres")
@OneToMany
private List<Genre> genres;
public int getMovieId() { return movieId; }
public String getTitle() { return title; }
public List<Genre> getGenres() { return genres; }
public void setMovieId(int movieId) { this.movieId = movieId; }
public void setTitle(String title) { this.title = title; }
public void setGenres(List<Genre> genres) { this.genres = genres; }
}
Genre.java Genre.java
@JsonRootName("genre")
@Entity
@Table(name = "moviesGenre")
public class Genre implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
private int id;
@Column(name = "movieId")
private int movieId;
@JsonProperty("name")
@Column(name = "name")
private String name;
public int getMovieId() { return movieId; }
public String getName() { return name; }
public void setMovieId(int movieId) { this.movieId = movieId; }
public void setName(String name) { this.name = name; }
}
Main.java Main.java
public MovieDb getMovie(int movieId) {
Session s = HibernateUtil.getSessionFactory().openSession();
Transaction tx = s.beginTransaction();
MovieDb movie = null;
try {
String hql = "FROM MovieDb E WHERE E.movieId = " + movieId;
Query query = s.createQuery(hql);
List movies = query.list();
movie = (MovieDb)movies.get(0);
tx.commit();
}catch (HibernateException ex) {
if (tx != null)
tx.rollback();
}finally {
s.close();
}
return movie;
}
public void saveMovie(MovieDb movie) {
Session s = HibernateUtil.getSessionFactory().openSession();
Transaction tx = s.beginTransaction();
try {
s.save(movie);
s.flush();
tx.commit();
} catch (HibernateException ex) {
if (tx != null)
tx.rollback();
} finally {
s.close();
}
}
My database is setup like this: 我的数据库设置如下:
movies 电影
id int (primary)
movieId int
title varchar
moviesGenre 电影类型
id int (primary)
movieId int
name varchar
My Hibernate.cfg.xml file showing mapping is below: 我的Hibernate.cfg.xml文件显示映射如下:
Hibernate.cfg.xml Hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
...
...
...
<mapping class="com.medialibrary.api.model.MovieDb"/>
<mapping class="com.medialibrary.api.model.Genre"/>
</session-factory>
</hibernate-configuration>
on the MovieDb class side 在MovieDb类方面
@OneToMany(mappedBy="movie", cascade = CascadeType.ALL)
private List<Genre> genres;
and in Genre 并且在类型上
@ManyToOne
@JoinColumn(name="movieId")
private MovieDb movie;
Bugs in the code: 代码中的错误:
when you are saving parent entity then them children are not persisted as long as you do not use CascadeType.PERSIST or CascadeType.ALL (for all the operation types) 保存父实体时,只要不使用CascadeType.PERSIST或CascadeType.ALL(对于所有操作类型),它们的子对象就不会保留
you are trying to do a bidirectional relation so you have to specify the owner mappedBy="movie" will do this job - it says that a moveId will be written on the Genre side 您正在尝试建立双向关系,因此必须指定所有者maptedBy =“ movie”将完成此工作-它说moveId将写在流派一侧
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.