我正在研究具有以下三个实体的hibernate项目,当我在实体之间添加onetomany关系时得到Field' user_id'没有默认值。
当我尝试插入新主题时
Hibernate:插入主题(content,date,image,image_name)值(?,?,?,?) 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配置文件
<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>
答案 0 :(得分:1)
可能您没有设置试图插入主题表的主题用户。
还有一件事,就是使用单数类名。 主题 - &gt;主题
答案 1 :(得分:0)
我在这里看到很多问题: