Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 生成自动递增的字符串类型的长数字id_Java_Jpa_Spring Boot_H2 - Fatal编程技术网

Java 生成自动递增的字符串类型的长数字id

Java 生成自动递增的字符串类型的长数字id,java,jpa,spring-boot,h2,Java,Jpa,Spring Boot,H2,我需要将带有数字id(15-25位)的实体保存到H2db。由于db不支持biginger(它映射到Decimal),因此保存如此长的数字的唯一方法是String类型 问题:我如何通过自动递增生成这样一个String类型的数字id 更新 ID看起来应该是:123456789012345(最小15位,最大25位)使用标识(从1000000000开始),java长,自动递增:您仍然可以在幕后使用大整数 类似的东西应该适用于任意位数,并且是线程安全的 private static final Atomi

我需要将带有数字id(15-25位)的实体保存到
H2
db。由于db不支持
biginger
(它映射到
Decimal
),因此保存如此长的数字的唯一方法是
String
类型

问题:我如何通过自动递增生成这样一个
String
类型的数字id

更新


ID看起来应该是:123456789012345(最小15位,最大25位)

使用标识(从1000000000开始),java长,自动递增:

您仍然可以在幕后使用
大整数

类似的东西应该适用于任意位数,并且是线程安全的

private static final AtomicReference<BigInteger> id = new AtomicReference<>(BigInteger.ZERO);

public String nextId() {
    BigInteger next = id.accumulateAndGet(BigInteger.ONE, (x, y) -> x.add(y));
    return next.toString();
}

public void test(String[] args) {
    for ( int i = 0; i < 100; i++ ) {
        System.out.println(nextId());
    }
}
private static final AtomicReference id=新的AtomicReference(biginger.ZERO);
公共字符串nextId(){
biginger next=id.acgregateandget(biginger.ONE,(x,y)->x.add(y));
返回next.toString();
}
公共无效测试(字符串[]args){
对于(int i=0;i<100;i++){
System.out.println(nextId());
}
}
如果你的限制(15-25位)很难,那么像这样的事情可能就行了

private static final int MIN_DIGITS = 2;
private static final int MAX_DIGITS = 3;
private static final BigInteger start = BigInteger.TEN.pow(MIN_DIGITS-1);
private static final BigInteger limit = BigInteger.TEN.pow(MAX_DIGITS).subtract(BigInteger.ONE);

private static final AtomicReference<BigInteger> id = new AtomicReference<>(start);

public String nextId() {
    BigInteger next = id.accumulateAndGet(BigInteger.ONE, (x, y) -> {
        if(x.compareTo(limit) >= 0) {
            // Back to start.
            return start;
        }
        return x.add(y);
    });
    return next.toString();
}

public void test(String[] args) {
    for ( int i = 0; i < 1000; i++ ) {
        System.out.println(nextId());
    }
}
private static final int MIN_DIGITS=2;
专用静态最终整数最大位数=3;
私有静态final biginger start=biginger.TEN.pow(最小位数-1);
私有静态最终BigInteger限制=BigInteger.TEN.pow(最大\u位)。减法(BigInteger.ONE);
私有静态最终原子引用id=新原子引用(开始);
公共字符串nextId(){
biginger next=id.acgregateandget(biginger.ONE,(x,y)->{
如果(x.compareTo(limit)>=0){
//回到起点。
返回启动;
}
返回x.add(y);
});
返回next.toString();
}
公共无效测试(字符串[]args){
对于(int i=0;i<1000;i++){
System.out.println(nextId());
}
}

请注意,对于测试,我已将限制设置为
2
3
。您可以根据自己的喜好进行调整。

UUID.randomUUID().toString()
。但是Long是可能的:UUID格式('550e8400-e29b-41d4-a716-446655440000')不合适。如果可以,这将是最简单的方法,但Long只支持最多19个符号。我最多需要25个符号。这就是为什么我迟迟不知道该怎么做。@user1376885为什么您最多需要25个符号,为什么它应该是自动递增的?你的要求很奇怪。没有解释为什么需要25个符号,这只是测试任务范围内的要求。至于autoincrement,如何使ID唯一是我的假设。@user1376885不要假设任何事情,并澄清您的需求。你现在试图用“自动递增字符串”做的事情太荒谬了。我将尝试澄清需求。一个问题是,如果需要对ID使用长类型,但最小位数=10,最大位数=15,我如何设置这些限制?我看不到db中对现有ID的任何引用。是否存在可能的重复ID?特别是如果我们重新启动application@user1376885-您必须在启动时从数据库中初始化初始值。