Hibernate中的@ManyToOne和@OneToMany导致默认值错误

时间:2019-05-16 13:13:25

标签: java hibernate spring-boot

我试图在Hibernate / Spring Boot中使用带有@OneToMany和@ManyToOne批注的外键,但是我一直收到此错误:

java.sql.SQLException: Field 'note_id' doesn't have a default value

我的代码:

Note.java

@Entity
@Table(name = "notes")
@EntityListeners(AuditingEntityListener.class)
public class Note {

public Note() {
}

public Note(String name, String content) {
    this.name = name;
    this.content = content;
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "note_id")
private Integer id;

@ManyToOne
@JoinColumn(name = "user_id")
private User user;

@NotBlank
private String name;

@NotBlank
@Column(columnDefinition="LONGTEXT")
private String content;

User.java

@Entity
@Table(name  = "user")
@EntityListeners(AuditingEntityListener.class)
public class User implements Serializable {

public User() {
}

public User(String username, String password) {
    this.username = username;
    this.password = password;
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
private Integer id;

@OneToMany(mappedBy = "user", cascade = ALL)
private Set<Note> notes;

@NotEmpty
@Column(nullable = false, unique = true)
private String username;

@NotEmpty
private String password;

每当我尝试创建便笺时都会发生错误。 预先感谢!

0 个答案:

没有答案