Skip to main content

On This Page

Solving Hibernate SyntaxException: token ‘*’, no viable alternative at input

2 min read
Share

These articles are AI-generated summaries. Please check the original sources for full details.

Solving Hibernate SyntaxException: token ‘*’, no viable alternative at input

Hibernate throws a SyntaxException when developers use SELECT * in HQL queries, as HQL interprets * as invalid. This error occurs because HQL operates on entities, not database tables.

Why This Matters

HQL (Hibernate Query Language) is object-oriented, unlike SQL, which works with tables and columns. The * operator, valid in SQL for selecting all columns, has no equivalent in HQL. Misusing it leads to parsing failures, with costs ranging from debugging delays to production query errors. The 2025-12-07 Baeldung article highlights this as a common pitfall for developers transitioning from SQL to HQL.

Key Insights

  • “8-hour App Engine outage, 2012”: Not applicable here; focus on HQL specifics.
  • “Sagas over ACID for e-commerce”: Not relevant; HQL syntax is the core issue.
  • “Temporal used by Stripe, Coinbase”: Irrelevant; this article focuses on Hibernate, not workflow orchestration.

Working Example

// Failing HQL query with asterisk
session.createQuery("SELECT * FROM Person p", Person.class).list();
// Throws: SyntaxException: token '*', no viable alternative at input
// Correct HQL query using entity alias
List<Person> personList = session.createQuery("SELECT p FROM Person p", Person.class).list();
// Alternatively: session.createQuery("FROM Person", Person.class).list();

Practical Applications

  • Use Case: Selecting all Person entities in HQL using SELECT p or FROM Person.
  • Pitfall: Using SELECT * in HQL leads to SyntaxException due to HQL’s entity-based design.

References:


Continue reading

Next article

Strix: The Open-Source AI Penetration Testing Agent

Related Content