【发布时间】:2018-02-02 11:49:09
【问题描述】:
我想将评论表和电影表与用户表相关联。我希望允许一个用户有很多 cmets,而一个电影有很多 cmets。然后,我想在每部电影的详细信息页面中显示 cmets 列表,为创建评论的用户提供删除或更新评论的选项。
我更改了我的代码,试图在评论和电影之间创建一对多的关系,但我得到了错误:
原因:org.h2.jdbc.JdbcSQLException: NULL not allowed for column "FILM_ID"; SQL语句:alter table film add column film_id bigint 不为空 [23502-196]
这让我想到两件事:
1) 设置为允许 null 或找出为什么会有 null 字段。我尝试通过添加 @Column(name = "film_id", nullable = true) 来允许 null,但它说参数是多余的。
2) 电影表已经有自动递增的 ID,所以通过添加 @Column(name = "film_id") 我是在复制 ID 吗?与“添加列”的错误消息一样,它让我这么认为?
我目前的尝试是:
电影.java
package com.demo.spring.domain;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.*;
import java.util.Date;
import java.util.List;
@Entity
public class Film {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "film_id", nullable = true)
Long id;
String title;
String director;
String description;
@DateTimeFormat(pattern="yyyy-MM-dd")
Date date;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "film_id", referencedColumnName = "film_id")
List<Comment> comments;
public List<Comment> getComments() {
return comments;
}
public void setComments(List<Comment> comments) {
this.comments = comments;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
//rest of getter and setters below//
Comment.java
package com.demo.spring.domain;
import javax.persistence.*;
@Entity
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "comment_id")
Long id;
String body;
@Column(name = "film_id")
Long filmId;
public Long getFilmId() {
return filmId;
}
public void setFilmId(Long filmId) {
this.filmId = filmId;
}
public Comment(){
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
更新
我已经更改了 Film.java..
发件人:
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "film_id", referencedColumnName = "film_id")
List<Comment> comments;
收件人:
@OneToMany(cascade = CascadeType.ALL)
List<Comment> comments;
如果我在 Comment.java 中添加:
@OneToMany(cascade=CascadeType.ALL)
@JoinTable(name="film", joinColumns=@JoinColumn(name = "film_id_fk", referencedColumnName = "film_id"))
private Set<Comment> comment = new HashSet<Comment>();
Film film;
我明白了:
MappingException: 外键 (FK5vk85sy54a8be115ye9ra1lyu:film_cmets [film_film_id])) 必须有 与引用的主键相同的列数(电影 [film_id_fk,comment_comment_id])
如果我将private Set<Comment> comment = new HashSet<Comment>(); 更改为List<Comment> comments = new ArrayList<Comment>();,我会得到:
“FILM_ID”列不允许为 NULL; SQL语句:alter table film 添加列film_id bigint 不为空
如果我改为添加:
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name = "film_id_fk", referencedColumnName = "film_id")
private Set<Comment> comment = new HashSet<Comment>();
Film film;
我明白了:
MappingException:无法确定类型: com.demo.spring.domain.Film,表:注释,列: [org.hibernate.mapping.Column(film)]
如果我将private Set<Comment> comment = new HashSet<Comment>(); 更改为List<Comment> comments = new ArrayList<Comment>();,我会得到:
“FILM_ID”列不允许为 NULL; SQL语句:alter table film 添加列film_id bigint 不为空
【问题讨论】: