How to Fix Hibernate’s token ‘*’ SyntaxException
Hibernate’s JPQL (Java Persistence Query Language) provides an object-oriented abstraction over SQL, allowing us to query entities instead of database tables. However, this abstraction also introduces syntax rules that differ significantly from traditional SQL.
One common pitfall is attempting to use the wildcard selector (SELECT *), which is not supported in JPQL and results in a runtime error such as SyntaxException: token '*', no viable alternative at input. This exception can be confusing, especially for developers transitioning from direct SQL usage to ORM-based querying.
In this article, we examine the root cause of this error, explain how JPQL parsing works, and demonstrate the correct querying patterns.
1. Entity Example
Let’s define a User entity.
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue
private Long id;
@Column
private String name;
@Column
private String email;
public User() {
}
public User(String name, String email) {
this.name = name;
this.email = email;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
}
This User class is a JPA entity mapped to the users database table using the @Entity and @Table annotations. The id field is the primary key and is automatically generated, while name and email are mapped to table columns.
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
<persistence-unit name="demoPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.jcg.demo.User</class>
<properties>
<property name="jakarta.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="jakarta.persistence.jdbc.url" value="jdbc:h2:mem:statelessdb;DB_CLOSE_DELAY=-1"/>
<property name="jakarta.persistence.jdbc.user" value="sa"/>
<property name="jakarta.persistence.jdbc.password" value=""/>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.cj.jdbc.Driver"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
2. The Problem: Using SELECT * in JPQL
Developers transitioning from traditional SQL often attempt to write queries like this:
String jpql = "SELECT * FROM User";
This is invalid JPQL.
JPQL works with entities and aliases, not raw tables and wildcard selectors.
Example That Triggers the Error
public class HibernateSyntaxExceptionDemo {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("demoPU");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(new User("Thomas", "thomas@jcg.com"));
em.persist(new User("Benjamin", "benjamin@jcg.com"));
em.getTransaction().commit();
// WRONG JPQL
String jpql = "SELECT * FROM User";
em.createQuery(jpql, User.class).getResultList();
em.close();
emf.close();
}
}
Runtime Error Output
at java.lang.Thread.run (Thread.java:1474)
Caused by: org.hibernate.query.SyntaxException: At 1:7 and token '*', no viable alternative at input 'SELECT ** FROM User' [SELECT * FROM User]
at org.hibernate.query.hql.internal.StandardHqlTranslator$1.syntaxError (StandardHqlTranslator.java:119)
Why This Happens
JPQL syntax is object-oriented, not SQL-based.
| SQL | JPQL |
|---|---|
SELECT * FROM users | Not allowed |
SELECT u FROM User u | Correct |
| Operates on tables | Operates on entities |
JPQL expects queries to reference an entity name, such as User, along with an alias like u, and requires selecting the alias itself rather than using a wildcard (*).
3. The Correct Way
To write valid JPQL queries, you should reference the entity and assign it an alias, then select the alias instead of using *. This approach aligns with JPQL’s object-oriented design, allowing Hibernate to translate the query into proper SQL while ensuring type safety and clear mapping between your Java entities and database tables.
Select All Users
String jpql = "SELECT u FROM User u";
or shorter:
String jpql = "FROM User u";
Both are valid JPQL.
Fixed Example (Working Code)
public class HibernateSyntaxExceptionDemo {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("demoPU");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(new User("Thomas", "thomas@jcg.com"));
em.persist(new User("Benjamin", "benjamin@jcg.com"));
em.getTransaction().commit();
// CORRECT JPQL
String jpql = "SELECT u FROM User u";
TypedQuery<User> query = em.createQuery(jpql, User.class);
List<User> users = query.getResultList();
// Print results
users.forEach(u -> System.out.println(u.getId() + " - " + u.getName()+ " (" + u.getEmail() + ")"));
em.close();
emf.close();
}
}
Selecting Specific Fields
If you only need certain columns, you can select fields explicitly.
String jpql = "SELECT u.name, u.email FROM User u";
List<Object[]> results = em.createQuery(jpql).getResultList();
for (Object[] row : results) {
System.out.println("Name=" + row[0] + ", Email=" + row[1]);
}
4. Conclusion
The SyntaxException: token '*' , no viable alternative at input error occurs because JPQL does not support wildcard selection like SQL. Hibernate expects us to select entity aliases or specific fields, not *. By switching to valid JPQL syntax such as SELECT u FROM User u, we eliminate the error and make our queries portable, type-safe, and aligned with object-oriented persistence.
5. Download the Source Code
This article discussed how to identify and fix the Hibernate token asterisk ( syntax exception.*)
You can download the full source code of this example here: hibernate syntax exception token asterisk

