|
| 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.annotations.loader; |
| 8 | + |
| 9 | +import javax.persistence.Column; |
| 10 | +import javax.persistence.Entity; |
| 11 | +import javax.persistence.GeneratedValue; |
| 12 | +import javax.persistence.Id; |
| 13 | + |
| 14 | +import org.hibernate.HibernateException; |
| 15 | +import org.hibernate.annotations.Loader; |
| 16 | +import org.hibernate.annotations.NamedNativeQueries; |
| 17 | +import org.hibernate.annotations.NamedQuery; |
| 18 | +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; |
| 19 | + |
| 20 | +import org.hibernate.testing.util.ExceptionUtil; |
| 21 | +import org.junit.Test; |
| 22 | + |
| 23 | +import static org.junit.Assert.assertTrue; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author Vlad Mihalcea |
| 27 | + */ |
| 28 | +public class LoaderWithInvalidQueryTest extends BaseEntityManagerFunctionalTestCase { |
| 29 | + |
| 30 | + @Override |
| 31 | + protected Class<?>[] getAnnotatedClasses() { |
| 32 | + return new Class<?>[] { |
| 33 | + Person.class |
| 34 | + }; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void buildEntityManagerFactory() throws Exception { |
| 39 | + try { |
| 40 | + super.buildEntityManagerFactory(); |
| 41 | + } |
| 42 | + catch (Exception expected) { |
| 43 | + HibernateException rootCause = (HibernateException) ExceptionUtil.rootCause( expected ); |
| 44 | + assertTrue(rootCause.getMessage().contains( "could not resolve property: valid" )); |
| 45 | + assertTrue(rootCause.getMessage().contains( "_Person is not mapped" )); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void test() { |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + @Entity(name = "Person") |
| 55 | + @Loader(namedQuery = "invalid_sql") |
| 56 | + @NamedQuery( |
| 57 | + name = "invalid_sql", |
| 58 | + query = "SELECT p " + |
| 59 | + "FROM Person p " + |
| 60 | + "WHERE p.id = ? and p.valid = true" |
| 61 | + ) |
| 62 | + @NamedQuery( |
| 63 | + name = "another_invalid_sql", |
| 64 | + query = "SELECT p " + |
| 65 | + "FROM _Person p " + |
| 66 | + "WHERE p.id = ?" |
| 67 | + ) |
| 68 | + public static class Person { |
| 69 | + |
| 70 | + @Id |
| 71 | + @GeneratedValue |
| 72 | + private Long id; |
| 73 | + |
| 74 | + @Column(name = "full_name") |
| 75 | + private String name; |
| 76 | + |
| 77 | + public Long getId() { |
| 78 | + return id; |
| 79 | + } |
| 80 | + |
| 81 | + public void setId(Long id) { |
| 82 | + this.id = id; |
| 83 | + } |
| 84 | + |
| 85 | + public String getName() { |
| 86 | + return name; |
| 87 | + } |
| 88 | + |
| 89 | + public void setName(String name) { |
| 90 | + this.name = name; |
| 91 | + } |
| 92 | + } |
| 93 | + //end::sql-custom-crud-example[] |
| 94 | + |
| 95 | +} |
0 commit comments