You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot查询MySQL产品列表返回空集合无报错问题排查

Spring Boot查询MySQL返回空List无报错问题排查

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) or none (if your table is already set up and you don’t want Hibernate modifying it). Update your application.properties like this:
    spring.jpa.hibernate.ddl-auto=update
    
    After making this change, restart your app, manually insert test data into the products table, 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 numberofavailableinstock is varchar(45), but your entity uses long numberofavailableInStock
  • Database column percentageofsell is varchar, but your entity uses double 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 numberofavailableinstock to bigint
    • Change percentageofsell to double or decimal
      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.

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 the name value 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:
    logging.level.org.hibernate.SQL=DEBUG
    logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
    
    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.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:58:29