Skip to content

Commit 164e4c9

Browse files
committed
BAEL-9408: Resolving Hibernate SyntaxException: token '*', no viable alternative
1 parent 03cf67b commit 164e4c9

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.hibernate.syntaxexception;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.hibernate.SessionFactory;
7+
import org.hibernate.boot.Metadata;
8+
import org.hibernate.boot.MetadataSources;
9+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
10+
import org.hibernate.service.ServiceRegistry;
11+
12+
public class HibernateUtil {
13+
private static SessionFactory sessionFactory;
14+
15+
public static SessionFactory getSessionFactory() {
16+
if (sessionFactory == null) {
17+
Map<String, Object> settings = new HashMap<>();
18+
settings.put("hibernate.connection.driver_class", "org.h2.Driver");
19+
settings.put("hibernate.connection.url", "jdbc:h2:mem:test");
20+
settings.put("hibernate.connection.username", "sa");
21+
settings.put("hibernate.connection.password", "");
22+
settings.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
23+
settings.put("hibernate.show_sql", "true");
24+
settings.put("hibernate.hbm2ddl.auto", "update");
25+
26+
ServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().applySettings(settings)
27+
.build();
28+
29+
Metadata metadata = new MetadataSources(standardRegistry).addAnnotatedClass(Person.class)
30+
.getMetadataBuilder()
31+
.build();
32+
33+
sessionFactory = metadata.getSessionFactoryBuilder()
34+
.build();
35+
}
36+
37+
return sessionFactory;
38+
}
39+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.hibernate.syntaxexception;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.Id;
5+
6+
@Entity
7+
public class Person {
8+
9+
@Id
10+
private int id;
11+
private String firstName;
12+
private String lastName;
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.hibernate.syntaxexception;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
5+
6+
import org.hibernate.Session;
7+
import org.hibernate.query.SyntaxException;
8+
import org.junit.jupiter.api.AfterAll;
9+
import org.junit.jupiter.api.BeforeAll;
10+
import org.junit.jupiter.api.Test;
11+
12+
import com.baeldung.hibernate.exception.persistentobject.HibernateUtil;
13+
14+
class SyntaxExceptionUnitTest {
15+
16+
private static Session session;
17+
18+
@BeforeAll
19+
static void beforeAll() {
20+
session = HibernateUtil.getSessionFactory()
21+
.openSession();
22+
session.beginTransaction();
23+
}
24+
25+
@AfterAll
26+
static void afterAll() {
27+
session.close();
28+
}
29+
30+
@Test
31+
void whenUsingInvalidHQLSyntax_thenThrowSyntaxException() {
32+
assertThatThrownBy(() -> {
33+
session.createQuery("SELECT * FROM Person p", Person.class)
34+
.list();
35+
}).hasRootCauseInstanceOf(SyntaxException.class)
36+
.hasMessageContaining("token '*', no viable alternative");
37+
}
38+
39+
@Test
40+
void whenUsingValidHQLSyntax_thenCorrect() {
41+
assertThat(session.createQuery("SELECT p FROM Person p", Person.class)
42+
.list()).isEmpty();
43+
}
44+
45+
}

0 commit comments

Comments
 (0)