在Film.java中
package com.flp.ems.domain;
import java.util.Date;
import java.util.Set;
import javax.persistence.*;
@Entity
public class Film
{
@Id private int film_id;
private String title;
private String description;
private Date release_year;
@ManyToOne
@JoinColumn(name="language_id")
private Language language;
private float rental_duration;
private float rental_rate;
private int length;
private int replacement_cost;
private String rating;
private String special_features;
private Date last_update;
@ManyToMany
@JoinTable(name = "film_actor", joinColumns = @JoinColumn(name = "film_id", referencedColumnName = "film_id") , inverseJoinColumns = @JoinColumn(name = "actor_id", referencedColumnName = "actor_id"))
private Set<Actor> actor;
@ManyToOne
private Category category;
public Film()
{
}
public int getFilm_id() {
return film_id;
}
public void setFilm_id(int film_id) {
this.film_id = film_id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getRelease_year() {
return release_year;
}
public void setRelease_year(Date release_year) {
this.release_year = release_year;
}
public Language getLanguage() {
return language;
}
public void setLanguage(Language language) {
this.language = language;
}
public float getRental_duration() {
return rental_duration;
}
public void setRental_duration(float rental_duration) {
this.rental_duration = rental_duration;
}
public float getRental_rate() {
return rental_rate;
}
public void setRental_rate(float rental_rate) {
this.rental_rate = rental_rate;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getReplacement_cost() {
return replacement_cost;
}
public void setReplacement_cost(int replacement_cost) {
this.replacement_cost = replacement_cost;
}
public String getRating() {
return rating;
}
public void setRating(String rating) {
this.rating = rating;
}
public String getSpecial_features() {
return special_features;
}
public void setSpecial_features(String special_features) {
this.special_features = special_features;
}
public Date getLast_update() {
return last_update;
}
public void setLast_update(Date last_update) {
this.last_update = last_update;
}
public Set<Actor> getActor() {
return actor;
}
public void setActor(Set<Actor> actor) {
this.actor = actor;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
@Override
public String toString() {
return "Film [film_id=" + film_id + ", title=" + title + ", description=" + description + ", release_year="
+ release_year + ", language=" + language + ", rental_duration=" + rental_duration + ", rental_rate="
+ rental_rate + ", length=" + length + ", replacement_cost=" + replacement_cost + ", rating=" + rating
+ ", special_features=" + special_features + ", last_update=" + last_update + ", actor=" + actor
+ ", category=" + category + "]";
}
}
在Actor.java中包com.flp.ems.domain;
import java.util.Date;
import java.util.Set;
import javax.persistence.*;
@Entity
public class Actor
{
@Id private int actor_id;
private String first_name;
private String last_name;
private Date last_update;
@ManyToMany(mappedBy="actor")
private Set<Film> films;
public Actor()
{
}
public int getActor_id() {
return actor_id;
}
public void setActor_id(int actor_id) {
this.actor_id = actor_id;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public Date getLast_update() {
return last_update;
}
public void setLast_update(Date date) {
this.last_update = date;
}
public Set<Film> getFilms() {
return films;
}
public void setFilms(Set<Film> films) {
this.films = films;
}
@Override
public String toString() {
return "Actor [actor_id=" + actor_id + ", first_name=" + first_name + ", last_name=" + last_name
+ ", last_update=" + last_update + ", films=" + films + "]";
}
}
它应该加入电影表和演员表,它应该创建一个film_actor表。
我的问题是当我将数据插入电影表和actor表时数据没有自动插入到film_actor表中为什么?
答案 0 :(得分:0)
根据我的理解,您需要为“film_actor”创建模型,以下链接可以帮助您。检查此http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/