使用JPA与MySQL的Java应用出现错误码1054的问题求助
Hey Daniel, let's work through this MySQL error 1054 you're hitting—it's one of the most common hiccups when mapping JPA entities to a MySQL database, so we can narrow it down quickly.
First, a quick clarity check: MySQL Error 1054 always means the database doesn't recognize a column or table name you're referencing—either it doesn't exist, or there's a spelling/case mismatch between your code and the actual database schema.
Possible Causes & Fixes
Let's break down the most likely issues based on your code snippet:
1. Mismatched Entity Field <-> Database Column
Your findJobByCode("ABN54") method is trying to query a job-related field, but there's a good chance either:
- Your
OrderTableentity doesn't have a corresponding field, or - The field's
@Columnannotation doesn't match the actual database column name.
Fix:
Check your OrderTable entity for the field you're targeting. For example, if your database has a column named job_code, your entity should explicitly map to it:
@Column(name = "job_code") // Matches the exact database column name private String jobCode;
If your database column is just code, rename your method to findByCode(String code)—Spring Data JPA (if you're using it) will incorrectly look for a job_code column if you keep findJobByCode.
2. Missing or Incorrect Named Query
You showed a partial @NamedQueries block for OrderTable.findAll, but if findJobByCode relies on a named query, it's probably missing or has a typo.
Fix:
Add a valid named query that references existing entity fields:
@NamedQueries({ @NamedQuery(name = "OrderTable.findAll", query = "SELECT o FROM OrderTable o"), @NamedQuery(name = "OrderTable.findJobByCode", query = "SELECT o FROM OrderTable o WHERE o.jobCode = :code") })
Double-check that o.jobCode matches the exact field name in your OrderTable entity. If using native SQL, replace the entity field name with the actual database column name (e.g., job_code).
3. Table Name Case/Spelling Mismatch
Your entity uses @Table(name = "order_table"), but MySQL's table name sensitivity varies by OS:
- Linux/macOS: MySQL is case-sensitive by default, so if your database table is named
Order_TableorORDERTABLE, this will fail. - Windows: Case-insensitive, but it's still best to match exactly.
Fix:
Verify your database actually has a table named order_table (run SHOW TABLES; in the MySQL CLI to check). If the table name differs, update the @Table annotation to match.
4. Method Name Derivation Issues (Spring Data JPA)
If you're using Spring Data JPA's automatic query generation from method names, findJobByCode follows the pattern find[Prefix]By[Field]. The Job part here is being interpreted as part of the field name—so JPA will look for a field named jobCode. If your entity only has a code field, this triggers the 1054 error.
Fix:
Rename the method to findByCode(String code) to match your entity's field name, or adjust the entity field name to jobCode (and update the database column if needed).
内容的提问来源于stack exchange,提问作者Daniel M

