Solving Hibernate SyntaxException: token ‘*’, no viable alternative at input
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
Personentities in HQL usingSELECT porFROM Person. - Pitfall: Using
SELECT *in HQL leads toSyntaxExceptiondue to HQL’s entity-based design.
References:
Continue reading
Next article
Strix: The Open-Source AI Penetration Testing Agent
Related Content
A Guide to @ConcreteProxy in Hibernate
Learn how to use Hibernate's @ConcreteProxy annotation to correctly lazy load polymorphic associations, resolving type checking failures in inheritance hierarchies.
Building Unshielded Token Smart Contracts on Midnight Network
Develop unshielded token contracts on the Midnight network using the UTXO model and CompactStandardLibrary for transparent public fund management.
Scaling PrestaShop: Solving Load Balancer and Auto-Scaling Challenges
Learn how to scale PrestaShop behind a load balancer, reducing SQL queries by up to 70% while managing 300k SKUs through strategic caching.