Home > Net >  Java JPA - Why is IllegalArgumentException being thrown in Repository method that should allow for n
Java JPA - Why is IllegalArgumentException being thrown in Repository method that should allow for n

Time:07-23

I have a JPA repository method that is throwing an exception for a null value. I don't understand why its throwing the exception because it can be null in the database table. I don't believe its reaching the persistence stuff yet, but rather exception is be thrown before getting there.

Unfortunately I have to disguise a lot of the coding (plus I simplified by removing lots of params for readability). I would be grateful for any insights.

Here is key part of exception:

java.lang.IllegalArgumentException: Parameter a_e_mark in CMarkRepository.insertCMark must not be null!
    at org.springframework.data.repository.core.support.MethodInvocationValidator.invoke(MethodInvocationValidator.java:93)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
    at com.sun.proxy.$Proxy142.insertCMark(Unknown Source)
    at service.SMarkService.postCMark(SMarkService.java:213)
    at controller.SMarkController.postCMark(SMarkController.java:259)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    .....

Here is the important part of calling code:

public int postCMark    (CMark cMark) {

    CMark cm = null;
    int status = 0;
    try {
        status = cMarkRepository.insertCMark(cMark.getCFk(), stringListToStringArray(cMark.getAEMark())));
    } catch ( Exception e) {
        e.printStackTrace();
    }
    return status;
}
private String stringListToStringArray(List<String> stringList) {
    StringListConverterCommaDelim stringListConverterCommaDelim = new StringListConverterCommaDelim();
    return stringListConverterCommaDelim.convertToDatabaseColumn(stringList);
}

Here is the converter:

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.util.Arrays;
import java.util.List;
import static java.util.Collections.emptyList;

@Converter
public class StringListConverterCommaDelim implements AttributeConverter<List<String>, String> 
{
    private static final String SPLIT_CHAR = ",";

    @Override
    public String convertToDatabaseColumn(List<String> stringList) {
        String splitString = null;
        if(stringList != null) {
            splitString = String.join(SPLIT_CHAR, stringList);
            splitString = "{"   splitString   "}";
        }
        return splitString;
    }

    @Override
    public List<String> convertToEntityAttribute(String string) {
        return string != null ? Arrays.asList(string.split(SPLIT_CHAR)) : emptyList();
    }
}

here is the repo:

public interface CMarkRepository extends JpaRepository <CMark, String> {

    @Transactional
    @Modifying
    @Query(value = "INSERT INTO c_mark(c_fk, a_e_mark) values(:c_fk, :a_e_mark\\:\\:character varying[])", nativeQuery = true)
    int insertClassMark(@Param("c_fk") String classification_fk, @Param("a_e_mark") String a_e_mark);
}

CodePudding user response:

ok, figured it out. Just by adding @Nullable to each parameter directly its solved:

public interface CMarkRepository extends JpaRepository <CMark, String> {

    @Transactional
    @Modifying
    @Query(value = "INSERT INTO c_mark(c_fk, a_e_mark) values(:c_fk, :a_e_mark\\:\\:character varying[])", nativeQuery = true)
    int insertClassMark(@Param("c_fk") @Nullable String classification_fk, @Param("a_e_mark") @Nullable String a_e_mark);

}

  • Related