0
votes

I working on hibernate project that have the following three entity,when I add onetomany relationship between the entities get Field 'user_id' doesn't have a default value .

when try to insert new topics I get

Hibernate: insert into topics (content, date, image, image_name) values (?, ?, ?, ?) May 08, 2016 11:46:56 PM org.hibernate.util.JDBCExceptionReporter logExceptions WARNING: SQL Error: 1364, SQLState: HY000 May 08, 2016 11:46:56 PM org.hibernate.util.JDBCExceptionReporter logExceptions SEVERE: Field 'user_id' doesn't have a default value

and get same error when try to insert new like record

user class

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

like class

@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

topics class

 @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 file

 <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>

2

2 Answers

1
votes

Probably, You didn't set the User of Topics that you attempted to insert topics table.

One more thing, use singular class names. Topics -> Topic

0
votes

I see numerous problems here:

  • Are you sure that topics are not ManyToMany rather than OneToMany at all side (seems like not valid stuff)... (one people can interested in many topics, topics can contain many people)
  • If you want to persist new topic with new user and like you should use @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }) should work if you setup correctly in the fields, if you not, you may will be suffer from circular references...
  • and I would rather use GenerationType.SEQUENCE, because AUTO is like a blackbox. You will not know exactly what is happening... (e.g. oracle will create a sequence to provide unique primary key)