Java Hibernate在MySQL中对“nullable=false”产生问题

Java Hibernate在MySQL中对“nullable=false”产生问题,java,mysql,hibernate,Java,Mysql,Hibernate,我试图用hibernate在MySQL表中插入一些值。我使用下表: 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 n

我试图用hibernate在MySQL表中插入一些值。我使用下表:

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)
);
在java文件中,我尝试添加值:

@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;
}
我可以在MySQL服务器中看到我的表:

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)
但是我在部署时遇到一些异常问题。以下是完整的堆栈跟踪:

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
但是我在java代码中使用了nullable=false。但这在PostgreSQL和DB2中运行良好。仅在MySQL上获取异常

这是对应的DB2表:


有什么建议吗

@Columnnullable=false是JPA将列声明为非null的方法。如果希望它能够插入空地址,请不要使用此批注。

表不匹配。在DB2中,地址列是可空的,而在MySQL中则不是。因此,您正在为表或已存在的表使用不同的create语句。因此,简而言之,MySQL上的表定义与DB2中的不同。您是否也尝试在db中设置默认值?我面临的问题是“address”列的update值不为null。在部署EAR文件之后。
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)
)#