【问题标题】:hibernate Field 'user_id' doesn't have a default value休眠字段'user_id'没有默认值
【发布时间】: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>

【问题讨论】:

    标签: java mysql hibernate


    【解决方案1】:

    可能,您没有设置您尝试插入主题表的主题用户。

    还有一件事,使用单数类名。 主题 -> 主题

    【讨论】:

      【解决方案2】:

      我在这里看到了很多问题:

      • 你确定 topics 不是 ManyToMany 而不是 OneToMany(似乎不是有效的东西)...(一个人可以对多个主题感兴趣,主题可以包含很多人)
      • 如果您想与新用户保持新主题,并且您应该使用 @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }) 如果您在字段中正确设置,则应该可以工作,否则,您可能会遭受循环引用的困扰...
      • 我宁愿使用 GenerationType.SEQUENCE,因为 AUTO 就像一个黑盒。你不会确切知道发生了什么......(例如,oracle 将创建一个序列来提供唯一的主键)

      【讨论】:

        猜你喜欢
        • 2018-07-31
        • 2010-10-22
        • 1970-01-01
        • 2018-07-14
        • 2023-01-03
        • 2020-03-08
        • 2013-07-26
        • 1970-01-01
        相关资源
        最近更新 更多