Question

I am using JPA 2 for an enterprise application, and my DBA's just hit me with a twist.

They want me to use the group's centralized object ID generator for all my tables. This means rather than using table values or a sequence table, I will need to call a web service to get a batch of ~50 ids.

Then, as I persist any new object, I would need to inject this id first, and save that to the table.

So how would I manipulate the @Id column of an entity to handle this.

Is it as simple as setting a key before I persist? I suspect that would throw some sort of unmanaged entity with ID set error.

Was it helpful?

Solution

Update:

The better method is to actually specify a Sequence strategy on Generated fields and specify a custom Sequence class.

JPA will then call this class's nextId() method every time it inserts a new object.

This method allows full graphs to be persisted without intervening on each entity manually.


Figured it out. Amazingly complex ;) - just remove the GeneratedValue annotation from the key field.

It is intended for Native Ids like SSN or email, but works regardless of source.

@Entity
public class Client{

    @Id
    @Column(name="CLNT_ID") 
    private long key;
    @Column(name="CLNT_NUM")
    private String clientNumber;
...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top