Spring

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: Error attempting to apply AttributeConverter; nested exception is javax.persistence.PersistenceException

codeManager 2022. 5. 31. 18:45
반응형
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: Error attempting to apply AttributeConverter; nested exception is javax.persistence.PersistenceException:..

 

JPA AttributeConverter에서 exception 발생하는 경우입니다.

 

JPA entity에서 conveter를 만들어서 사용하는 경우에 null 처리가 안되어 있을 때 발생합니다.

 

public class ExampleEntity {
	// .....
    @Column(name = "ids", columnDefinition = "json")
    @Convert(converter = CustomConverter.class)
    private List<Long> ids;
}

 

public class CustomConverter implements AttributeConverter<Object, String> {
    private static final ObjectMapper om = new ObjectMapper();

    @Override
    public String convertToDatabaseColumn(Object attribute) {
    	if (Objects.isNull(attribute) {
        	// ... null 처리
        }
        try {
            return om.writeValueAsString(attribute);
        } catch (JsonProcessingException ex) {
            //... exception 처리
        }
    }

    @Override
    public Object convertToEntityAttribute(String dbData) {
    	if (Objects.isNull(dbData) {
        	// ... null 처리
        }
        try {
            return om.readValue(dbData, new TypeReference<List<Long>>() {});
        } catch (IOException ex) {
            //... exception 처리
        }
    }
}

 

해결방법

convertToDatabaseColum(), convertToEntityAttributes() 함수에서 받는 paramter Null 처리하는 부분을 추가한다.

반응형