ClassCastException retrieving byte[] from database

Hi,

I’m migrating from hibernate 5.3.24 to hibernate 6.2.1.

I have an entity mapped

SQL Server - Column definition: Photo IMAGE

@Entity	
@Table(name = "MYTABLE")
public class MyEntity {

	...

	@Lob
	@Column(name = "Photo")
	private byte[] photo = null;
	
	...
	
}

public byte[] getPhoto(String id) {
	CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
	CriteriaQuery<byte[]> cq = cb.createQuery(byte[].class);

	Root<MyEntity> root = cq.from(MyEntity.class);

	cq.select(root.get(MyEntity_.photo))
			.where(cb.equal(root.get(MyEntity_.id), id));

	TypedQuery<byte[]> q = getEntityManager().createQuery(cq);

	return q.getSingleResult();
}

Executing the query i’m getting:

Caused by: java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [B ([Ljava.lang.Object; and [B are in module java.base of loader 'bootstrap')

I’m not sure if this is a bug or something wrong on my mapping/criteria query.

Thanks

Looks like a bug. Please create an issue in the issue tracker(https://hibernate.atlassian.net) with a test case(hibernate-test-case-templates/JPAUnitTestCase.java at main · hibernate/hibernate-test-case-templates · GitHub) that reproduces the issue.

Thanks beikov, issue created:

https://hibernate.atlassian.net/browse/HHH-16606

1 Like