0
私はいくつかの値をMySQLテーブルにハイバネートで挿入しようとしています。私は、MySQLサーバに私のテーブルを参照することができHibernateはMySQLの "nullable = false"で問題を作ります
@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;
}
を::
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ファイルに私が値を追加しようとしている:私は、次の表を使用しています
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
テーブルです:
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)
)#
任意の提案?
テーブルが一致しません。 DB2では、 'address'カラムは' nullable'であり、MySQLではそうではありません。したがって、テーブル(またはテーブルがすでに存在している)に対して異なるcreate文を使用しています。つまり、MySQLのテーブル定義はDB2とは異なります。 –
dbでデフォルト値を設定しようとしましたか? – AnarchoEnte
私が直面している問題は、「アドレス」列が非ヌルに更新されていることです。 EARファイルをデプロイした後。 – user2986042