Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate "Column --- cannot be null

I'm using Hibernate in combination with phpmyadmin (MySQL). Recently I found a really weird error. Whenever I try to insert a new row (.persist) I get the following error:

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'user_id' cannot be null

The error is really obvious. But the weird part is, this error came out of nowhere. I'm ENTIRELY sure the property userId (mapped to 'user_id') isn't null. I tested several times. This is the mapping part: (On the @manytoone part).

  <property name="userId" update="false" insert="false" column="user_id" type="java.lang.Long" />

The OneToMany part isn't the problem I guess.

So the problem here is, i'm 100% sure the value isn't null, still Hibernate passes it as null to the MySQL. (Getter does work).

UPDATE

@ManyToOne side, which causes the error

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="xx.xx.xx.AlarmEntity" table="alarms">
        <meta attribute="class-description">
            Hierin staan de object properties van de alarm entiteit.
        </meta>
        <id name="id" type="java.lang.Long" column="id">
            <generator class="native"/>
        </id>
        <property name="keepsRunning"  type="java.lang.Boolean">
            <column name="keeps_running" sql-type="int"></column>
        </property>
        <property name="userId" update="false" insert="false" column="user_id" type="java.lang.Long" />
        <many-to-one name="userAlarm" cascade="save-update" fetch="select" column="user_id" class="xx.xx.xx.UserEntity" />
    </class>
</hibernate-mapping>
like image 683
iLuvCode Avatar asked Jun 08 '17 20:06

iLuvCode


People also ask

How Hibernate handle null values?

The best way to avoid Hibernate's attempts at setting null values to primitives is to use Wrapper classes (Integer, Long, Double...); and especially, if you need to tack on a column or 2 to an existing table. Auto-boxing is your friend.

What is the use of @column annotation?

@Column annotation is used for Adding the column the name in the table of a particular MySQL database.

What is nullable false in Hibernate?

The @Column(nullable = false) Annotation It's used mainly in the DDL schema metadata generation. This means that if we let Hibernate generate the database schema automatically, it applies the not null constraint to the particular database column.

What is nullable true in Hibernate?

@Column Annotation can have property "nullable" which can be set to true or false. This allows Hibernate to see whether nulls are to be stored or not.


1 Answers

Please check the mapping

UserEntity

private int user_id;
private Set<AlarmEntity> alarm;

AlarmEntity

private int alarm_id;
private String keepsRunning;

As per your requirement , I have provided mapping with annotation please check

User

@Id
@Column(name = "userId")
private Long userId;

// parent to Alarm
@Fetch(value = FetchMode.SELECT)
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "userId")
@JsonIgnore 
private List<Alarm> alarmList ;

Alarm

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "alarmId")
private Long alarmId;


@ManyToOne
@JoinColumn(name = "userId", insertable = true, updatable = true, nullable = true)
private User user;

@Column(name="keepsRunning")
private String keepsRunning;
like image 188
Hema Avatar answered Sep 30 '22 16:09

Hema