mikeb
mikeb

Reputation: 11267

EclipseLink Generated id while non-jpa processes are adding records too

I have an eclipselink entity with an id column:

@Id
@Basic(optional = false)
@NotNull
@Column(name = "ID")
private Long id;

The problem is no matter what generation strategy I use, I get errors because I have an ETL tool inserting records and the eclipse link generators do not seem to be able to check for an existing ID before creating a new one.

For example, if the JPA generator just generated id 49 for a new record, then the ETL tool generates 20 records with ID 50-70, when I create another eclipse link record the generator comes up with 50 for the ID, which has already been used.

How do I handle this?

Upvotes: 0

Views: 50

Answers (1)

Douglas
Douglas

Reputation: 5087

Your id generation strategy needs to:

  1. Be based on something in the database.
  2. Get the new id and update the next one atomically and without waiting for commit.
  3. Be used by both EclipseLink and your ETL tool.

If you satisfy all three conditions then it should work unless the database itself is not thread safe.

The easiest way to meet these conditions is probably to include AUTO_INCREMENT in the column's definition in the database. Another option would be a sequence generator, provided your database supports it, but you would have to make sure the ETL tool uses the sequence rather than, say, finding the max current id and adding 1.

I would guess the ETL tool using its own custom id generation (such as max+1) is your problem.

Upvotes: 1

Related Questions