开发者

How to inject custom object ids into JPA entities

开发者 https://www.devze.com 2023-01-13 05:47 出处:网络
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

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.


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;
...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消