Issue
I want to have an entity with a string pk, which is generated by database with a static prefix and a sequence. I get this error when I try to insert a new row via my spring application. Can someone help me?
My Error:
"Unknown integral data type for ids : java.lang.String; nested exception is org.hibernate.id.IdentifierGenerationException: Unknown integral data type for ids : java.lang.String"
My Table sql:
CREATE sequence octo_reference_code;
SELECT setval('octo_reference_code', 1010);
create table user_references
(
code text not null default 'octo' || nextval('octo_reference_code')
constraint user_references_pk
primary key,
user_id uuid
constraint user_references_users_id_fk
references users,
create_date timestamptz default now()
);
My entity class pk definition:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "code", columnDefinition = "text", nullable = false, updatable = false)
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
Solution
You need to cast the sequence to a TEXT. Append ::TEXT
to the end of the nextval()
call:
code text not null default 'octo' || nextval('octo_reference_code')::TEXT
Answered By - Jack Straw
Answer Checked By - Willingham (JavaFixing Volunteer)