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

HHH-18894 Hibernate 6.6 enum literal is considered field literal instead #9342

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,21 @@ public Set<EmbeddableType<?>> getEmbeddables() {

@Override
public EnumJavaType<?> getEnumType(String className) {
return enumJavaTypes.get( className );
final EnumJavaType<?> enumJavaType = enumJavaTypes.get( className );
if ( enumJavaType != null ) {
return enumJavaType;
}
final ClassLoaderService classLoaderService = serviceRegistry.getService( ClassLoaderService.class );
try {
final Class<Object> clazz = classLoaderService.classForName( className );
if ( clazz == null || !clazz.isEnum() ) {
return null;
}
return new EnumJavaType( clazz );
}
catch (ClassLoadingException e) {
throw new RuntimeException( e );
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.query;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Tuple;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

@DomainModel(annotatedClasses = SelectUnknownEnumLiteralTest.Transaction.class)
@SessionFactory
public class SelectUnknownEnumLiteralTest {

@BeforeAll
void setup(SessionFactoryScope scope) {
scope.inTransaction( em -> em.persist( new Transaction( 1L, "abc" ) ) );
}

@AfterAll
void clean(SessionFactoryScope scope) {
scope.inTransaction( em -> em.createMutationQuery( "delete from Tx" ).executeUpdate() );
}

@Test
void test(SessionFactoryScope scope) {
final List<Tuple> tuples = scope.fromSession( em ->
em.createQuery(
"SELECT org.hibernate.orm.test.query.SelectUnknownEnumLiteralTest$Type.TRANSACTION, e.id, e.reference FROM Tx e",
Tuple.class ).getResultList() );
assertEquals( 1, tuples.size() );
assertEquals( Type.TRANSACTION, tuples.get( 0 ).get( 0 ) );
assertEquals( 1L, tuples.get( 0 ).get( 1 ) );
}

@Entity(name = "Tx")
static class Transaction {
@Id
Long id;
String reference;

Transaction() {
}

Transaction(Long id, String reference) {
this.id = id;
this.reference = reference;
}
}

enum Type {
TRANSACTION, DIRECT_DEBIT_GROUP, DIRECT_DEBIT
}
}
Loading