java hibernate how update value after record save in certain format

303 views Asked by At

I want generate unique code in two format for my record in table. like following way

1) "Firstprefix"+"ddmmyyyy"+Sr.number e.g AD190620151

2) "secondprefix"+"ddmmyyyy"+Sr.number e.g AP190620151

if another record inserts it will be one of them but serial no change e.g AD190620152 AP190620152 this generated according to another field in record how to do this please give mi answer.

i have try in following way

     @Override
     public String createOrder(OrderMasterTbl order) {
     getHibernateTemplate().saveOrUpdate(order);


     Integer addId=order.getAdType().getId();
     String id = null ;
     if(addId.equals(1))
      {
      id="ap"+new SimpleDateFormat("ddmmyyyy").format(newDate())+order.getId(); 

     }else{
         id="dp"+new SimpleDateFormat("ddmmyyyy").
         format(newDate())+order.getId();
     }

     System.out.print(id);
     order.setOrderCode(id);
     getHibernateTemplate().saveOrUpdate(order);

     return id;
}

But in this way i am not able to generate proper serial number according two format .

1

There are 1 answers

5
cнŝdk On BEST ANSWER

You don't get a proper serial number here because you are using a wrong format for SimpleDateFormat, you are referring to month with m while it should be M, take a look at the SimpleDateFormat Documentation for further information.

Your code should be like this:

new SimpleDateFormat("ddMMyyyy").format(newDate())

EDIT:

In order to make the code increment in the last you have just to use two variables ap and dp in your class that you initialize with 1 and in your method you increment them according to your if block, your code should be like this:

 @Override
 public String createOrder(OrderMasterTbl order) {
      getHibernateTemplate().saveOrUpdate(order);


      Integer addId=order.getAdType().getId();
      String id = null ;
      if(addId.equals(1)){
          id="ap"+ new SimpleDateFormat("ddMMyyyy").format(newDate())+ap;
          ap++;

      }else{
          id="dp"+new SimpleDateFormat("ddMMyyyy").format(newDate())+dp;
          dp++;
      }

      System.out.print(id);
      order.setOrderCode(id);
      getHibernateTemplate().saveOrUpdate(order);

      return id;
 }