hibernate annotation 生成uuid主键

本文介绍如何使用Hibernate在Java持久化API(JPA)标准之外生成UUID类型的主键。通过具体的代码示例展示了如何利用注解配置自定义的主键生成策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JPA标准方式下,不可以生成uuid类型的主键,但是hibernate提供了一些方式生成uuid主键,具体如下:
1.主键生成器
    @GeneratedValue(generator="idGenerator")
2.通过注解方式生成一个generator
    @GenericGenerator(name="idGenerator", strategy="uuid")
 
 

/**
 * annotation uuid
 * @author xueji
 */
@Entity
public class Teacher2 {
    private String id ;
    private String name ;
    private int title ;
    
    @Id
    @GeneratedValue(generator="idGenerator")
    @GenericGenerator(name="idGenerator", strategy="uuid")
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getTitle() {
        return title;
    }
    public void setTitle(int title) {
        this.title = title;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值