我试图用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
表:
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)
)#
有什么建议吗?
答案 0 :(得分:1)
@Column(nullable = false)
是JPA声明列不为null的方法。如果您希望它能够插入null
地址,请不要使用此注释。