我认为您的注释映射不好。看看我是怎么做到的。
角色 ID
import java.io.Serializable;
import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Embeddable
public class RoleId implements Serializable {
private static final long serialVersionUID = 1L;
@ManyToOne(optional = false)
@JoinColumn(name = "movie")
private Movie movie;
@ManyToOne(optional = false)
@JoinColumn(name = "actor")
private Actor actor;
private String role;
public Movie getMovie() {
return movie;
}
public void setMovie(Movie movie) {
this.movie = movie;
}
public Actor getActor() {
return actor;
}
public void setActor(Actor actor) {
this.actor = actor;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
@Override
public String toString() {
return "movie=" + movie.getTitle() + ", actor=" + actor.getName()
+ ", role=" + role;
}
}
角色
@Entity
@Table(name = "role")
public class Role implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private RoleId id;
public RoleId getId() {
return id;
}
public void setId(RoleId id) {
this.id = id;
}
@Override
public String toString() {
return getId().toString();
}
}
演员
@Entity
@Table(name = "actor")
public class Actor implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "id")
private int id;
private String name;
@OneToMany
@JoinColumn(name = "actor")
private List<Role> roles;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder(124);
builder.append(name).append(" roles ");
builder.append(Arrays.toString(roles.toArray()));
return builder.toString();
}
}
电影
@Entity
@Table(name = "movie")
public class Movie implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "id")
private int id;
private String title;
@OneToMany
@JoinColumn(name = "movie")
private List<Role> roles;
public Role createRole(Actor actor, String role) {
Role myRole = new Role();
RoleId roleId = new RoleId();
roleId.setMovie(this);
roleId.setRole(role);
roleId.setActor(actor);
myRole.setId(roleId);
return myRole;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder(124);
builder.append(title).append(" roles ");
builder.append(Arrays.toString(roles.toArray()));
return builder.toString();
}
}
MySQL 表
CREATE TABLE `actor` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
CREATE TABLE `movie` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
CREATE TABLE `role` (
`movie` bigint(20) NOT NULL,
`actor` bigint(20) NOT NULL,
`role` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`movie`,`actor`,`role`),
CONSTRAINT `role_movie_movie` FOREIGN KEY (`movie`) REFERENCES `movie` (`id`),
CONSTRAINT `role_actor_actor` FOREIGN KEY (`actor`) REFERENCES `actor` (`id`)
) ENGINE=InnoDB;
INSERT INTO actor(name) VALUES('Arnold Schwarzenegger');
INSERT INTO actor(name) VALUES('Michael Biehn');
INSERT INTO actor(name) VALUES('Linda Hamilton');
INSERT INTO movie(title) VALUES('Termiantor');
INSERT INTO role VALUES(1, 1, 'Terminator');
INSERT INTO role VALUES(1, 2, 'Kyle Reese');
INSERT INTO role VALUES(1, 3, 'Sara Connor');
执行以下源代码后:
public static void main(String[] args) {
movieTest();
}
public static void movieTest() {
Session session = HibernateUtil.getSessionFactory().openSession();
showEntities(session, Movie.class, "Movies");
showEntities(session, Actor.class, "Actors");
showEntities(session, Role.class, "Roles");
}
private static void showEntities(Session session, Class<?> entity,
String name) {
System.out.println(name);
for (Object item : session.createCriteria(entity).list()) {
System.out.println(item);
}
}
你会看到这样的输出
Movies
Termiantor roles [movie=Termiantor, actor=Arnold Schwarzenegger, role=Terminator, movie=Termiantor, actor=Michael Biehn, role=Kyle Reese, movie=Termiantor, actor=Linda Hamilton, role=Sara Connor]
Actors
Arnold Schwarzenegger roles [movie=Termiantor, actor=Arnold Schwarzenegger, role=Terminator]
Michael Biehn roles [movie=Termiantor, actor=Michael Biehn, role=Kyle Reese]
Linda Hamilton roles [movie=Termiantor, actor=Linda Hamilton, role=Sara Connor]
Roles
movie=Termiantor, actor=Arnold Schwarzenegger, role=Terminator
movie=Termiantor, actor=Michael Biehn, role=Kyle Reese
movie=Termiantor, actor=Linda Hamilton, role=Sara Connor