Skip to content

Commit

Permalink
Verified @NamedQuery support
Browse files Browse the repository at this point in the history
  • Loading branch information
sebersole committed Apr 28, 2020
1 parent 9337887 commit 75bfc09
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ protected boolean applyNativeQueryLockMode(Object value) {
protected void collectHints(Map<String, Object> hints) {
super.collectHints( hints );

if ( queryOptions.getAppliedGraph() != null ) {
if ( queryOptions.getAppliedGraph() != null && queryOptions.getAppliedGraph().getSemantic() != null ) {
hints.put(
queryOptions.getAppliedGraph().getSemantic().getJpaHintName(),
queryOptions.getAppliedGraph().getGraph()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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
*/

/**
* @author Steve Ebersole
*/
package org.hibernate.orm.test.query.named;
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.orm.test.query.named.simple;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQuery;

/**
* @author Steve Ebersole
*/
@Entity
@NamedQuery(
name = "simple",
query = "select e from SimpleEntityWithNamedQueries e"
)
@NamedQuery(
name = "restricted",
query = "select e from SimpleEntityWithNamedQueries e where e.name = :name"
)
public class SimpleEntityWithNamedQueries {
@Id
private Integer id;
private String name;

SimpleEntityWithNamedQueries() {
}

public SimpleEntityWithNamedQueries(Integer id, String name) {
this.id = id;
this.name = name;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* 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.orm.test.query.named.simple;

import javax.persistence.FlushModeType;

import org.hibernate.CacheMode;
import org.hibernate.query.Query;
import org.hibernate.query.hql.spi.NamedHqlQueryMemento;
import org.hibernate.query.named.NamedQueryRepository;
import org.hibernate.query.spi.QueryImplementor;

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.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;

/**
* @author Steve Ebersole
*/
@DomainModel( annotatedClasses = SimpleEntityWithNamedQueries.class )
@SessionFactory
public class SimpleNamedQueryTests {
@Test
public void testBinding(SessionFactoryScope scope) {
final NamedQueryRepository namedQueryRepository = scope.getSessionFactory()
.getQueryEngine()
.getNamedQueryRepository();

final NamedHqlQueryMemento simpleMemento = namedQueryRepository.getHqlQueryMemento( "simple" );
assertThat( simpleMemento, notNullValue() );

final NamedHqlQueryMemento restrictedMemento = namedQueryRepository.getHqlQueryMemento( "restricted" );
assertThat( restrictedMemento, notNullValue() );
}

@Test
public void testExecution(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createNamedQuery( "simple" ).list();
session.createNamedQuery( "restricted" ).setParameter( "name", "a name" ).list();
}
);
final NamedQueryRepository namedQueryRepository = scope.getSessionFactory()
.getQueryEngine()
.getNamedQueryRepository();

final NamedHqlQueryMemento simpleMemento = namedQueryRepository.getHqlQueryMemento( "simple" );
assertThat( simpleMemento, notNullValue() );

final NamedHqlQueryMemento restrictedMemento = namedQueryRepository.getHqlQueryMemento( "restricted" );
assertThat( restrictedMemento, notNullValue() );
}

@Test
public void testStoring(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
final String qryString = "select e from SimpleEntityWithNamedQueries e where e.id = :id";
final Query<SimpleEntityWithNamedQueries> query = session.createQuery( qryString, SimpleEntityWithNamedQueries.class );
session.getSessionFactory().addNamedQuery( "byId", query );
}
);

scope.inTransaction(
session -> {
session.createNamedQuery( "byId" ).setParameter( "id", 1 ).list();
}
);
}

@Test
public void testOptions(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
final String qryString = "select e from SimpleEntityWithNamedQueries e where e.id = :id";
final Query<SimpleEntityWithNamedQueries> query = session.createQuery( qryString, SimpleEntityWithNamedQueries.class );
query.setFetchSize( 20 );
query.setFirstResult( 20 );
query.setMaxResults( 20 );
query.setFlushMode( FlushModeType.COMMIT );
query.setCacheMode( CacheMode.IGNORE );
query.setCacheRegion( "custom-region" );
session.getSessionFactory().addNamedQuery( "options", query );
}
);

scope.inTransaction(
session -> {
final QueryImplementor<SimpleEntityWithNamedQueries> query = session.createNamedQuery( "options", SimpleEntityWithNamedQueries.class );
assertThat( query.getFetchSize(), is( 20 ) );
assertThat( query.getFirstResult(), is( 20 ) );
assertThat( query.getMaxResults(), is( 20 ) );
assertThat( query.getFlushMode(), is( FlushModeType.COMMIT ) );
assertThat( query.getCacheMode(), is( CacheMode.IGNORE ) );
assertThat( query.getCacheRegion(), is( "custom-region" ) );
}
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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
*/

/**
* @author Steve Ebersole
*/
package org.hibernate.orm.test.query.named.simple;

0 comments on commit 75bfc09

Please sign in to comment.