Spring Boot查询MySQL产品列表返回空集合无报错问题排查
Hey there, let's work through why your product list is coming back empty even with no console errors. I’ve gone through your code and config, and here are the most likely culprits and fixes:
1. The create-drop setting is wiping your table data
Your spring.jpa.hibernate.ddl-auto=create-drop configuration tells Hibernate to delete and recreate your database tables every time the app starts. That means any existing data in your products table gets erased on each launch—so of course your query returns an empty list!
- Fix: Change this value to
update(to keep existing data while updating table structure) ornone(if your table is already set up and you don’t want Hibernate modifying it). Update yourapplication.propertieslike this:
After making this change, restart your app, manually insert test data into thespring.jpa.hibernate.ddl-auto=updateproductstable, and hit your endpoint again.
2. Mismatched field types between entity and database
Looking at your table structure and entity class, there are type mismatches that can break data mapping (even without explicit errors):
- Database column
numberofavailableinstockisvarchar(45), but your entity useslong numberofavailableInStock - Database column
percentageofsellisvarchar, but your entity usesdouble percentageOfSell
Hibernate can’t properly map these incompatible types, which might cause it to return no results. - Fix: Align the types. The better approach is to modify your database columns to match the entity:
- Change
numberofavailableinstocktobigint - Change
percentageofselltodoubleordecimal
Alternatively, you could adjust the entity field types to match the database, but storing numeric values as strings isn’t ideal for inventory and percentage data.
- Change
3. Confirm your products table actually has data
It sounds obvious, but double-check that there’s real data in your myproduct database’s products table. If create-drop wiped the table, or you never inserted any data, an empty list is expected behavior.
- Fix: Use a MySQL client (like MySQL Workbench or Navicat) to run
SELECT * FROM products;and verify there are rows. If not, add a few test entries and retry your query.
4. Verify column name mapping (case sensitivity)
While MySQL is case-insensitive on Windows (and usually on Linux with default settings), double-check that your entity’s column mappings match exactly:
- You already used
@Column(name="...")to map fields, but confirm thenamevalue matches the database column name perfectly (including any underscores or casing if your database is case-sensitive). For example:@Column(name="numberofavailableinstock") private long numberofavailableInStock;
5. Enable detailed logging to debug the SQL
If none of the above works, turn on more verbose Hibernate logging to see exactly what SQL is being executed, and if there are hidden warnings:
- Add these lines to
application.properties:
The console will print the full SQL query Hibernate runs. Copy this query and run it directly in your MySQL client—if it returns no results, you’ll know the issue is with the data or query, not your code.logging.level.org.hibernate.SQL=DEBUG logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
Start with the create-drop fix first—it’s the most common reason for this exact problem. Let me know if you still run into issues after trying these steps!
内容的提问来源于stack exchange,提问作者OUN Saif

