본문 바로가기

Java

(9)
JPA에서 entity가 update가 아닌 insert가 되는 경우 결론:Entity의 colmun이 primary key이기 때문에 Entity의 colmun이 기존에 저장된 값과 다르면 새로운 행으로 인식되어 insert가 되는 것이다 -- 내용 -- 위와 같은 entity가 있다 치고 ExampleEntity exampleEntity = exampleRepository.findFirstByOrderByColumnAsc(); if (exampleEntity == null) { exampleEntity = new ExampleEntity(); } exampleEntity.setColmun(BigInteger.valueOf(12345)); exampleRepository.save(exampleEntity); 아래 같은 로직이 있다 치자 entity가 null이 라면 ne..
가변인자 사용해서 자바에서 디폴트 매개 변수 흉내내기 파이썬에선 디폴트 매개변수를 사용해서 파라미터를 생략 할 수 있다. 이를 정말 유용하게 썼었지만 자바에는 이런 기능이 없다. 그래서 한번 비슷하게 하는법 public String sampleMethod(String name, String id, long... age) { ... if (age.length == 0) { // set defualt } ... } 이런식으로 age를 가변인자로 박으면 어느정도 default 변수처럼 흉내 낼 수 있다... 추가로 Arrays.stream(age).sum()) .. 같이 스트림을 자기 상황에 맞게 쓰면 더 편할 수 있다
@JSONSETTER 이용 중 삽질 기존 코드 @Data @NoArgsConstructor public class MetaData { ... @JsonSetter("attributes") public void setCategory(List attributes) { Class clazz = this.getClass(); for(Map attribute : attributes){ String traitType = (String) attribute.get("trait_type"); String traitValue = (String) attribute.get("value"); try { Field field = clazz.getDeclaredField(traitType.toLowerCase()); field.setAccessible(true); ..
[자바/java] 배열 마지막 요소 가져오기 Stream.of(httpServletRequest.getServletPath().split("/")).reduce((first, second)->second).get(); 슬랙의 이미지 url 을 구현하기 위해 https://dasjdkl.slack.com/team/U03K5F41WBG
[JPA] @Query update문 (Not supported for DML operations 해결) update retyrn 받는법 여지껏 mybatis만 사용하다가 이번에 프로젝트를 진행하면서 JPA를 사용하기로 결정했다. JPA에선 @DynamicUpdate 를 이용해서 dirty checking을 하며 바뀐 값만을 update 해준다. 수정 시간도 자동으로 넣어줄 수 있어서 매우 편리하고 깔끔하다. 하지만 dirty check를 사용하기 때문에 mybatis처럼 return 값을 받을 수가 없다. 실패했는지 성공했는지, update가 진행 됐는지, 진행이 됐다면 몇 행이 수정됐는지 등을 알 수가 없다. 하여 이부분을 찾다가 dirtycheck로는 무리가 있다고 판단. 쿼리를 직접 짜보기로했다. ========= 본론 ========== public interface UserProfileRepository extends JpaRe..
yaml & * &로 변수 *로 갖다 쓰기
[JAVA] HttpURLConnection https 호출 시 ssl 인증서 오류(java.security.cert.CertificateException: No subject alternative names matching IP address found) HttpURLConnection를 사용해 http 주소를 불러 오다가 https로 교체되었다. 주소만 바꿔줬더니 "javax.net.ssl.SSLHandshakeException:javax.net.ssl.SSLHandshakeException: No subject alternative names matching IP address found" 에러 발생 결론부터 말하면 https 사용에 따른 ssl 인증 오류이다. 이를 해결하기 위해 여러 방법이 있지만 SSL 인증을 우회하는 방법을 사용했다. 우선 HttpURLConnection을 알아보자 URLConnection, HttpURLConnection, HttpsURLConnection 차이 URLConnection: 기본 클래스입니다. 추상 클래스이므로 ..
spring 2로 바꾸면서 Spring security로 인한 로그인창 없애고 custom 로그인 창으로 대체하기 1. Spring security 때문에 로그인 페이지가 뜬다. 이걸 끄고 custom 로그인 페이지를 사용해보자 SecurityConfig를 만들어준다. 그안에서 ignore.antMatcher("경로") 로 spring security를 무시해주자 참고할 만한곳 : https://gs.saro.me/dev?page=20&tn=480 2. viewControllerRegistry를 해준다. config 파일에 처음 들어오면 가게되는 페이지를 설정한다. registry.addViewController("/").setViewName("forward:/login"); // 컨트롤러에서 받을 주소 registry.setOrder(Ordered.HIGHEST_PRECEDENCE); super.addViewCon..