I am looking for a way to get hibernate to use oracle's SYS_GUID()
function when inserting new rows. Currently my DB tables have SYS_GUID()
as the default so if hibernate simply generated SQL that omited the value it should work.
I have everything working, but it is currently generating the UUID/GUID in code using the system-uuid generator:
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
@Column(name = "PRODUCT_ID", unique = true, nullable = false)
public String getId() {
return this.productId;
}
This is fine, but I would prefer that the guids were generated by the database so they will be sequential and potentially have better performance. Plus I would just like to know how to configure this.
I am using annotations for configuration, but xml configuration examples are awesome as well.
Here is a sample table definition (in case it matters):
CREATE TABLE SCHEMA_NAME.PRODUCT
(
PRODUCT_ID RAW(16) DEFAULT SYS_GUID() NOT NULL,
PRODUCT_CODE VARCHAR2(10 CHAR) NOT NULL,
PRODUCT_NAME VARCHAR2(30 CHAR) NOT NULL,
PRODUCT_DESC VARCHAR2(512 CHAR)
)
UPDATE:
Mat's sollution of using "guid" worked, here is the sql generated:
Hibernate:
select rawtohex(sys_guid())
from dual
Hibernate:
insert into PRODUCT
(PRODUCT_CODE, PRODUCT_DESC, LOB_ID, PRODUCT_NAME, PROVIDER_ID, PRODUCT_ID)
values (?, ?, ?, ?, ?, ?)
It seems that using the columns default value in an insert is not possible, so the choice is between an application generated guid and a database round trip.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…