|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.test.comments; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | +import javax.persistence.EntityManager; |
| 12 | +import javax.persistence.TypedQuery; |
| 13 | +import javax.persistence.criteria.CompoundSelection; |
| 14 | +import javax.persistence.criteria.CriteriaBuilder; |
| 15 | +import javax.persistence.criteria.CriteriaQuery; |
| 16 | +import javax.persistence.criteria.Path; |
| 17 | +import javax.persistence.criteria.Root; |
| 18 | + |
| 19 | +import org.hibernate.cfg.AvailableSettings; |
| 20 | +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; |
| 21 | + |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +import static org.hamcrest.CoreMatchers.is; |
| 26 | +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; |
| 27 | +import static org.junit.Assert.assertThat; |
| 28 | + |
| 29 | +/** |
| 30 | + * @author Andrea Boriero |
| 31 | + */ |
| 32 | +public class UseSqlCommentTest extends BaseEntityManagerFunctionalTestCase { |
| 33 | + |
| 34 | + @Override |
| 35 | + protected Class<?>[] getAnnotatedClasses() { |
| 36 | + return new Class[] { TestEntity.class, TestEntity2.class }; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + protected void addMappings(Map settings) { |
| 41 | + settings.put( AvailableSettings.USE_SQL_COMMENTS, "true" ); |
| 42 | + settings.put( AvailableSettings.FORMAT_SQL, "false" ); |
| 43 | + } |
| 44 | + |
| 45 | + @Before |
| 46 | + public void setUp() { |
| 47 | + doInJPA( this::entityManagerFactory, entityManager -> { |
| 48 | + TestEntity testEntity = new TestEntity(); |
| 49 | + testEntity.setId( "test1" ); |
| 50 | + testEntity.setValue( "value1" ); |
| 51 | + entityManager.persist( testEntity ); |
| 52 | + |
| 53 | + TestEntity2 testEntity2 = new TestEntity2(); |
| 54 | + testEntity2.setId( "test2" ); |
| 55 | + testEntity2.setValue( "value2" ); |
| 56 | + entityManager.persist( testEntity2 ); |
| 57 | + } ); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testIt() { |
| 62 | + String appendLiteral = "*/select id as col_0_0_,value as col_1_0_ from testEntity2 where 1=1 or id=?--/*"; |
| 63 | + doInJPA( this::entityManagerFactory, entityManager -> { |
| 64 | + |
| 65 | + List<TestEntity> result = findUsingQuery( "test1", appendLiteral, entityManager ); |
| 66 | + |
| 67 | + TestEntity test1 = result.get( 0 ); |
| 68 | + assertThat( test1.getValue(), is( appendLiteral ) ); |
| 69 | + } ); |
| 70 | + |
| 71 | + doInJPA( this::entityManagerFactory, entityManager -> { |
| 72 | + |
| 73 | + List<TestEntity> result = findUsingCriteria( "test1", appendLiteral, entityManager ); |
| 74 | + |
| 75 | + TestEntity test1 = result.get( 0 ); |
| 76 | + assertThat( test1.getValue(), is( appendLiteral ) ); |
| 77 | + } ); |
| 78 | + } |
| 79 | + |
| 80 | + public List<TestEntity> findUsingCriteria(String id, String appendLiteral, EntityManager entityManager) { |
| 81 | + CriteriaBuilder builder = entityManager.getCriteriaBuilder(); |
| 82 | + CriteriaQuery<TestEntity> criteria = builder.createQuery( TestEntity.class ); |
| 83 | + Root<TestEntity> root = criteria.from( TestEntity.class ); |
| 84 | + |
| 85 | + Path<Object> idPath = root.get( "id" ); |
| 86 | + CompoundSelection<TestEntity> selection = builder.construct( |
| 87 | + TestEntity.class, |
| 88 | + idPath, |
| 89 | + builder.literal( appendLiteral ) |
| 90 | + ); |
| 91 | + criteria.select( selection ); |
| 92 | + |
| 93 | + criteria.where( builder.equal( idPath, builder.parameter( String.class, "where_id" ) ) ); |
| 94 | + |
| 95 | + TypedQuery<TestEntity> query = entityManager.createQuery( criteria ); |
| 96 | + query.setParameter( "where_id", id ); |
| 97 | + return query.getResultList(); |
| 98 | + } |
| 99 | + |
| 100 | + public List<TestEntity> findUsingQuery(String id, String appendLiteral, EntityManager entityManager) { |
| 101 | + TypedQuery<TestEntity> query = |
| 102 | + entityManager.createQuery( |
| 103 | + "select new org.hibernate.test.comments.TestEntity(id, '" |
| 104 | + + appendLiteral.replace( "'", "''" ) |
| 105 | + + "') from TestEntity where id=:where_id", |
| 106 | + TestEntity.class |
| 107 | + ); |
| 108 | + query.setParameter( "where_id", id ); |
| 109 | + return query.getResultList(); |
| 110 | + } |
| 111 | +} |
0 commit comments