2

I am incrementing my Autogenerated Id value but I need String type

.This is my service class:

package com.stackroute.orderhistory.service;

import com.stackroute.orderhistory.model.AutogenerateId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Objects;


import static org.springframework.data.mongodb.core.FindAndModifyOptions.options;

@Service
public class SequenceGeneratorService {
    @Autowired
    private MongoOperations mongoOperations;

    public String getSequenceNumber(String sequenceName){
        Query query=new Query(Criteria.where("order_id").is(sequenceName));

        Update update=new Update().inc("seq",1);

        AutogenerateId counter = mongoOperations
                .findAndModify(query,
                        update, options().returnNew(true).upsert(true),
                        AutogenerateId.class);

        return !Objects.isNull(counter) ? counter.getSeq() : "1";
    }
}

.This is my Entity Class :

import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.Document;

@NoArgsConstructor
@ToString
@AllArgsConstructor
@Getter
@Setter
@Document(collection = "orders")
public class OrderHistoryModel {
    @Transient
    public static final String SEQUENCE_NAME="Ordersequence";
    @Id
    private String orderId;
    private String productName;
    private int orderAmount;
    private String orderPlaced;
    private String orderStatus;
    private String paymentMethod;
    private String shippingAddress;
}

.This is my AutoGenerated Entity class :

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "db_sequence")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AutogenerateId {

    @Id
    private String id;
    private String seq;
}

In this Code I am getting Order_Id as 1,2,3 and so on (In Postman)

But I need Order_Id like order1, order2, order3 and so on... Please help me out.

1 Answer 1

2

In my case, I didn't customise the auto-generation of the id, instead: in my entity i just annotated the id by @Id as follow:

enter image description here

  • the DTO:

enter image description here

  • and here's my service :

enter image description here

  • the method dtoToUser:

enter image description here

and mongodb will generate a random and unique UUID for me, as follow:

enter image description here

So, Use IDs provided by MongoDB, which are more or less universally unique

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.