Notice
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- ApplicationPidFileWriter
- 세미나
- Spring
- spring-mvc
- batch
- IntelliJ
- SuperTypeToken
- spring batch 코드
- docker
- spring-webmvc #코드읽기
- JPA mapping
- spring pid
- spring jpa
- 코드 리뷰
- JPA
- REST API
- spring-webmvc
- spring boot
- JUnit
- tomcat
- Spring Data JPA
- 톰캣
- spring camp
- spring boot tomcat
- Data REST
- static inner class
- ORM
- Spring Data REST
- Spring Batch
- spring bean
- Today
- 83
- Total
- 822,211
woniper
JPA SLiPP 발표자료 본문
'Framework' 카테고리의 다른 글
[querydsl] Data JPA와 Querydsl 설정과 사용 (3) | 2016.10.23 |
---|---|
IoC와 Framework (1) | 2016.01.22 |
JPA SLiPP 발표자료 (3) | 2015.09.22 |
[junit] Spring-Test 사용하기 (0) | 2015.05.21 |
[junit] junit 사용하기 (1) | 2015.05.21 |
[JPA] Entity 객체 생명주기(Lifecycle)와 Persistence Context (4) | 2015.05.12 |
3 Comments
-
goodjwon 2015.09.22 14:45 좋은 자료 감사 드립니다. 초반에 언급해 주신 문제로 해서 신규로 진행 하는 프로젝트들은 jpa를 써 보려고 한참 공부 하는 중 입니다.^^
한가지 궁금한게 있어서 질문 드립니다.
대리키를 시퀀셜 하게 1,2,3, 이렇게 쓰는 경우도 있지만 저희는 보통 "업무코드_날짜+순번"을 섞어서 내부 시스템 키를 생성 합니다.
열심히 찾아 보았으나 후자와 같이 키를 생성 하는 방법을 jpa에서는 찾지를 못하고 있습니다. 힌트 주실수 있으면 goodjwon@gmail.com 으로 메일 주셨으면 합니다.
감사합니다.^^* -
goodjwon 2015.10.01 18:01 자답입니다. 혹시나 구글링 하시다가 오시는 분 있을 것 같아 남깁니다.^^
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.id.IdentifierGenerator;
public class StockCodeGenerator implements IdentifierGenerator {
private static Logger log = Logger.getLogger(StockCodeGenerator.class);
public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
String prefix = "M";
System.out.println(prefix);
Connection connection = session.connection();
try {
PreparedStatement ps = connection.prepareStatement("SELECT nextval ('HIBERNATE_SEQUENCE') as nextval");
ResultSet rs = ps.executeQuery();
if (rs.next()) {
int id = rs.getInt("nextval");
String code = prefix + StringUtils.leftPad("" + id, 3, '0');
log.debug("Generated Stock Code: " + code);
return code;
}
} catch (SQLException e) {
log.error(e);
throw new HibernateException("Unable to generate Stock Code Sequence");
}
return null;
}
}
이런 식으로 사용자 정의 가능 하고
@Id
@GenericGenerator(name="seq_id", strategy="jpabook.codes.StockCodeGenerator")
@GeneratedValue(generator="seq_id")
@Column(name = "MEMBER_ID")
private String id;
이런 식으로 사용 가능 합니다.
수고하세요~ -
woniper1 2015.10.02 17:06 신고 와 감사합니다!!