I have two identical tables (h2 database). Same schema, same DDL, same all, except for different table names. When I try to map the same entity bean to one of them, it works; to the other - it fails, saying "NULL not allowed for column ID".
Here are the two (identical, I think) DDLs:
Good:
Code:
CREATE TABLE AAA.CONTAINER_USE (
ID BIGINT DEFAULT (NEXT VALUE FOR AAA.SYSTEM_SEQUENCE_3F597044_4263_4BCE_B384_6F46C150DA0F) NOT NULL,
BATCH VARCHAR(6),
CONTAINER VARCHAR(16) NOT NULL,
GROSS_WEIGHT DOUBLE,
ICE_WEIGHT DOUBLE,
NET_WEIGHT DOUBLE,
PRODUCT VARCHAR(15),
FROM_TIME TIMESTAMP,
TO_TIME TIMESTAMP,
STATION VARCHAR(25),
EMPLOYEE VARCHAR(16),
COMMENT VARCHAR(300),
SOURCE VARCHAR(50),
PRIMARY KEY (ID)
);
ALTER TABLE AAA.CONTAINER_USE
ADD FOREIGN KEY (BATCH)
REFERENCES TEST.AAA.BATCH (ID);
ALTER TABLE AAA.CONTAINER_USE
ADD FOREIGN KEY (CONTAINER)
REFERENCES TEST.AAA.CONTAINER (ID);
ALTER TABLE AAA.CONTAINER_USE
ADD FOREIGN KEY (PRODUCT)
REFERENCES TEST.AAA.PRODUCT (ID);
ALTER TABLE AAA.CONTAINER_USE
ADD FOREIGN KEY (STATION)
REFERENCES TEST.AAA.STATION (ID);
ALTER TABLE AAA.CONTAINER_USE
ADD FOREIGN KEY (EMPLOYEE)
REFERENCES TEST.BBB.EMPLOYEE (ID);
Bad:
Code:
CREATE TABLE AAA.COPY_OF_CONTAINER_USE (
ID BIGINT DEFAULT (NEXT VALUE FOR AAA.SYSTEM_SEQUENCE_3F597044_4263_4BCE_B384_6F46C150DA0F) NOT NULL,
BATCH VARCHAR(6),
CONTAINER VARCHAR(16) NOT NULL,
GROSS_WEIGHT DOUBLE,
ICE_WEIGHT DOUBLE,
NET_WEIGHT DOUBLE,
PRODUCT VARCHAR(15),
FROM_TIME TIMESTAMP,
TO_TIME TIMESTAMP,
STATION VARCHAR(25),
EMPLOYEE VARCHAR(16),
COMMENT VARCHAR(300),
SOURCE VARCHAR(50),
PRIMARY KEY (ID)
);
ALTER TABLE AAA.COPY_OF_CONTAINER_USE
ADD FOREIGN KEY (BATCH)
REFERENCES TEST.AAA.BATCH (ID);
ALTER TABLE AAA.COPY_OF_CONTAINER_USE
ADD FOREIGN KEY (CONTAINER)
REFERENCES TEST.AAA.CONTAINER (ID);
ALTER TABLE AAA.COPY_OF_CONTAINER_USE
ADD FOREIGN KEY (PRODUCT)
REFERENCES TEST.AAA.PRODUCT (ID);
ALTER TABLE AAA.COPY_OF_CONTAINER_USE
ADD FOREIGN KEY (STATION)
REFERENCES TEST.AAA.STATION (ID);
ALTER TABLE AAA.COPY_OF_CONTAINER_USE
ADD FOREIGN KEY (EMPLOYEE)
REFERENCES TEST.BBB.EMPLOYEE (ID);
Here is the entity bean:
Code:
@Entity
// This will result in an exception; however, remove "COPY_OF_" below, and it will work
@Table(name = "COPY_OF_CONTAINER_USE", catalog = "TEST")
public class ContainerUse extends Base
{
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(unique=true)
//----------------------------------------------------------------------------------
public int getId()
{
return id;
}
//----------------------------------------------------------------------------------
public void setId(int id)
{
this.id = id;
}
@Column (name="FROM_TIME")
//----------------------------------------------------------------------------------
public Date getFromTime()
{
return fromTime;
}
//----------------------------------------------------------------------------------
public void setFromTime(Date date)
{
this.fromTime = date;
}
.........................
From looking at the logs, this line appears at the successful scenario (first table) and does not appear at the bad one (2nd table):
Code:
2009-10-14 13:38:31,115 DEBUG [AWT-EventQueue-0] IdentifierGeneratorFactory - Natively generated identity: 705
Here is the "good" log:
Code:
2009-10-14 15:51:56,444 DEBUG [AWT-EventQueue-0] DefaultSaveOrUpdateEventListener - saving transient instance
2009-10-14 15:51:56,470 DEBUG [AWT-EventQueue-0] AbstractSaveEventListener - saving [aaa.model.ContainerUse#<null>]
2009-10-14 15:51:56,487 DEBUG [AWT-EventQueue-0] AbstractSaveEventListener - executing insertions
2009-10-14 15:51:56,601 DEBUG [AWT-EventQueue-0] AbstractSaveEventListener - executing identity-insert immediately
2009-10-14 15:51:56,625 DEBUG [AWT-EventQueue-0] AbstractEntityPersister - Inserting entity: aaa.model.ContainerUse (native id)
2009-10-14 15:51:56,648 DEBUG [AWT-EventQueue-0] AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
2009-10-14 15:51:56,663 DEBUG [AWT-EventQueue-0] AbstractBatcher - insert into TEST.AAA.CONTAINER_USE (id, BATCH, comment, CONTAINER, employee, FROM_TIME, GROSS_WEIGHT, ICE_WEIGHT, NET_WEIGHT, PRODUCT, source, station, TO_TIME) values (null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2009-10-14 15:51:56,680 DEBUG [AWT-EventQueue-0] AbstractBatcher - preparing statement
2009-10-14 15:51:56,741 DEBUG [AWT-EventQueue-0] AbstractEntityPersister - Dehydrating entity: [aaa.model.ContainerUse#<null>]
2009-10-14 15:51:56,762 DEBUG [AWT-EventQueue-0] NullableType - binding null to parameter: 1
2009-10-14 15:51:56,790 DEBUG [AWT-EventQueue-0] NullableType - binding null to parameter: 2
2009-10-14 15:51:56,809 DEBUG [AWT-EventQueue-0] NullableType - binding '1' to parameter: 3
2009-10-14 15:51:56,827 DEBUG [AWT-EventQueue-0] NullableType - binding null to parameter: 4
2009-10-14 15:51:56,847 DEBUG [AWT-EventQueue-0] NullableType - binding null to parameter: 5
2009-10-14 15:51:56,867 DEBUG [AWT-EventQueue-0] NullableType - binding '0.0' to parameter: 6
2009-10-14 15:51:56,885 DEBUG [AWT-EventQueue-0] NullableType - binding '0.0' to parameter: 7
2009-10-14 15:51:56,908 DEBUG [AWT-EventQueue-0] NullableType - binding '0.0' to parameter: 8
2009-10-14 15:51:56,927 DEBUG [AWT-EventQueue-0] NullableType - binding null to parameter: 9
2009-10-14 15:51:56,948 DEBUG [AWT-EventQueue-0] NullableType - binding null to parameter: 10
2009-10-14 15:51:56,967 DEBUG [AWT-EventQueue-0] NullableType - binding null to parameter: 11
2009-10-14 15:51:56,985 DEBUG [AWT-EventQueue-0] NullableType - binding null to parameter: 12
2009-10-14 15:51:57,047 DEBUG [AWT-EventQueue-0] IdentifierGeneratorFactory - Natively generated identity: 713
//------^^^^^^ This line is a culprit I think-------------------------------------------------------
2009-10-14 15:51:57,064 DEBUG [AWT-EventQueue-0] AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2009-10-14 15:51:57,083 DEBUG [AWT-EventQueue-0] AbstractBatcher - closing statement
20
Here is the "bad" log:
Code:
2009-10-14 15:59:00,904 DEBUG [AWT-EventQueue-0] AbstractSaveEventListener - saving [aaa.model.ContainerUse#<null>]
2009-10-14 15:59:00,922 DEBUG [AWT-EventQueue-0] AbstractSaveEventListener - executing insertions
2009-10-14 15:59:01,042 DEBUG [AWT-EventQueue-0] AbstractSaveEventListener - executing identity-insert immediately
2009-10-14 15:59:01,064 DEBUG [AWT-EventQueue-0] AbstractEntityPersister - Inserting entity: aaa.model.ContainerUse (native id)
2009-10-14 15:59:01,088 DEBUG [AWT-EventQueue-0] AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
2009-10-14 15:59:01,105 DEBUG [AWT-EventQueue-0] AbstractBatcher - insert into TEST.AAA.COPY_OF_CONTAINER_USE (id, BATCH, comment, CONTAINER, employee, FROM_TIME, GROSS_WEIGHT, ICE_WEIGHT, NET_WEIGHT, PRODUCT, source, station, TO_TIME) values (null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2009-10-14 15:59:01,122 DEBUG [AWT-EventQueue-0] AbstractBatcher - preparing statement
2009-10-14 15:59:01,196 DEBUG [AWT-EventQueue-0] AbstractEntityPersister - Dehydrating entity: [aaa.model.ContainerUse#<null>]
2009-10-14 15:59:01,214 DEBUG [AWT-EventQueue-0] NullableType - binding null to parameter: 1
2009-10-14 15:59:01,232 DEBUG [AWT-EventQueue-0] NullableType - binding null to parameter: 2
2009-10-14 15:59:01,250 DEBUG [AWT-EventQueue-0] NullableType - binding '1' to parameter: 3
2009-10-14 15:59:01,267 DEBUG [AWT-EventQueue-0] NullableType - binding null to parameter: 4
2009-10-14 15:59:01,284 DEBUG [AWT-EventQueue-0] NullableType - binding null to parameter: 5
2009-10-14 15:59:01,304 DEBUG [AWT-EventQueue-0] NullableType - binding '0.0' to parameter: 6
2009-10-14 15:59:01,341 DEBUG [AWT-EventQueue-0] NullableType - binding '0.0' to parameter: 7
2009-10-14 15:59:01,361 DEBUG [AWT-EventQueue-0] NullableType - binding '0.0' to parameter: 8
2009-10-14 15:59:01,381 DEBUG [AWT-EventQueue-0] NullableType - binding null to parameter: 9
2009-10-14 15:59:01,400 DEBUG [AWT-EventQueue-0] NullableType - binding null to parameter: 10
2009-10-14 15:59:01,417 DEBUG [AWT-EventQueue-0] NullableType - binding null to parameter: 11
2009-10-14 15:59:01,437 DEBUG [AWT-EventQueue-0] NullableType - binding null to parameter: 12
2009-10-14 15:59:01,472 DEBUG [AWT-EventQueue-0] AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2009-10-14 15:59:01,489 DEBUG [AWT-EventQueue-0] AbstractBatcher - closing statement
2009-10-14 15:59:01,525 DEBUG [AWT-EventQueue-0] JDBCExceptionReporter - could not insert: [aaa.model.ContainerUse] [insert into TEST.AAA.COPY_OF_CONTAINER_USE (id, BATCH, comment, CONTAINER, employee, FROM_TIME, GROSS_WEIGHT, ICE_WEIGHT, NET_WEIGHT, PRODUCT, source, station, TO_TIME) values (null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]
org.h2.jdbc.JdbcSQLException: NULL not allowed for column ID; SQL statement:
insert into TEST.AAA.COPY_OF_CONTAINER_USE (id, BATCH, comment, CONTAINER, employee, FROM_TIME, GROSS_WEIGHT, ICE_WEIGHT, NET_WEIGHT, PRODUCT, source, station, TO_TIME) values (null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [90006-111]
at org.h2.message.Message.getSQLException(Message.java:107)
at org.h2.message.Message.getSQLException(Message.java:118)
at org.h2.message.Message.getSQLException(Message.java:77)
at org.h2.table.Column.validateConvertUpdateSequence(Column.java:279)
at org.h2.table.Table.validateConvertUpdateSequence(Table.java:582)
at org.h2.command.dml.Insert.update(Insert.java:97)
at org.h2.command.CommandContainer.update(CommandContainer.java:71)
at org.h2.command.Command.executeUpdate(Command.java:207)
at org.h2.server.TcpServerThread.process(TcpServerThread.java:297)
at org.h2.server.TcpServerThread.run(TcpServerThread.java:136)
at java.lang.Thread.run(Unknown Source)
at org.h2.engine.SessionRemote.done(SessionRemote.java:525)
at org.h2.command.CommandRemote.executeUpdate(CommandRemote.java:193)
at org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPreparedStatement.java:139)
at org.h2.jdbc.JdbcPreparedStatement.executeUpdate(JdbcPreparedStatement.java:128)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2158)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2638)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
.......(snip)
2009-10-14 15:59:01,588 WARN [AWT-EventQueue-0] JDBCExceptionReporter - SQL Error: 90006, SQLState: 90006
2009-10-14 15:59:01,605 ERROR [AWT-EventQueue-0] JDBCExceptionReporter - NULL not allowed for column ID; SQL statement:
insert into TEST.AAA.COPY_OF_CONTAINER_USE (id, BATCH, comment, CONTAINER, employee, FROM_TIME, GROSS_WEIGHT, ICE_WEIGHT, NET_WEIGHT, PRODUCT, source, station, TO_TIME) values (null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [90006-111]
Everything else in the logs is the same, i.e. the same INSERT statement with a null for ID, which magically succeds in one of the cases even though ID is not null... perhaps due to some magic with IdentifierGeneratorFactory. What does it do, and how do I make it work for the 2nd case as well?