user2986042
user2986042

Reputation: 1270

Hibernate makes problems on " nullable = false" in MySQL

I am trying to insert some of the values in MySQL table with hibernate . I am using following table :

create table test_settings (
  id bigint not null,
  address varchar(64),
  enabled smallint not null,
  max_count integer,
  storage_period integer,
  rocommunity varchar(128) not null,
  skip_unknown smallint not null,
  storage_type integer not null,
  primary key (id)
);

and in java file i am trying to add values :

@Entity
@Table(name = "test_settings ")
public class TestSettings extends Singleton {
@Column(name = "address", nullable = false, length = 64)
    String address;

    @Column(name = "storage_type", nullable = false)
    @Enumerated(EnumType.ORDINAL)
    StorageType storageType = StorageType.MEMORY;

    @Column(name = "max_count")
    Integer maxCountInMemory;

    @Column(name = "storage_period")
    Integer storagePeriod;

    @Column(name = "skip_unknown", nullable = false)
    boolean skipUnknown = true;

    @Column(name = "rocommunity", length = 128, nullable = false)
    String rocommunity = "public";

    @Column(name = "enabled", nullable = false)
    boolean enabled = false;
}

I can see my table in MySQL server :

mysql> describe test_settings;
+---------------------+--------------+------+-----+---------+-------+
| Field               | Type         | Null | Key | Default | Extra |
+---------------------+--------------+------+-----+---------+-------+
| id                  | bigint(20)   | NO   | PRI | NULL    |       |
| address             | varchar(64)  | NO   |     | NULL    |       |
| enabled             | bit(1)       | NO   |     | NULL    |       |
| max_count           | int(11)      | YES  |     | NULL    |       |
| rocommunity         | varchar(128) | NO   |     | NULL    |       |
| skip_unknown        | bit(1)       | NO   |     | NULL    |       |
| storage_period      | int(11)      | YES  |     | NULL    |       |
| storage_type        | int(11)      | NO   |     | NULL    |       |
+---------------------+--------------+------+-----+---------+-------+
8 rows in set (0.00 sec)

But i am getting some exception problem while deploying . These are the complete stack trace :

Caused by: org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; SQL [insert into mam_db.snmp_settings (address, enabled, max_count_in_memory, rocommunity, skip_unknown, storage_period, storage_type, id) values (?, ?, ?, ?, ?, ?, ?, ?)]; constraint [null];


10:37:41,165 INFO  [stdout] (ServerService Thread Pool -- 116) Caused by: java.sql.BatchUpdateException: Column 'address' cannot be null
10:37:41,165 INFO  [stdout] (ServerService Thread Pool -- 116)  at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1666)
10:37:41,165 INFO  [stdout] (ServerService Thread Pool -- 116)  at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1082)
10:37:41,165 INFO  [stdout] (ServerService Thread Pool -- 116)  at org.jboss.jca.adapters.jdbc.WrappedStatement.executeBatch(WrappedStatement.java:1077)
10:37:41,166 INFO  [stdout] (ServerService Thread Pool -- 116)  at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
10:37:41,166 INFO  [stdout] (ServerService Thread Pool -- 116)  at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
10:37:41,166 INFO  [stdout] (ServerService Thread Pool -- 116)  ... 58 more

But i am using nullable = false in java code . But this is working fine with PostgreSQL and DB2 . Getting exception on MySQL only .

This is the corresponding DB2 table :

CREATE TABLE TEST_SETTINGS (
  ID BIGINT NOT NULL,
  ADDRESS VARCHAR(64),
  ENABLED SMALLINT NOT NULL,
  MAX_COUNT INTEGER,
  STORAGE_PERIOD INTEGER,
  ROCOMMUNITY VARCHAR(128) NOT NULL,
  SKIP_UNKNOWN SMALLINT NOT NULL,
  STORAGE_TYPE INTEGER NOT NULL,
  PRIMARY KEY (ID)
)#

Any suggestion ?

Upvotes: 0

Views: 591

Answers (1)

Dumbo
Dumbo

Reputation: 1827

@Column(nullable = false) is the JPA way to declare a column to be not-null. If you want that it will be able to insert null address do not use this annotation.

Upvotes: 1

Related Questions