Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImmutableType should also implement the Type interface #119

Closed
vladmihalcea opened this issue Aug 23, 2019 · 3 comments
Closed

ImmutableType should also implement the Type interface #119

vladmihalcea opened this issue Aug 23, 2019 · 3 comments
Milestone

Comments

@vladmihalcea
Copy link
Owner

vladmihalcea commented Aug 23, 2019

Because UserType does not extend the Hibernate Type contract, we cannot provide a custom Type for a scalar result in a native SQL query as in the following example:

List<Tuple> tuples = entityManager
.createNativeQuery(
	"SELECT " +
	"  id, " +
	"  account_id, " +
	"  created_on - FIRST_VALUE(created_on) OVER ( " +
	"    PARTITION BY account_id " +
	"    ORDER BY created_on " +
	"    RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING " +
	"  ) AS since_first_transaction, " +
	"  LAST_VALUE(created_on) OVER ( " +
	"    PARTITION BY account_id " +
	"    ORDER BY created_on " +
	"    RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING " +
	"  ) - created_on AS until_last_transaction " +
	"FROM account_transaction " +
	"ORDER BY id", Tuple.class)
.unwrap(org.hibernate.query.NativeQuery.class)
.addScalar("since_first_transaction", PostgreSQLIntervalType.INSTANCE)
.getResultList(); 

The easiest way to fix this is to make the ImmutableType implement the Type contract as well.

This way, we can deal with the No Dialect mapping for JDBC type at the SQL query level, no matter if the associated type implements the Hibernate org.hibernate.type.Type or org.hibernate.usertype.UserType.

@vladmihalcea
Copy link
Owner Author

Fixed.

@vladmihalcea vladmihalcea added this to the 2.6.0 milestone Aug 23, 2019
@mfur
Copy link

mfur commented Apr 14, 2023

Hi, probably not appropriate place for this but it is directly related.

I'm using hypersistence-utils-hibernate-62 with hibernate 6.2. When I try to add custom type as a scalar result:

Query query = em.createNativeQuery(query.unwrap(NativeQuery.class)
		.addScalar("operation_time", PostgreSQLPeriodType.INSTANCE);

Type is not accepted and can not be cast:

The method addScalar(String, BasicTypeReference) in the type NativeQuery is not applicable for the arguments (String, PostgreSQLPeriodType)

or

java.lang.ClassCastException: class io.hypersistence.utils.hibernate.type.interval.PostgreSQLPeriodType cannot be cast to class org.hibernate.metamodel.model.domain.BasicDomainType (io.hypersistence.utils.hibernate.type.interval.PostgreSQLPeriodType and org.hibernate.metamodel.model.domain.BasicDomainType are in unnamed module of loader com.ibm.ws.classloading.internal.AppClassLoader @656f8f18)

Am I doing something wrong?

@vladmihalcea
Copy link
Owner Author

@mfur I created #609 for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants