Class <<classname>> cannot be cast to class java.lang.String (<<classname>> is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')

We have migrated one springboot project from 2.3.4-RELEASE to 3.0.1 . The application is running fine but when we hit the request , we are getting below error -

Entity relationship:

@Data
@Entity
@Table(name = "tableA ")
public class tableA {

@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name = “pk”)
private long objectId;

@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name = “reference_code”,referencedColumnName = “reference_code”)
private Set tableBList = new HashSet<>();

//Other columns of TableA
}

@Data
@Entity
@Table(name = “TableB”)
public class tableB {

@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name = “pk1”)
private long objectId;
//Other columns of TableB
}

java.lang.ClassCastException: class classname cannot be cast to class java.lang.String (classname is in unnamed module of loader ‘app’; java.lang.String is in module java.base of loader ‘bootstrap’)
at org.hibernate.type.descriptor.java.StringJavaType.unwrap(StringJavaType.java:27) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.type.descriptor.jdbc.VarcharJdbcType$1.doBind(VarcharJdbcType.java:97) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.type.descriptor.jdbc.BasicBinder.bind(BasicBinder.java:63) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.exec.internal.AbstractJdbcParameter.bindParameterValue(AbstractJdbcParameter.java:108) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.exec.internal.AbstractJdbcParameter.bindParameterValue(AbstractJdbcParameter.java:98) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.results.jdbc.internal.DeferredResultSetAccess.executeQuery(DeferredResultSetAccess.java:188) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.results.jdbc.internal.DeferredResultSetAccess.getResultSet(DeferredResultSetAccess.java:146) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.results.jdbc.internal.JdbcValuesResultSetImpl.advanceNext(JdbcValuesResultSetImpl.java:205) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.results.jdbc.internal.JdbcValuesResultSetImpl.processNext(JdbcValuesResultSetImpl.java:85) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.results.jdbc.internal.AbstractJdbcValues.next(AbstractJdbcValues.java:29) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]

I’m not sure what your application is trying to do, so it’s hard to say what the problem might be. I see from the stack-trace you’re still using Hibernate 6.1.6.Final, so I can suggest trying to upgrade to Hibernate’s latest minor version 6.2.

If you still encounter the error, please provide more details on the context where the CCE occurs and ideally the code that triggers the problem.

We are just trying to fetch data by defining a uni-directional oneToMany mapping . Although there is no foreign key relationship between the tables in database. Same issue with 6.2.0.Final

The latest version of 6.2 currently available is 6.2.8.Final. We’re going to need more details to know where the problem resides, but if you think this is a bug please create an issue in the issue tracker with a test case that reproduces the issue.