Skip to content

Commit c434d9a

Browse files
committed
HHH-11500 - Provide the cause of the error when validating @loader named queries
1 parent d80c6ac commit c434d9a

File tree

2 files changed

+103
-3
lines changed

2 files changed

+103
-3
lines changed

hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,16 @@ public void sessionFactoryClosed(SessionFactory factory) {
320320
final Map<String, HibernateException> errors = checkNamedQueries();
321321
if ( !errors.isEmpty() ) {
322322
StringBuilder failingQueries = new StringBuilder( "Errors in named queries: " );
323-
String sep = "";
323+
String separator = System.lineSeparator();
324+
324325
for ( Map.Entry<String, HibernateException> entry : errors.entrySet() ) {
325326
LOG.namedQueryError( entry.getKey(), entry.getValue() );
326-
failingQueries.append( sep ).append( entry.getKey() );
327-
sep = ", ";
327+
328+
failingQueries
329+
.append( separator)
330+
.append( entry.getKey() )
331+
.append( " failed because of: " )
332+
.append( entry.getValue() );
328333
}
329334
throw new HibernateException( failingQueries.toString() );
330335
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)