![](/img/trans.png)
[英]SpringBoot ManyToOne saving to database issue - getting error: Field 'user_id' doesn't have a default value
[英]hibernate Field 'user_id' doesn't have a default value
当我在具有以下三个实体的休眠项目中工作时,当我在实体之间添加一个原子关系时,字段“ user_id”没有默认值。
当尝试插入新主题时,我得到
休眠状态:插入主题(内容,日期,图像,图像名称)值(?,?,?,?)2016年5月8日11:46:56 PM org.hibernate.util.JDBCExceptionReporter logExceptions警告:SQL错误:1364,SQLState :HY000 2016年5月8日,下午11:46:56 org.hibernate.util.JDBCExceptionReporter logExceptions严重:字段'user_id'没有默认值
并尝试插入新的类似记录时得到相同的错误
用户类别
public class User implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id", unique = true, nullable = false)
private long id;
@Column(name = "first_name", nullable = false, length = 10)
private String firstName;
@Column(name = "last_name", nullable = false, length = 10)
private String lastName;
@Column(name = "role", nullable = false, length = 10)
private String role;
@Column(name = "email", unique = true, nullable = false, length = 10)
private String email;
@Column(name = "password", nullable = false, length = 10)
private String password;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
private Set<Topics> topics=new HashSet<Topics>();
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user1")
private Set<Like> likes=new HashSet<Like>();
//getter and setter
喜欢上课
@Entity
@Table(name = "likes")
public class Like implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "likes_id", unique = true, nullable = false)
private long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false,insertable = false, updatable = false)
private User user1;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "topics_id", nullable = false,insertable = false, updatable = false)
private Topics topics;
//getter and setter
主题课
@Entity
@Table(name = "topics")
public class Topics implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "topics_id", unique = true, nullable = false)
private long id;
@Column(name = "content", unique = true, nullable = false)
private String content;
@Temporal(TemporalType.DATE)
@Column(name = "date")
private Date date;
@Column(name = "image_name")
private String imageName;
@Lob
@Column(name="image", columnDefinition="longblob")
private byte[] image;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false,insertable = false, updatable = false)
private User user;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "topics")
private Set<Like> likes=new HashSet<Like>();
//getter and setter
休眠配置文件
<hibernate-configuration>
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/social</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="com.pro.model.User"></mapping>
<mapping class="com.pro.model.Topics"></mapping>
<mapping class="com.pro.model.Like"></mapping>
</session-factory>
可能您没有设置试图插入主题表的主题用户。
还有一件事,使用单数的类名。 主题->主题
我在这里看到许多问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.