jpa 如何在Java Sping Boot 中创建自定义id生成器时放置前缀

oyxsuwqo  于 8个月前  发布在  Java
关注(0)|答案(1)|浏览(81)

我正在尝试为我的Java Sping Boot 项目创建一个自定义ID,格式类似于BDI-100。我知道如何设置数字部分,但不知道如何把它的前缀。我已经尝试了从这个thread in stackoverflow的解决方案,但它似乎不能与我的项目匹配,因为一些不同的版本与我的。
下面是我的项目的spec:

  • 使用Spring Tool Suite 4.18.0.RELEASE构建
  • Java 17
  • Sping Boot v3.1.3
  • 基于ubuntu 22.04

这是我的模型课或许能给给予线索。谢谢你的任何手的家伙!

package com.recruittest.bosnet.model;

import java.util.Date;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

import com.fasterxml.jackson.annotation.JsonFormat;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.SequenceGenerator;
import jakarta.persistence.Table;

@Entity
@Table(name = "bosnet_people")
public class People {

    @Id
    @Column(name = "people_id")
    @SequenceGenerator(name = "PeopleIdGenerator", initialValue = 100)
    @GeneratedValue(generator = "PeopleIdGenerator", strategy = GenerationType.IDENTITY)
    private String id;
    
    @Column(name = "full_name")
    private String fullName;
    
    @Column(name = "birthday")
    @JsonFormat(pattern = "yyyy-mm-dd")
    private Date birthday;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "People [id=" + id + ", fullName=" + fullName + ", birthday=" + birthday + "]";
    }
}
chhqkbe1

chhqkbe11#

您需要创建一个CustomIdGenerator类来扩展IdentifierGenerator,如下所示:

public class CustomIdGenerator implements IdentifierGenerator {

    @Override
    public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
        String prefix = "BDI";
        String query = "SELECT MAX(people_id) FROM bosnet_people";
        Connection connection = session.getJdbcConnectionAccess().obtainConnection();

        try (PreparedStatement statement = connection.prepareStatement(query)) {
            ResultSet rs = statement.executeQuery();
            if (rs.next()) {
                String lastId = rs.getString(1);
                if (lastId != null) {
                    int number = Integer.parseInt(lastId.substring(prefix.length())) + 1;
                    return prefix + "-" + number;
                }
            }
        } catch (SQLException e) {
            throw new HibernateException("Unable to generate ID", e);
        }

        return prefix + "-" + 100; // Initial value if no records are present
    }
}

此生成器将负责生成自定义ID。
接下来,将自定义ID生成器应用到People类:

@Entity
@Table(name = "bosnet_people")
public class People {

   @Id
   @GeneratedValue(generator = "custom-id", strategy = GenerationType.IDENTITY)
   @GenericGenerator(name = "custom-id", strategy = "your.package.here.CustomIdGenerator")
   @Column(name = "people_id")
   private String id;

   // ... other fields and methods
}

看看这个:

我希望它有帮助!

相关问题