【发布时间】:2021-12-30 17:42:10
【问题描述】:
我使用 spring boot 作为后端 rest API 并连接到 MySQL 数据库。我创建了它将映射到的初始实体和数据库表。我意识到我的数据库中的一列命名不正确,并在数据库中更新了它的名称,并更新了我的实体以反映更改。这样做之后,当我向控制器端点发送 GET 或 POST 请求时,返回的对象的值不会反映我对实体/数据库所做的更改。我尝试将我的applications.properties 文件中的spring.jpa.hibernate.ddl-auto 属性更改为update、create 和create drop,但都没有成功。这是我的实体类、applications.properties 文件、我的控制器端点、我的存储库和 2 个图像、一个我的数据库表和一个来自 GET 端点的响应。
皮肤.java
import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
@Entity
@Table(name="skins")
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
public class Skin {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private Long id;
@Column(name="category")
private String category;
@Column(name="collection")
private String collection;
@Column(name="quality")
private String quality;
@Column(name="wear")
private String wear;
@Column(name="weapon")
private String weapon;
@Column(name="name")
private String name;
@Column(name="price")
private BigDecimal price;
@Column(name="quantity")
private int quantity;
@Column(name="date_bought")
private Date dateBought;
@ManyToOne(cascade= {CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH})
@JoinColumn(name="trade_id")
private Trade trade;
@Column(name="outcome_sell_price")
private BigDecimal outcomeSellPrice;
@Column(name="date_traded")
private Date dateTraded;
@Column(name="sell_price")
private BigDecimal sellPrice;
@Column(name="date_sold")
private Date dateSold;
public Skin() {
}
public Skin(String category, String collection, String quality, String wear, String weapon, String name,
BigDecimal price, int quantity, Date dateBought, Trade trade, BigDecimal outcomeSellPrice, Date dateTraded,
BigDecimal sellPrice, Date dateSold) {
this.category = category;
this.collection = collection;
this.quality = quality;
this.wear = wear;
this.weapon = weapon;
this.name = name;
this.price = price;
this.quantity = quantity;
this.dateBought = dateBought;
this.trade = trade;
this.outcomeSellPrice = outcomeSellPrice;
this.dateTraded = dateTraded;
this.sellPrice = sellPrice;
this.dateSold = dateSold;
}
public String getWeapon() {
return weapon;
}
public void setWeapon(String weapon) {
this.weapon = weapon;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCollection() {
return collection;
}
public void setCollection(String collection) {
this.collection = collection;
}
public String getWear() {
return wear;
}
public void setWear(String wear) {
this.wear = wear;
}
public String getQuality() {
return quality;
}
public void setQuality(String quality) {
this.quality = quality;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public Date getDateBought() {
return dateBought;
}
public void setDateBought(Date dateBought) {
this.dateBought = dateBought;
}
public Trade getTrade() {
return trade;
}
public void setTrade(Trade trade) {
this.trade = trade;
}
public BigDecimal getSellPrice() {
return sellPrice;
}
public void setSellPrice(BigDecimal sellPrice) {
this.sellPrice = sellPrice;
}
public Date getDateTraded() {
return dateTraded;
}
public void setDateTraded(Date dateTraded) {
this.dateTraded = dateTraded;
}
public BigDecimal getSoldAt() {
return outcomeSellPrice;
}
public void setSoldAt(BigDecimal soldAt) {
this.outcomeSellPrice = soldAt;
}
public Date getDateSold() {
return dateSold;
}
public void setDateSold(Date dateSold) {
this.dateSold = dateSold;
}
@Override
public String toString() {
return "Skin [id=" + id + ", category=" + category + ", collection=" + collection + ", quality=" + quality
+ ", wear=" + wear + ", weapon=" + weapon + ", name=" + name + ", price=" + price + ", quantity="
+ quantity + ", dateBought=" + dateBought + ", trade=" + trade + ", outcomeSellPrice=" + outcomeSellPrice
+ ", dateTraded=" + dateTraded + ", sellPrice=" + sellPrice + ", dateSold=" + dateSold + "]";
}
}
控制器端点
@Autowired
SkinRepository sr;
@GetMapping("")
public List<Skin> getSkins() {
return sr.findByTradeIdNullAndDateSoldNull();
}
@PostMapping("")
public Skin insertSkin(@RequestBody Skin skin) {
// need to figure out why this isn't picking up the database changes
return sr.save(skin);
}
SkinRepository.java
@Repository
public interface SkinRepository extends JpaRepository<Skin, Long> {
List<Skin> findByTradeIdNullAndDateSoldNull();
}
application.properties
spring.datasource.url=jdbc:mysql://${DB_HOST}:${DB_PORT}/${DB_SCHEMA}?useSSL=false&serverTimezone=UTC
spring.datasource.username=${DB_USER}
spring.datasource.password=${DB_PASS}
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.SQL=DEBUG
spring.jpa.hibernate.ddl-auto=update
这是我在向 GET 端点发送请求时得到的响应。值soldAt 是更改前列的旧名称,应该是outcome_sell_price。
任何关于如何从端点获得响应以匹配数据库和实体的想法将不胜感激。提前致谢。
【问题讨论】:
-
spring.jpa.hibernate.ddl-auto=create应该做你的工作 -
我发布了一个可能的解决方案,你能检查一下吗?@Micah Ludwick
标签: java spring spring-boot jpa