Hibernate反向工程—java.math.BigDecimal替换

本文介绍在使用Struts2框架时遇到的问题:从Oracle数据库中读取的Integer字段通过Hibernate反向工程生成的Java Bean字段为BigDecimal类型,导致Struts2中的XWork无法正确转换。文中提供了一种快速解决大量字段类型转换的方法。

       

        从oracle数据库中的integer字段通过hibernate的反向工程,生成的bean字段为java.math.BigDecimal类型。但是struts2框架中的xworks对从jsp页面穿过来的bean对象不能对java.math.BigDecimal类型从string类型转换到BigDecimal类型,但能转换为integer和long类型。因此要将BigDecimal类型改成integer和long类型

          现在的问题是如果我有很多字段类型需要转换的话,那我的替换工作量实在太大了,所以我用了一个好办法,很快完成整个项目所有的替换:

步骤如下:

1、

 

2、

 

3、

修改后的Product类: package com.warehouse.demo.entity; import javax.persistence.*; import java.math.BigDecimal; import java.time.LocalDateTime; @Entity @Table(name = "products") public class Product { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(nullable = false, unique = true) private String productCode; @Column(nullable = false) private String name; private String description; @Column(nullable = false) private BigDecimal price; @Column(nullable = false) private Integer stockQuantity; @ManyToOne @JoinColumn(name = "category_id") private Category category; @ManyToOne @JoinColumn(name = "supplier_id") private Supplier supplier; @Column(nullable = false) private LocalDateTime createTime; private LocalDateTime updateTime; // 构造方法 public Product() {} public Product(String productCode, String name, String description, BigDecimal price, Integer stockQuantity) { this.productCode = productCode; this.name = name; this.description = description; this.price = price; this.stockQuantity = stockQuantity; this.createTime = LocalDateTime.now(); } // Getter 和 Setter 方法 public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getProductCode() { return productCode; } public void setProductCode(String productCode) { this.productCode = productCode; } 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 BigDecimal getPrice() { return price; } public void setPrice(BigDecimal price) { this.price = price; } public Integer getStockQuantity() { return stockQuantity; } public void setStockQuantity(Integer stockQuantity) { this.stockQuantity = stockQuantity; } public Category getCategory() { return category; } public void setCategory(Category category) { this.category = category; } public Supplier getSupplier() { return supplier; } public void setSupplier(Supplier supplier) { this.supplier = supplier; } public LocalDateTime getCreateTime() { return createTime; } public void setCreateTime(LocalDateTime createTime) { this.createTime = createTime; } public LocalDateTime getUpdateTime() { return updateTime; } public void setUpdateTime(LocalDateTime updateTime) { this.updateTime = updateTime; } @PrePersist protected void onCreate() { createTime = LocalDateTime.now(); } @PreUpdate protected void onUpdate() { updateTime = LocalDateTime.now(); } @Override public String toString() { return "Product{" + "id=" + id + ", productCode='" + productCode + '\'' + ", name='" + name + '\'' + ", price=" + price + ", stockQuantity=" + stockQuantity + '}'; } }
最新发布
10-11
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值