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

如何通过Hibernate向PostgreSQL上传图片?缓存配置问题排查

问题概述

尝试通过Hibernate将图片存储到PostgreSQL时,上传图片环节出现异常;后续配置Ehcache二级缓存,因配置错误触发内部服务器错误;关闭缓存后,又收到二级缓存已禁用等相关警告信息。


相关代码

ProductModel.java

@Entity
@Table(name="products")
@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class ProductModel {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "product_name", nullable = false)
    private String name;

    @Column(name = "description", length = 500)
    private String description;

    @Column(name = "price", nullable = false)
    private Long price;

    @Column(name = "stock", nullable = false)
    private Long stock;

    @Column(name = "category")
    private String category;

    @Column(name="discount")
    private int discount;

    @Column(name = "count")
    private Long count;

    @Column(name="brand")
    private String brand;

    @Column(name="rating")
    private int rating;

    @Column(name="product_image_type")
    private List<String> productImageType;

    @Lob
    @Column(name="product_image")
    private List<byte[]> productImage;

    @Column(name="product_image_name")
    private List<String> productImageName;

    public ProductModel() {
    }

    public ProductModel(Long id, String name, String description, Long price, Long stock, String category, int discount, Long count, String brand, int rating, List<String> productImageType, List<byte[]> productImage, List<String> productImageName) {
        this.id = id;
        this.name = name;
        this.description = description;
        this.price = price;
        this.stock = stock;
        this.category = category;
        this.discount = discount;
        this.count = count;
        this.brand = brand;
        this.rating = rating;
        this.productImageType = productImageType;
        this.productImage = productImage;
        this.productImageName = productImageName;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Long getPrice() {
        return price;
    }

    public void setPrice(Long price) {
        this.price = price;
    }

    public Long getStock() {
        return stock;
    }

    public void setStock(Long stock) {
        this.stock = stock;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public int getDiscount() {
        return discount;
    }

    public void setDiscount(int discount) {
        this.discount = discount;
    }

    public Long getCount() {
        return count;
    }

    public void setCount(Long count) {
        this.count = count;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public int getRating() {
        return rating;
    }

    public void setRating(int rating) {
        this.rating = rating;
    }

    public List<String> getProductImageType() {
        return productImageType;
    }

    public void setProductImageType(List<String> productImageType) {
        this.productImageType = productImageType;
    }

    public List<byte[]> getProductImage() {
        return productImage;
    }

    public void setProductImage(List<byte[]> productImage) {
        this.productImage = productImage;
    }

    public List<String> getProductImageName() {
        return productImageName;
    }

    public void setProductImageName(List<String> productImageName) {
        this.productImageName = productImageName;
    }

    @Override
    public String toString() {
        return "ProductModel{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", description='" + description + '\'' +
                ", price=" + price +
                ", stock=" + stock +
                ", category='" + category + '\'' +
                ", discount=" + discount +
                ", count=" + count +
                ", brand='" + brand + '\'' +
                ", rating=" + rating +
                ", productImageType=" + productImageType +
                ", productImage=" + productImage +
                ", productImageName=" + productImageName +
                '}';
    }
}

EmployeeController.java

@RestController
@RequestMapping("/api/employee")
public class EmployeeController {

    private UserService userService;
    private ProductService productService;
    private PurchaseService purchaseService;

    public EmployeeController(UserService userService, ProductService productService, PurchaseService purchaseService) {
        this.userService = userService;
        this.productService = productService;
        this.purchaseService = purchaseService;
    }

    @PostMapping("/addNewProduct")
    public ResponseEntity<ProductModel> addProduct(@RequestPart("product_detail") ProductModel product
            , @RequestPart("product_images") MultipartFile[] imageData){
        return ResponseEntity.ok(productService.saveTheProduct(product,imageData));
    }

}

hibernate.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:2427/test</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">kevin.2427</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.highlight_sql">true</property>
        <property name="hibernate.cache.use_second_level_cache" value="true" />
        <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.jcache.internal.JCacheRegionFactory" />
        <property name="hibernate.javax.cache.uri" value="ehcache.xml" />
        <property name="hibernate.javax.cache.provider" value="org.ehcache.jsr107.EhcacheCachingProvider" />
    </session-factory>
</hibernate-configuration>

关闭缓存后的报错信息

07-Feb-2025 19:24:36.466 INFO [http-nio-3000-exec-2]
 org.hibernate.cache.internal.RegionFactoryInitiator.initiateService HHH000026:
   二级缓存已禁用
07-Feb-2025 19:24:36.476 WARN [http-nio-3000-exec-2]
 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure HHH10001002:
   使用内置连接池(不建议用于生产环境)
07-Feb-2025 19:24:36.551 WARN [http-nio-3000-exec-2]
 org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.constructDialect HHH90000025:
   无需通过 'hibernate.dialect' 显式指定PostgreSQLDialect(移除该配置项,Hibernate会自动选择)
07-Feb-2025 19:24:36.554 INFO [http-nio-3000-exec-2]
 org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService HHH10001005:
   数据库信息:数据库JDBC URL [jdbc:postgresql://localhost:2427/test] 
   数据库驱动:org.postgresql.Driver 
   数据库版本:17.0 
   自动提交模式:false
   隔离级别:未定义/未知 
   最小池大小:1 
   最大池大小:20 

内容的提问来源于stack exchange,提问作者K Kevin Gladson

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 13:40:54