fmpdmb
fmpdmb

Reputation: 1414

JPA: Usage of @GeneratedValue on non-id column

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;

Upvotes: 6

Views: 7603

Answers (2)

Petar Tahchiev
Petar Tahchiev

Reputation: 4376

I have created a proposal for JPA to support @GeneratedValue for non-id attributes. Please vote here for this to be included in 2.2 https://java.net/jira/browse/JPA_SPEC-113

Upvotes: 1

DataNucleus
DataNucleus

Reputation: 15577

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

Upvotes: 4

Related Questions