DDL Generation & @GenerationType.IDENTITY [message #1709053] |
Wed, 23 September 2015 11:00  |
Eclipse User |
|
|
|
Hy!
I have a Entity which has a combined Primary key like the following
@Entity
@IdClass(MessagePK.class)
public class MessageEntity {
@Id
@Column(Name = "CUSTOMER_ID")
private Integer customerId
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", unique = true, nullable = false, columnDefinition = "INTEGER")
private Integer id;
So the ID column has an Auto-Increment value in the production database. In Unit-Test we are using a in-Memory derby database and DDL Generation. But the table created doesn't respect the @GeneratedValue Annotation, only a plain Integer column without any Identity/Auto-Increment is defined.
So in Unit-Tests a EntityManager.persist(...) doesn't create a ID value, which causes NullPointerException's.
Is there any way to tell the DDL Generator to add Identity to a column?
Best regards,
Andreas Fagschlunger
|
|
|
|
Re: DDL Generation & @GenerationType.IDENTITY [message #1709103 is a reply to message #1709075] |
Thu, 24 September 2015 02:06   |
Eclipse User |
|
|
|
Hello Chris!
Here's the DDL statement for table creation:
CREATE TABLE MESSAGEENTITY (COMPANYID INTEGER, MESSAGEID INTEGER, SUBJECT VARCHAR(56), TEXT VARCHAR(1024), PRIMARY KEY (COMPANYID, MESSAGEID))
And this is what the INSERT Statement Looks like:
[EL Fine]: sql: 2015-09-24 07:50:06.127--ClientSession(1724457619)--Connection(2123960023)--Thread(Thread[main,5,main])--INSERT INTO MESSAGEENTITY (COMPANYID, SUBJECT, TEXT) VALUES (?, ?, ?)
bind => [1, Lorem ipsum ..., Lorem ipsum dolor sit amet ...]
The outcome is a SQLException:
Caused by: ERROR 23502: Column 'MESSAGEID' cannot accept a NULL value.
Properties are set as follow:
properties.put("javax.persistence.provider", "org.eclipse.persistence.jpa.PersistenceProvider");
properties.put(PersistenceUnitProperties.JDBC_URL, "jdbc:derby:memory:testDB2;create=true");
properties.put(PersistenceUnitProperties.TRANSACTION_TYPE, "RESOURCE_LOCAL");
properties.put(PersistenceUnitProperties.DDL_GENERATION, PersistenceUnitProperties.DROP_AND_CREATE);
properties.put(PersistenceUnitProperties.DDL_GENERATION_MODE, PersistenceUnitProperties.DDL_BOTH_GENERATION);
properties.put(PersistenceUnitProperties.LOGGING_PARAMETERS, Boolean.TRUE.toString());
properties.put(PersistenceUnitProperties.TARGET_DATABASE, TargetDatabase.Derby);
properties.put(PersistenceUnitProperties.LOGGING_LEVEL, SessionLog.FINER_LABEL);
Attached to this post you will find some Java source files to reproduce the Problem.
Best regards,
Andreas Fagschlunger
|
|
|
|
|
|
Re: DDL Generation & @GenerationType.IDENTITY [message #1710190 is a reply to message #1709996] |
Mon, 05 October 2015 05:01   |
Eclipse User |
|
|
|
Hello Chris!
Yes, removing the columDefinition did the trick:
[EL Fine]: sql: 2015-10-05 10:54:48.159--ServerSession(1638172114)--Connection(335708295)--Thread(Thread[main,5,main])--CREATE TABLE MESSAGEENTITY (MESSAGEID INTEGER GENERATED BY DEFAULT AS IDENTITY NOT NULL, COMPANYID INTEGER, SUBJECT VARCHAR(56), TEXT VARCHAR(1024), PRIMARY KEY (MESSAGEID))
This also answers my original question, adding "GENERATED BY DEFAULT AS IDENTITY NOT NULL" to the columnDefinition creates the column with identity.
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false, columnDefinition = "INTEGER GENERATED BY DEFAULT AS IDENTITY NOT NULL")
private Integer messageId;
Thank you.
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03833 seconds