use of jakarta.persistence.NamedQuery in project hibernate-orm by hibernate.
the class JPAXMLOverriddenAnnotationReader method getNamedQueries.
private NamedQueries getNamedQueries(ManagedType root, XMLContext.Default defaults) {
// TODO avoid the Proxy Creation (@NamedQueries) when possible
List<NamedQuery> queries = root instanceof JaxbEntity ? buildNamedQueries(((JaxbEntity) root).getNamedQuery(), defaults, classLoaderAccess) : new ArrayList<>();
if (defaults.canUseJavaAnnotations()) {
NamedQuery annotation = getPhysicalAnnotation(NamedQuery.class);
addNamedQueryIfNeeded(annotation, queries);
NamedQueries annotations = getPhysicalAnnotation(NamedQueries.class);
if (annotations != null) {
for (NamedQuery current : annotations.value()) {
addNamedQueryIfNeeded(current, queries);
}
}
}
if (queries.size() > 0) {
AnnotationDescriptor ad = new AnnotationDescriptor(NamedQueries.class);
ad.setValue("value", queries.toArray(new NamedQuery[queries.size()]));
return AnnotationFactory.create(ad);
} else {
return null;
}
}
use of jakarta.persistence.NamedQuery in project hibernate-orm by hibernate.
the class JPAXMLOverriddenAnnotationReader method addNamedQueryIfNeeded.
private void addNamedQueryIfNeeded(NamedQuery annotation, List<NamedQuery> queries) {
if (annotation != null) {
String queryName = annotation.name();
boolean present = false;
for (NamedQuery current : queries) {
if (current.name().equals(queryName)) {
present = true;
break;
}
}
if (!present) {
queries.add(annotation);
}
}
}
use of jakarta.persistence.NamedQuery in project hibernate-orm by hibernate.
the class JPAXMLOverriddenAnnotationReader method buildNamedQueries.
public static List<NamedQuery> buildNamedQueries(List<JaxbNamedQuery> elements, XMLContext.Default defaults, ClassLoaderAccess classLoaderAccess) {
List<NamedQuery> namedQueries = new ArrayList<>();
for (JaxbNamedQuery element : elements) {
AnnotationDescriptor ann = new AnnotationDescriptor(NamedQuery.class);
copyAttribute(ann, "name", element.getName(), false);
copyAttribute(ann, "query", element.getQuery(), true);
buildQueryHints(element.getHint(), ann);
copyAttribute(ann, "lock-mode", element.getLockMode(), false);
namedQueries.add(AnnotationFactory.create(ann));
}
return namedQueries;
}
use of jakarta.persistence.NamedQuery in project hibernate-orm by hibernate.
the class NamedQueryTest method testNamedQueriesOrdinalParametersAreOneBased.
@Test
public void testNamedQueriesOrdinalParametersAreOneBased(SessionFactoryScope scope) {
scope.inTransaction(session -> {
Query query = session.getNamedQuery("NamedQuery");
query.setParameter(1, GAME_TITLES[0]);
List list = query.getResultList();
assertEquals(1, list.size());
});
}
use of jakarta.persistence.NamedQuery in project hibernate-orm by hibernate.
the class JPAXMLOverriddenMetadataProvider method getDefaults.
@Override
public Map<Object, Object> getDefaults() {
if (!xmlMappingEnabled) {
return Collections.emptyMap();
} else {
if (defaults == null) {
defaults = new HashMap<>();
XMLContext.Default xmlDefaults = xmlContext.getDefaultWithGlobalCatalogAndSchema();
defaults.put("schema", xmlDefaults.getSchema());
defaults.put("catalog", xmlDefaults.getCatalog());
defaults.put("delimited-identifier", xmlDefaults.getDelimitedIdentifier());
defaults.put("cascade-persist", xmlDefaults.getCascadePersist());
List<Class> entityListeners = new ArrayList<Class>();
for (String className : xmlContext.getDefaultEntityListeners()) {
try {
entityListeners.add(classLoaderAccess.classForName(className));
} catch (ClassLoadingException e) {
throw new IllegalStateException("Default entity listener class not found: " + className);
}
}
defaults.put(EntityListeners.class, entityListeners);
for (JaxbEntityMappings entityMappings : xmlContext.getAllDocuments()) {
List<JaxbSequenceGenerator> jaxbSequenceGenerators = entityMappings.getSequenceGenerator();
List<SequenceGenerator> sequenceGenerators = (List<SequenceGenerator>) defaults.get(SequenceGenerator.class);
if (sequenceGenerators == null) {
sequenceGenerators = new ArrayList<>();
defaults.put(SequenceGenerator.class, sequenceGenerators);
}
for (JaxbSequenceGenerator element : jaxbSequenceGenerators) {
sequenceGenerators.add(JPAXMLOverriddenAnnotationReader.buildSequenceGeneratorAnnotation(element));
}
List<JaxbTableGenerator> jaxbTableGenerators = entityMappings.getTableGenerator();
List<TableGenerator> tableGenerators = (List<TableGenerator>) defaults.get(TableGenerator.class);
if (tableGenerators == null) {
tableGenerators = new ArrayList<>();
defaults.put(TableGenerator.class, tableGenerators);
}
for (JaxbTableGenerator element : jaxbTableGenerators) {
tableGenerators.add(JPAXMLOverriddenAnnotationReader.buildTableGeneratorAnnotation(element, xmlDefaults));
}
List<NamedQuery> namedQueries = (List<NamedQuery>) defaults.get(NamedQuery.class);
if (namedQueries == null) {
namedQueries = new ArrayList<>();
defaults.put(NamedQuery.class, namedQueries);
}
List<NamedQuery> currentNamedQueries = JPAXMLOverriddenAnnotationReader.buildNamedQueries(entityMappings.getNamedQuery(), xmlDefaults, classLoaderAccess);
namedQueries.addAll(currentNamedQueries);
List<NamedNativeQuery> namedNativeQueries = (List<NamedNativeQuery>) defaults.get(NamedNativeQuery.class);
if (namedNativeQueries == null) {
namedNativeQueries = new ArrayList<>();
defaults.put(NamedNativeQuery.class, namedNativeQueries);
}
List<NamedNativeQuery> currentNamedNativeQueries = JPAXMLOverriddenAnnotationReader.buildNamedNativeQueries(entityMappings.getNamedNativeQuery(), xmlDefaults, classLoaderAccess);
namedNativeQueries.addAll(currentNamedNativeQueries);
List<SqlResultSetMapping> sqlResultSetMappings = (List<SqlResultSetMapping>) defaults.get(SqlResultSetMapping.class);
if (sqlResultSetMappings == null) {
sqlResultSetMappings = new ArrayList<>();
defaults.put(SqlResultSetMapping.class, sqlResultSetMappings);
}
List<SqlResultSetMapping> currentSqlResultSetMappings = JPAXMLOverriddenAnnotationReader.buildSqlResultsetMappings(entityMappings.getSqlResultSetMapping(), xmlDefaults, classLoaderAccess);
sqlResultSetMappings.addAll(currentSqlResultSetMappings);
List<NamedStoredProcedureQuery> namedStoredProcedureQueries = (List<NamedStoredProcedureQuery>) defaults.get(NamedStoredProcedureQuery.class);
if (namedStoredProcedureQueries == null) {
namedStoredProcedureQueries = new ArrayList<>();
defaults.put(NamedStoredProcedureQuery.class, namedStoredProcedureQueries);
}
List<NamedStoredProcedureQuery> currentNamedStoredProcedureQueries = JPAXMLOverriddenAnnotationReader.buildNamedStoreProcedureQueries(entityMappings.getNamedStoredProcedureQuery(), xmlDefaults, classLoaderAccess);
namedStoredProcedureQueries.addAll(currentNamedStoredProcedureQueries);
}
}
return defaults;
}
}
Aggregations