我有一个用户和一个电影模型: 用户:
@Entity(name = "User")
@Table(name = "USER")
public class User {
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_seq")
@SequenceGenerator(name = "user_seq", sequenceName = "user_seq", allocationSize = 1)
private Long id;
@Column(name = "USERNAME", length = 50, unique = true)
@NotNull
@Size(min = 4, max = 50)
private String username;
@Column(name = "PASSWORD", length = 100)
@NotNull
@Size(min = 4, max = 100)
private String password;
@Column(name = "FIRSTNAME", length = 50)
@NotNull
@Size(min = 4, max = 50)
private String firstname;
@Column(name = "LASTNAME", length = 50)
@NotNull
@Size(min = 4, max = 50)
private String lastname;
@Column(name = "EMAIL", length = 50)
@NotNull
@Size(min = 4, max = 50)
private String email;
@Column(name = "ENABLED")
@NotNull
private Boolean enabled;
@Column(name = "LASTPASSWORDRESETDATE")
@Temporal(TemporalType.TIMESTAMP)
@NotNull
private Date lastPasswordResetDate;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "USER_AUTHORITY",
joinColumns = {@JoinColumn(name = "USER_ID", referencedColumnName = "ID")},
inverseJoinColumns = {@JoinColumn(name = "AUTHORITY_ID", referencedColumnName = "ID")})
private List<Authority> authorities;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Boolean getEnabled() {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
public List<Authority> getAuthorities() {
return authorities;
}
public void setAuthorities(List<Authority> authorities) {
this.authorities = authorities;
}
public Date getLastPasswordResetDate() {
return lastPasswordResetDate;
}
public void setLastPasswordResetDate(Date lastPasswordResetDate) {
this.lastPasswordResetDate = lastPasswordResetDate;
}
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "user_movie",
joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "movie_id", referencedColumnName = "id")
)
private Set<Movie> movies;
public Set<Movie> getMovies() {
return movies;
}
public void setMovies(Set<Movie> movies) {
this.movies = movies;
}
}
电影:
@Entity(name = "Movie")
@Table(name = "movie")
public class Movie {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
public Movie(){}
public Movie(Integer id, String name ) {
this.id = id;
this.name = name;
}
@ManyToMany(mappedBy = "movies")
private Set<User> users;
public Set<User> getUsers() {
return users;
}
public void addUser(User user){
System.out.println("ADD MOVIE: " + user);
users.add(user);
user.getMovies().add(this);
}
public void setUsers(Set<User> users) {
this.users = users;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString(){
return "id: " + id + "name: " + name;
}
}
我在这些模型之间建立了多对多的关系。如果我是正确的,那么用户就是关系的所有者。
在我的MovieController.java中,我有:
@RequestMapping(value = "/", method = RequestMethod.POST)
public Movie createMovie(@RequestBody Movie movie){
return movieService.createMovie(movie);
}
这会调用MovieService.java:
@Override
public Movie createMovie(Movie movie) {
return movieRepository.save(movie);
}
这会调用MovieRepository.java:
@Repository
public interface MovieRepository extends CrudRepository<Movie, Serializable> {}
当我从前端调用post方法时,电影记录保存在我的影片表中,但是在user_movie表中没有创建记录。由于我在用户和电影之间建立了多对多的关系,Hibernate不会隐含这样做吗?
答案 0 :(得分:0)
您保存电影,并且为了保存用户,必须在电影中设置级联。否则你可以在用户中保留级联并保存他。
您需要将级联放到您调用save的实体上以进行级联。
public interface repository extends JpaRepository<User, Long> {
List<User> findByUserId(String userId)
}
在保存之前,不要忘记将用户添加到电影,反之亦然。
与所有双向关系一样,对象模型和应用程序负责维护双向关系。 JPA中没有任何魔力,如果您在集合的一侧添加或删除,您还必须在另一侧添加或删除,请参阅对象损坏。从技术上讲,如果您只是从关系的拥有方添加/删除数据库,那么数据库将会正确更新,但随后您的对象模型将不同步,这可能会导致问题。
见这里:https://en.wikibooks.org/wiki/Java_Persistence/ManyToMany
答案 1 :(得分:0)
对于第一个视图,您的代码是正确的 问题可能在 GenerationType.SEQUENCE (尝试将GenerationType.AUTO用于用户的ID),或者您需要将 @Transactional 添加到您的控制器。