JPA: Usage of @GeneratedValue on non-id column

jpa

Solution

JPA only mandates support for @GeneratedValue on @Id fields. Some JPA implementations (such as DataNucleus JPA) support it but not all do.

Problem

I am attempting to persist an entity with an attribute that I want to be populated from a DB sequence. I'm using Oracle, have created the sequence, verified the sequence works via sql, and yet my attribute isn't getting populated. Here's what I have: ``` @GeneratedValue(generator = "RFQ_LINE_IDS_SEQUENCE", strategy=GenerationType.SEQUENCE) @SequenceGenerator(name="RFQ_LINE_IDS_SEQUENCE", sequenceName="RFQ_LINE_IDS_SEQUENCE", allocationSize=1000000000) @Column(name = "external_line_item_id") private String externalLineItemId; ``` All the examples I'm seen online show this annotation being used with @Id, but I have another attribute that I'm using for my id. I've also tried the following to no avail: ``` @GeneratedValue(generator = "RFQ_LINE_IDS_SEQUENCE", strategy=GenerationType.SEQUENCE) @GenericGenerator(name = "RFQ_LINE_IDS_SEQUENCE", strategy = "sequence", parameters = {@Parameter(name = "sequence", value = "RFQ_LINE_IDS_SEQUENCE")}) @Column(name = "external_line_item_id") private String externalLineItemId; ```

Original source

Related problems