HHH-11092 - Add test for issue

This commit is contained in:
Andrea Boriero 2016-09-23 13:36:47 +01:00
parent 306c151fff
commit f8f5486fda
2 changed files with 307 additions and 0 deletions

View File

@ -0,0 +1,194 @@
/*
* 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.jpa.test.query;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Query;
import org.hibernate.Session;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.TestForIssue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertEquals;
/**
* @author Andrea Boriero
*/
@TestForIssue(jiraKey = "HHH-11092")
public class NamedQueryTest extends BaseEntityManagerFunctionalTestCase {
private static final String[] GAME_TITLES = {"Halo", "Grand Theft Auto", "NetHack"};
@Override
public Class[] getAnnotatedClasses() {
return new Class[] {Game.class};
}
@Before
public void setUp()
throws Exception {
doInJPA( this::entityManagerFactory, entityManager -> {
for ( String title : GAME_TITLES ) {
Game game = new Game( title );
entityManager.persist( game );
}
} );
}
@After
public void tearDown() {
doInJPA( this::entityManagerFactory, entityManager -> {
entityManager.createQuery( "delete from Game" ).executeUpdate();
}
);
}
@Test
public void testNamedQueriesOrdinalParametersAreOneBased() {
doInJPA( this::entityManagerFactory, entityManager -> {
Query query = entityManager.createNamedQuery( "NamedQuery" );
query.setParameter( 1, GAME_TITLES[0] );
List list = query.getResultList();
assertEquals( 1, list.size() );
}
);
}
@Test
public void testNamedQueryOrdinalParametersConflict() {
doInJPA( this::entityManagerFactory, entityManager -> {
Query query = entityManager.createNamedQuery( "NamedQuery" );
query.setParameter( 1, GAME_TITLES[0] );
List list = query.getResultList();
assertEquals( 1, list.size() );
final Session session = entityManager.unwrap( Session.class );
final org.hibernate.query.Query sessionQuery = session.createQuery( "select g from Game g where title = ?" );
sessionQuery.setParameter( 0, GAME_TITLES[0] );
list = sessionQuery.getResultList();
query.setParameter( 1, GAME_TITLES[0] );
assertEquals( 1, list.size() );
}
);
}
@Test
public void testNamedQueryOrdinalParametersConflict2() {
doInJPA( this::entityManagerFactory, entityManager -> {
Query query = entityManager.createNamedQuery( "NamedQuery" );
query.setParameter( 1, GAME_TITLES[0] );
List list = query.getResultList();
assertEquals( 1, list.size() );
final Session session = entityManager.unwrap( Session.class );
final org.hibernate.query.Query sessionQuery = session.getNamedQuery( "NamedQuery" );
sessionQuery.setParameter( 0, GAME_TITLES[0] );
list = sessionQuery.getResultList();
query.setParameter( 1, GAME_TITLES[0] );
assertEquals( 1, list.size() );
}
);
}
@Test
public void testNativeNamedQueriesOrdinalParametersAreOneBased() {
doInJPA( this::entityManagerFactory, entityManager -> {
Query query = entityManager.createNamedQuery( "NamedNativeQuery" );
query.setParameter( 1, GAME_TITLES[0] );
List list = query.getResultList();
assertEquals( 1, list.size() );
}
);
}
@Test
public void testNativeNamedQueriesOrdinalParametersConflict() {
doInJPA( this::entityManagerFactory, entityManager -> {
Query query = entityManager.createNamedQuery( "NamedNativeQuery" );
query.setParameter( 1, GAME_TITLES[0] );
List list = query.getResultList();
assertEquals( 1, list.size() );
final Session session = entityManager.unwrap( Session.class );
final org.hibernate.query.Query sessionQuery = session.createSQLQuery(
"select * from Game g where title = ?" );
sessionQuery.setParameter( 0, GAME_TITLES[0] );
list = sessionQuery.getResultList();
query.setParameter( 1, GAME_TITLES[0] );
assertEquals( 1, list.size() );
}
);
}
@Test
public void testNativeNamedQueriesOrdinalParametersConflict2() {
doInJPA( this::entityManagerFactory, entityManager -> {
Query query = entityManager.createNamedQuery( "NamedNativeQuery" );
query.setParameter( 1, GAME_TITLES[0] );
List list = query.getResultList();
assertEquals( 1, list.size() );
final Session session = entityManager.unwrap( Session.class );
final org.hibernate.query.Query sessionQuery = session.getNamedNativeQuery(
"NamedNativeQuery" );
sessionQuery.setParameter( 0, GAME_TITLES[0] );
list = sessionQuery.getResultList();
query.setParameter( 1, GAME_TITLES[0] );
assertEquals( 1, list.size() );
}
);
}
@Entity(name = "Game")
@NamedQueries(@NamedQuery(name = "NamedQuery", query = "select g from Game g where title = ?"))
@NamedNativeQueries(@NamedNativeQuery(name = "NamedNativeQuery", query = "select * from Game g where title = ?"))
public static class Game {
private Long id;
private String title;
public Game() {
}
public Game(String title) {
this.title = title;
}
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
}

View File

@ -0,0 +1,113 @@
/*
* 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.test.annotations.query;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Query;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
/**
* @author Andrea Boriero
*/
public class NamedQueryTest extends BaseCoreFunctionalTestCase {
private static final String[] GAME_TITLES = {"Halo", "Grand Theft Auto", "NetHack"};
@Override
public Class[] getAnnotatedClasses() {
return new Class[] {org.hibernate.jpa.test.query.NamedQueryTest.Game.class};
}
@Before
public void setUp()
throws Exception {
doInHibernate( this::sessionFactory, session -> {
for ( String title : GAME_TITLES ) {
org.hibernate.jpa.test.query.NamedQueryTest.Game game = new org.hibernate.jpa.test.query.NamedQueryTest.Game(
title );
session.save( game );
}
} );
}
@After
public void tearDown() {
doInHibernate( this::sessionFactory, session -> {
session.createQuery( "delete from Game" ).executeUpdate();
}
);
}
@Test
public void testNamedQueriesOrdinalParametersAreZeroBased() {
doInHibernate( this::sessionFactory, session -> {
Query query = session.getNamedQuery( "NamedQuery" );
query.setParameter( 0, GAME_TITLES[0] );
List list = query.getResultList();
assertEquals( 1, list.size() );
}
);
}
@Test
public void testNativeNamedQueriesOrdinalParametersAreZeroBased() {
doInHibernate( this::sessionFactory, session -> {
Query query = session.getNamedNativeQuery( "NamedNativeQuery" );
query.setParameter( 0, GAME_TITLES[0] );
List list = query.getResultList();
assertEquals( 1, list.size() );
}
);
}
@Entity(name = "Game")
@NamedQueries(@NamedQuery(name = "NamedQuery", query = "select g from Game g where title = ?"))
@NamedNativeQueries(@NamedNativeQuery(name = "NamedNativeQuery", query = "select * from Game g where title = ?"))
public static class Game {
private Long id;
private String title;
public Game() {
}
public Game(String title) {
this.title = title;
}
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
}