簡體   English   中英

休眠字段“ user_id”沒有默認值

[英]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>

可能您沒有設置試圖插入主題表的主題用戶。

還有一件事,使用單數的類名。 主題->主題

我在這里看到許多問題:

  • 您確定主題在所有方面都不是ManyToMany而不是OneToMany(似乎是無效的東西)...(一個人可能對許多主題感興趣,主題可以包含許多人)
  • 如果您想與新用戶繼續使用新主題,並且您應該使用@OneToMany( 層疊 = {CascadeType.PERSIST,CascadeType.MERGE})如果您在字段中正確設置,則應該可以使用,否則,您可能會遭受循環引用...
  • 我寧願使用GenerationType.SEQUENCE ,因為AUTO就像一個黑盒子。 您不會確切知道發生了什么...(例如,oracle將創建一個序列以提供唯一的主鍵)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM