HHH-18192 remove @Loader

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-06-15 21:47:50 +02:00 committed by Steve Ebersole
parent cc272f704e
commit 5d91c89057
17 changed files with 31 additions and 227 deletions

View File

@ -36,9 +36,6 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
* @author Gavin King
*
* @since 6.2
*
* @implNote This annotation is just an abbreviation for {@link Loader}
* together with {@link NamedQuery}.
*/
@Target({TYPE, FIELD, METHOD})
@Retention(RUNTIME)

View File

@ -1,33 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Specifies that a named query should be used to load an entity or
* collection, overriding the SQL that Hibernate generates by default.
*
* @author László Benke
*
* @deprecated Use {@link SQLSelect} or {@link HQLSelect}.
*/
@Target( {TYPE, FIELD, METHOD} )
@Retention( RUNTIME )
@Deprecated(since = "6.2")
public @interface Loader {
/**
* The named query to use for loading the entity or collection.
*/
String namedQuery() default "";
}

View File

@ -56,9 +56,6 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
* @author Gavin King
*
* @since 6.2
*
* @implNote This annotation is just an abbreviation for {@link Loader}
* together with {@link NamedNativeQuery}.
*/
@Target({TYPE, FIELD, METHOD})
@Retention(RUNTIME)

View File

@ -46,7 +46,6 @@ import org.hibernate.annotations.ListIndexBase;
import org.hibernate.annotations.ListIndexJavaType;
import org.hibernate.annotations.ListIndexJdbcType;
import org.hibernate.annotations.ListIndexJdbcTypeCode;
import org.hibernate.annotations.Loader;
import org.hibernate.annotations.ManyToAny;
import org.hibernate.annotations.MapKeyJavaType;
import org.hibernate.annotations.MapKeyJdbcType;
@ -1414,11 +1413,6 @@ public abstract class CollectionBinder {
collection.setLoaderName( loaderName );
QueryBinder.bindQuery( loaderName, hqlSelect, buildingContext );
}
final Loader loader = property.getDirectAnnotationUsage( Loader.class );
if ( loader != null ) {
collection.setLoaderName( loader.namedQuery() );
}
}
private void applySortingAndOrdering() {

View File

@ -32,7 +32,6 @@ import org.hibernate.annotations.Filter;
import org.hibernate.annotations.Filters;
import org.hibernate.annotations.HQLSelect;
import org.hibernate.annotations.Immutable;
import org.hibernate.annotations.Loader;
import org.hibernate.annotations.Mutability;
import org.hibernate.annotations.NaturalIdCache;
import org.hibernate.annotations.OnDelete;
@ -1348,8 +1347,6 @@ public class EntityBinder {
ensureNoMutabilityPlan();
bindCustomLoader();
registerImportName();
processNamedEntityGraphs();
@ -1466,13 +1463,6 @@ public class EntityBinder {
}
}
private void bindCustomLoader() {
final Loader loader = annotatedClass.getAnnotationUsage( Loader.class, getSourceModelContext() );
if ( loader != null ) {
persistentClass.setLoaderName( loader.namedQuery() );
}
}
private void bindSubselect() {
final Subselect subselect = annotatedClass.getAnnotationUsage( Subselect.class, getSourceModelContext() );
if ( subselect != null ) {

View File

@ -402,10 +402,6 @@ public interface HibernateAnnotations {
ListIndexJdbcTypeCode.class,
ListIndexJdbcTypeCodeAnnotation.class
);
OrmAnnotationDescriptor<Loader, LoaderAnnotation> LOADER = new OrmAnnotationDescriptor<>(
Loader.class,
LoaderAnnotation.class
);
OrmAnnotationDescriptor<ManyToAny, ManyToAnyAnnotation> MANY_TO_ANY = new OrmAnnotationDescriptor<>(
ManyToAny.class,
ManyToAnyAnnotation.class

View File

@ -1,60 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.boot.models.annotations.internal;
import java.lang.annotation.Annotation;
import org.hibernate.annotations.Loader;
import org.hibernate.boot.models.HibernateAnnotations;
import org.hibernate.models.spi.SourceModelBuildingContext;
import org.jboss.jandex.AnnotationInstance;
import static org.hibernate.boot.models.internal.OrmAnnotationHelper.extractJandexValue;
@SuppressWarnings({ "ClassExplicitlyAnnotation", "unused" })
@jakarta.annotation.Generated("org.hibernate.orm.build.annotations.ClassGeneratorProcessor")
public class LoaderAnnotation implements Loader {
private String namedQuery;
/**
* Used in creating dynamic annotation instances (e.g. from XML)
*/
public LoaderAnnotation(SourceModelBuildingContext modelContext) {
this.namedQuery = "";
}
/**
* Used in creating annotation instances from JDK variant
*/
public LoaderAnnotation(Loader annotation, SourceModelBuildingContext modelContext) {
this.namedQuery = annotation.namedQuery();
}
/**
* Used in creating annotation instances from Jandex variant
*/
public LoaderAnnotation(AnnotationInstance annotation, SourceModelBuildingContext modelContext) {
this.namedQuery = extractJandexValue( annotation, HibernateAnnotations.LOADER, "namedQuery", modelContext );
}
@Override
public Class<? extends Annotation> annotationType() {
return Loader.class;
}
@Override
public String namedQuery() {
return namedQuery;
}
public void namedQuery(String value) {
this.namedQuery = value;
}
}

View File

@ -24,12 +24,6 @@ import org.junit.Test;
* @author Emmanuel Bernard
*/
public class LoaderTest extends BaseCoreFunctionalTestCase {
@Override
protected String[] getOrmXmlFiles() {
return new String[] {
"org/hibernate/orm/test/annotations/loader/Loader.hbm.xml"
};
}
@Override
protected Class[] getAnnotatedClasses() {

View File

@ -12,7 +12,7 @@ import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import org.hibernate.HibernateException;
import org.hibernate.annotations.Loader;
import org.hibernate.annotations.HQLSelect;
import org.hibernate.annotations.NamedQuery;
import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase;
@ -43,8 +43,8 @@ public class LoaderWithInvalidQueryTest extends BaseEntityManagerFunctionalTestC
HibernateException rootCause = (HibernateException) ExceptionUtil.rootCause( expected );
Throwable[] suppressed = rootCause.getSuppressed();
assertEquals( 2, suppressed.length );
assertTrue( ExceptionUtil.rootCause( suppressed[0] ).getMessage().contains( "Could not resolve attribute 'valid'" ) );
assertTrue( ExceptionUtil.rootCause( suppressed[1] ).getMessage().contains( "Could not resolve root entity '_Person'" ) );
assertTrue( ExceptionUtil.rootCause( suppressed[0] ).getMessage().contains( "Could not resolve root entity '_Person'" ) );
assertTrue( ExceptionUtil.rootCause( suppressed[1] ).getMessage().contains( "Could not resolve attribute 'valid'" ) );
}
}
@ -54,9 +54,7 @@ public class LoaderWithInvalidQueryTest extends BaseEntityManagerFunctionalTestC
@Entity(name = "Person")
@Loader(namedQuery = "invalid_sql")
@NamedQuery(
name = "invalid_sql",
@HQLSelect(
query = "SELECT p " +
"FROM Person p " +
"WHERE p.id = ?1 and p.valid = true"

View File

@ -8,14 +8,16 @@ package org.hibernate.orm.test.annotations.loader;
import java.util.HashSet;
import java.util.Set;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityResult;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.SqlResultSetMapping;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.Loader;
import org.hibernate.annotations.SQLSelect;
@Entity
public class Team {
@ -34,7 +36,9 @@ public class Team {
@OneToMany(targetEntity = Player.class, mappedBy = "team", fetch = FetchType.EAGER)
@Fetch(FetchMode.SELECT)
@Loader(namedQuery = "loadByTeam")
@SQLSelect(sql = "select * from Player where team_id = ?1",
resultSetMapping = @SqlResultSetMapping(name = "",
entities = @EntityResult(entityClass = Player.class)))
public Set<Player> getPlayers() {
return players;
}

View File

@ -13,14 +13,13 @@ import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.NamedNativeQuery;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import org.hibernate.annotations.Loader;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLDeleteAll;
import org.hibernate.annotations.SQLInsert;
import org.hibernate.annotations.SQLSelect;
import org.hibernate.annotations.SQLUpdate;
/**
@ -31,8 +30,7 @@ import org.hibernate.annotations.SQLUpdate;
@SQLInsert( sql="INSERT INTO CHAOS(name, nick_name, chaos_size, id) VALUES(upper(?),?,?,?)")
@SQLUpdate( sql="UPDATE CHAOS SET name = upper(?), nick_name = ?, chaos_size = ? WHERE id = ?")
@SQLDelete( sql="DELETE CHAOS WHERE id = ?")
@Loader(namedQuery = "chaos")
@NamedNativeQuery(name="chaos", query="select id, chaos_size, name, lower( nick_name ) as nick_name from CHAOS where id= ?", resultClass = Chaos.class)
@SQLSelect( sql="select id, chaos_size, name, lower( nick_name ) as nick_name from CHAOS where id= ?" )
public class Chaos {
@Id
private Long id;

View File

@ -6,11 +6,10 @@
*/
package org.hibernate.orm.test.persister.entity;
import org.hibernate.annotations.Loader;
import org.hibernate.annotations.NamedNativeQuery;
import org.hibernate.annotations.ResultCheckStyle;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLInsert;
import org.hibernate.annotations.SQLSelect;
import org.hibernate.annotations.SQLUpdate;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.persister.entity.AbstractEntityPersister;
@ -87,12 +86,7 @@ public class CustomSqlSchemaResolvingIdentityTest {
}
@Entity(name = "CardWithCustomSQL")
@Loader(namedQuery = "find_foo_by_id")
@NamedNativeQuery(
name = "find_foo_by_id",
query = "SELECT id, name FROM {h-schema}FOO WHERE id = ?",
resultClass = CustomEntity.class
)
@SQLSelect(sql = "SELECT id, name FROM {h-schema}FOO WHERE id = ?")
@SQLInsert(sql = "INSERT INTO {h-schema}FOO (name) VALUES (?)")
@SQLDelete(sql = "DELETE FROM {h-schema}FOO WHERE id = ?", check = ResultCheckStyle.COUNT)
@SQLUpdate(sql = "UPDATE {h-schema}FOO SET name = ? WHERE id = ? ")

View File

@ -6,11 +6,10 @@
*/
package org.hibernate.orm.test.persister.entity;
import org.hibernate.annotations.Loader;
import org.hibernate.annotations.NamedNativeQuery;
import org.hibernate.annotations.ResultCheckStyle;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLInsert;
import org.hibernate.annotations.SQLSelect;
import org.hibernate.annotations.SQLUpdate;
import org.hibernate.persister.entity.AbstractEntityPersister;
import org.hibernate.sql.model.jdbc.JdbcMutationOperation;
@ -81,12 +80,7 @@ public class CustomSqlSchemaResolvingTest {
}
@Entity(name = "CardWithCustomSQL")
@Loader(namedQuery = "find_foo_by_id")
@NamedNativeQuery(
name = "find_foo_by_id",
query = "SELECT id, name FROM {h-schema}FOO WHERE id = ?",
resultClass = CustomEntity.class
)
@SQLSelect(sql = "SELECT id, name FROM {h-schema}FOO WHERE id = ?")
@SQLInsert(sql = "INSERT INTO {h-schema}FOO (name, id) VALUES (?, ?)")
@SQLDelete(sql = "DELETE FROM {h-schema}FOO WHERE id = ?", check = ResultCheckStyle.COUNT)
@SQLUpdate(sql = "UPDATE {h-schema}FOO SET name = ? WHERE id = ? ")

View File

@ -16,12 +16,11 @@ import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.NamedNativeQuery;
import org.hibernate.annotations.Loader;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLDeleteAll;
import org.hibernate.annotations.SQLInsert;
import org.hibernate.annotations.SQLSelect;
import org.hibernate.annotations.SQLUpdate;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.dialect.PostgreSQLDialect;
@ -114,20 +113,7 @@ public class CollectionLoaderTest extends BaseEntityManagerFunctionalTestCase {
@SQLInsert(sql = "INSERT INTO person (name, id, valid) VALUES (?, ?, true) ", check = COUNT)
@SQLUpdate(sql = "UPDATE person SET name = ? where id = ? ")
@SQLDelete(sql = "UPDATE person SET valid = false WHERE id = ? ")
@Loader(namedQuery = "find_valid_person")
@NamedNativeQuery(
name = "find_valid_person",
query = "SELECT id, name " +
"FROM person " +
"WHERE id = ? and valid = true",
resultClass = Person.class
)
@NamedNativeQuery(
name = "find_valid_phones",
query = "SELECT phones " +
"FROM Person_phones " +
"WHERE person_id = ? and valid = true "
)
@SQLSelect(sql = "SELECT id, name FROM person WHERE id = ? and valid = true")
public static class Person {
@Id
@ -139,7 +125,7 @@ public class CollectionLoaderTest extends BaseEntityManagerFunctionalTestCase {
@ElementCollection
@SQLInsert(sql = "INSERT INTO person_phones (person_id, phones, valid) VALUES (?, ?, true) ")
@SQLDeleteAll(sql = "UPDATE person_phones SET valid = false WHERE person_id = ?")
@Loader(namedQuery = "find_valid_phones")
@SQLSelect(sql = "SELECT phones FROM Person_phones WHERE person_id = ? and valid = true")
private List<String> phones = new ArrayList<>();
public Long getId() {

View File

@ -11,17 +11,15 @@ import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.NamedNativeQueries;
import jakarta.persistence.NamedNativeQuery;
import jakarta.persistence.PrimaryKeyJoinColumn;
import jakarta.persistence.SecondaryTable;
import jakarta.persistence.Table;
import org.hibernate.Session;
import org.hibernate.annotations.Loader;
import org.hibernate.annotations.ResultCheckStyle;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLInsert;
import org.hibernate.annotations.SQLSelect;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase;
@ -108,20 +106,15 @@ public class CustomSQLSecondaryTableTest extends BaseEntityManagerFunctionalTest
sql = "UPDATE person_details SET valid = false WHERE person_id = ? "
)
@Loader(namedQuery = "find_valid_person")
@NamedNativeQueries({
@NamedNativeQuery(
name = "find_valid_person",
query = "SELECT " +
@SQLSelect(
sql = "SELECT " +
" p.id, " +
" p.name, " +
" pd.image " +
"FROM person p " +
"LEFT OUTER JOIN person_details pd ON p.id = pd.person_id " +
"WHERE p.id = ? AND p.valid = true AND pd.valid = true",
resultClass = Person.class
"WHERE p.id = ? AND p.valid = true AND pd.valid = true"
)
})
public static class Person {
@Id

View File

@ -9,10 +9,10 @@ package org.hibernate.orm.test.sql;
import java.sql.Statement;
import org.hibernate.Session;
import org.hibernate.annotations.Loader;
import org.hibernate.annotations.ResultCheckStyle;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLInsert;
import org.hibernate.annotations.SQLSelect;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
@ -24,8 +24,6 @@ import org.junit.jupiter.api.Test;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.NamedNativeQueries;
import jakarta.persistence.NamedNativeQuery;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
@ -98,16 +96,7 @@ public class OracleCustomSQLWithStoredProcedureTest {
callable = true
)
//end::sql-sp-custom-crud-example[]
@Loader(namedQuery = "find_valid_person")
@NamedNativeQueries({
@NamedNativeQuery(
name = "find_valid_person",
query = "SELECT id, name " +
"FROM person " +
"WHERE id = ? and valid = 1",
resultClass = Person.class
)
})
@SQLSelect(sql = "SELECT id, name FROM person WHERE id = ? and valid = 1")
public static class Person {
@Id

View File

@ -1,27 +0,0 @@
<?xml version="1.0"?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--
Used to demonstrate the declarative configuration
of both hbm files and annotated classes
See hibernate.cfg.xml and ConfigurationTest
-->
<hibernate-mapping package="org.hibernate.orm.test.annotations.loader">
<sql-query name="loadByTeam">
<load-collection alias="p" role="Team.players"/>
select {p.*} from Player p where p.team_id = ?1
</sql-query>
</hibernate-mapping>