Home > database >  Need help: InvocationTargetException wrapped around "java.lang.ClassCastException: java.lang.St
Need help: InvocationTargetException wrapped around "java.lang.ClassCastException: java.lang.St

Time:08-25

I'm trying to execute a very simple query with Spring Data's CrudRepsitory and Hibernate.

Repo:

public interface LabelsAllRepository extends CrudRepository<LabelsAll, Integer>{

@Query("select distinct(a.kundentext1) from LabelsAll a where a.zeichnungsnummer= :zchnr and a.revision = :rev")
String findKtxt1ByZchnrAndRev(@Param("zchnr") String zchnr, @Param("rev") String rev);

}

I've tried many different ways to call that function but nothing helps. I always get InvocationTargetException wrapped around java.lang.ClassCastException: java.lang.String cannot be cast to [C. Also theres no proper stack trace i think because i'm using reflection? I'm really not exactly sure.

@Override
public boolean isCounterForSet(String edvNb, String revision) {
    boolean ret = false;
    // This line is causing the exception to be thrown
    String labelsAll = labelsAllRepository.findKtxt1ByZchnrAndRev(edvNb, revision); 
    if (!labelsAll.equals("")) {
        int snCount = snCounterRepository.countSnByKtxt1(labelsAll);
        ret = (snCount != 0);
    }
    return ret;
}

The query seems to be correct. [http-nio-8080-exec-1] DEBUG org.hibernate.SQL - select distinct labelsall0_.kundentext1 as col_0_0_ from ascom_etiketten_all labelsall0_ where labelsall0_.zeichnungsnummer=? and labelsall0_.revision=?

Could really use some help solving this problem.

CodePudding user response:

The problem was caused by entity class attribute defined as char[] instead of String. Hibernate therefore tried to cast @Param("rev") String rev to char[] which it couldn't do.

  • Related