使用@Table注解时出现‘statement or delimiter expected, got ...’错误求助
Hey there! Let's break down this frustrating error you're hitting with the @Table annotation—this is a super common pitfall when starting out with Spring Boot and JPA, so we'll get you sorted out quickly.
Here are the most likely causes and fixes to check step by step:
1. Your table name is a reserved SQL keyword
This is the #1 culprit. If your database table uses a name like user, order, group, or any other reserved keyword in your SQL dialect (MySQL, PostgreSQL, etc.), JPA will throw this error because it doesn't know how to parse the keyword as a table name.
Fix: Wrap the table name in backticks (`) to escape it:
// For a table named "order" (reserved keyword in MySQL) @Table(name = "`order`") public class OrderEntity { // ... your columns here }
2. Typos or case mismatch between annotation and database
Double-check that the name value in @Table exactly matches the actual table name in your database—including case sensitivity (some databases like PostgreSQL are case-sensitive by default if you created the table with quoted names) and underscores.
For example:
- If your database table is
customer_orders, don't write@Table(name = "customerOrders")unless your naming strategy is configured to auto-convert (but even then, if you're explicitly settingname, it overrides the strategy). - If you created the table as
CustomerOrdersin PostgreSQL (quoted), you need to match that case exactly:@Table(name = "CustomerOrders").
3. Special characters or spaces in the table name
If your table name includes spaces, hyphens, or other non-standard characters, JPA can't parse it without escaping.
Fix: Use backticks to wrap the name, just like with reserved keywords:
@Table(name = "`user-preferences`") public class UserPreferences { // ... }
4. Wrong @Table annotation import
It's easy to accidentally import the wrong @Table class, especially if your IDE auto-completes the wrong one. Make sure you're using the JPA version:
- For Spring Boot 2.x:
import javax.persistence.Table; - For Spring Boot 3.x:
import jakarta.persistence.Table;
Avoid importing any other @Table annotations from Spring Data or other libraries—those aren't meant for mapping database tables directly.
5. IDE cache glitch (false positive error)
Sometimes your IDE (like IntelliJ or Eclipse) will flag a valid annotation as an error because of a cached issue.
Fix:
- In IntelliJ: Go to
File > Invalidate Caches... > Invalidate and Restart - In Eclipse: Go to
Project > Clean...and clean your project, then refresh it.
6. Misconfigured JPA naming strategy
If you have a custom naming strategy set in your application.properties or application.yml, it might be conflicting with your explicit @Table(name) setting. For example, if you have:
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
This strategy automatically converts camelCase to snake_case, but if you're manually setting name, it should take precedence. Still, double-check that your manual name doesn't clash with the strategy's output.
Start with checking for reserved keywords and typos—those are the most common fixes. If none of these work, share a snippet of your @Table annotation and the actual database table name, and we can dig deeper!
内容的提问来源于stack exchange,提问作者kronokrusader

