mysql的sys_guid,配置Hibernate以使用Oracle的SYS_GUID()作为主键

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.

解决方案

You might be able to use the "guid" generator. See this post from the Hibernate forum. It looks like they added support for Oracle using SYS_GUID() a while back, but the documentation still says they only support SQL Server and MySQL.

I haven't worked with JPA annotations yet, but here is an example using XML configuration:

EDIT: In regards to your second question, I think you are asking why Hibernate can't do something like this:

INSERT INTO PRODUCT (PRODUCT_ID, /* etc */)

SELECT SYSGUID(), /* etc */

The reason is that Hibernate must know what the object's ID is. For example, consider the following scenario:

You create a new Product object and save it. Oracle assigns the ID.

You detach the Product from the Hibernate session.

You later re-attach it and make some changes.

You now want to persist those changes.

Without knowing the ID, Hibernate can't do this. It needs the ID in order to issue the UPDATE statement. So the implementation of org.hibernate.id.GUIDGenerator has to generate the ID beforehand, and then later on re-use it in the INSERT statement.

This is the same reason why Hibernate cannot do any batching if you use a database-generated ID (including auto-increment on databases that support it). Using one of the hilo generators, or some other Hibernate-generated ID mechanism, is the only way to get good performance when inserting lots of objects at once.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值