【发布时间】:2016-05-08 20:52:36
【问题描述】:
我正在从事具有以下三个实体的休眠项目,当我在实体之间添加单对多关系时,获取字段'user_id'没有默认值。
当尝试插入新主题时,我得到了
Hibernate:插入主题(内容、日期、图像、图像名称)值(?、?、?、?) 2016 年 5 月 8 日晚上 11:46:56 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>
【问题讨论】: