ysk(0soo)
Lifealong
ysk(0soo)
전체 방문자
오늘
어제
  • 분류 전체보기 (238)
    • Java (50)
      • whiteship-java-study (11)
      • Java (28)
      • time (6)
    • Spring (68)
      • JPA (15)
      • Spring (1)
      • SpringBoot (1)
      • SpringMVC (6)
      • Spring Security (22)
      • Jdbc (1)
      • RestDocs (14)
      • log (6)
    • Kotlin (3)
    • Web (2)
      • nginx (1)
    • Database (14)
      • MySQL (5)
      • PostgreSQL (1)
      • SQL (1)
      • Redis (4)
    • C, C++ (0)
    • Git (1)
    • Docker (2)
    • Cloud (3)
      • AWS (3)
    • 도서, 강의 (0)
      • t5 (0)
    • 기타 (7)
      • 프로그래밍 (1)
    • 끄적끄적 (0)
    • CS (14)
      • 운영체제(OS) (2)
      • 자료구조(Data Structure) (9)
    • 하루한개 (12)
      • 우아한 테크코스-10분테코톡 (12)
    • 스터디 (12)
      • 클린 아키텍처- 로버트마틴 (2)
      • JPA 프로그래밍 스터디 (10)
    • 테스트 (34)
      • JUnit (19)
      • nGrinder (2)
      • JMeter (0)
    • Infra (3)
    • 프로그래머스 백엔드 데브코스 3기 (0)
    • 디자인 패턴 (3)
    • Issue (4)
    • system (1)
      • grafana (0)
      • Prometheus (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • github

공지사항

인기 글

태그

  • StructuredConcorrency
  • AuthenticationException
  • 인가(Authorization) 처리
  • 동시성 제어
  • DataJpaTest
  • 동등성
  • 트랜잭션
  • 정규표현식
  • nginx basic auth
  • junit5
  • querydsl
  • AccessDecisionManager
  • restdocs enum
  • FilterSecurityInterceptor
  • mysql
  • scope value
  • 동일성
  • LocalDateTime
  • VirtualThread Springboot
  • AccessDecisionVoter 커스텀
  • 구조화된 동시성
  • jpa
  • tree
  • 가상 스레드
  • 가상 스레드 예외 핸들링
  • java
  • node exporter basic auth
  • restdocs custom
  • UserDetailsService
  • nGrinder

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
ysk(0soo)

Lifealong

Java/Java

Java 현재 시간 + 랜덤 문자로 고유값 만들기 - with apache commons library (Java Random String)

2023. 1. 28. 21:12

Java 현재 시간 + 랜덤 문자로 고유값 만들기 - with apache commons library (Java Random String)

Apache Commons의 RandomStringUtils 클래스를 이용하여 랜덤 문자열을 만들 수 있다.

RandomStringUtils 을 사용하기 위해서는 commons-lang3이 필요하다.

build.gradle 에 아래 항목을 추가한다.

implementation 'org.apache.commons:commons-lang3:3.0'
import org.apache.commons.lang3.RandomStringUtils;

RandomStringUtils.randomAlphabetic(4);

메서드 인자로 생성할 random String의 length를 넘겨주면 된다.

Custom JPA Id Generator

이를 응용한 현재 시간 + 랜덤 문자로 고유한 Id 값을 만들어 낼 수 있다.

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.apache.commons.lang3.RandomStringUtils;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;

public class OrderIdGenerator implements IdentifierGenerator {

    @Override
    public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {

        String currentDateTime = LocalDateTime.now()                                                                    
      .format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));

    String randomId = RandomStringUtils.randomAlphabetic(4); 

        return currentDateTime + "-ysk-" + randomId;
    }

}
저작자표시 비영리 (새창열림)

'Java > Java' 카테고리의 다른 글

Java CompletableFuture  (0) 2023.04.23
Java Future, Callable, Executor Concurrent 프로그래밍  (0) 2023.04.23
Java Papago 번역 API 사용  (0) 2023.01.26
Graceful Shutdown - JVM, Java, SpringBoot  (4) 2023.01.06
Lambda 표현식  (0) 2023.01.04
    'Java/Java' 카테고리의 다른 글
    • Java CompletableFuture
    • Java Future, Callable, Executor Concurrent 프로그래밍
    • Java Papago 번역 API 사용
    • Graceful Shutdown - JVM, Java, SpringBoot
    ysk(0soo)
    ysk(0soo)
    백엔드 개발을 좋아합니다. java kotlin spring, infra 에 관심이 많습니다. email : kim206gh@naver.com github : https://github.com/devysk

    티스토리툴바