我使用 spring boot 作為后端休息 API 并連接到 MySQL 資料庫。我創建了它將映射到的初始物體和資料庫表。我意識到我的資料庫中的一列被錯誤命名并更新了它在資料庫中的名稱并更新了我的物體以反映更改。這樣做之后,當我向控制器端點發送 GET 或 POST 請求時,回傳的物件的值不會反映我對物體/資料庫所做的更改。我曾試圖改變spring.jpa.hibernate.ddl-auto我的屬性applications.properties檔案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();
}
應用程式屬性
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.
GET 端點回應
關于如何從端點獲得回應以匹配資料庫和物體的任何想法將不勝感激。提前致謝。
uj5u.com熱心網友回復:
您仍然獲得相關名稱的原因可能是因為您有以下 getter 和 setter。洗掉或更新這個 getter 和 setter,看看你是否仍然得到錯誤的回應。
public BigDecimal getSoldAt() {
return outcomeSellPrice;
}
public void setSoldAt(BigDecimal soldAt) {
this.outcomeSellPrice = soldAt;
}
uj5u.com熱心網友回復:
有些accessor and mutator在您的代碼中不可用,因此將其洗掉,因為它在編譯您的代碼時會產生錯誤并設定hibernate.ddl-auto為create它可以在您的資料庫中創建一個新表,否則使用create它可以洗掉您的資料庫并基于物體創建表。
新皮膚物體
@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(Long id, 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.id = id;
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 Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getCollection() {
return collection;
}
public void setCollection(String collection) {
this.collection = collection;
}
public String getQuality() {
return quality;
}
public void setQuality(String quality) {
this.quality = quality;
}
public String getWear() {
return wear;
}
public void setWear(String wear) {
this.wear = wear;
}
public String getWeapon() {
return weapon;
}
public void setWeapon(String weapon) {
this.weapon = weapon;
}
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 getOutcomeSellPrice() {
return outcomeSellPrice;
}
public void setOutcomeSellPrice(BigDecimal outcomeSellPrice) {
this.outcomeSellPrice = outcomeSellPrice;
}
public Date getDateTraded() {
return dateTraded;
}
public void setDateTraded(Date dateTraded) {
this.dateTraded = dateTraded;
}
public BigDecimal getSellPrice() {
return sellPrice;
}
public void setSellPrice(BigDecimal sellPrice) {
this.sellPrice = sellPrice;
}
public Date getDateSold() {
return dateSold;
}
public void setDateSold(Date dateSold) {
this.dateSold = dateSold;
}
application.properties 中的更改
spring.jpa.hibernate.ddl-auto= create
或者
spring.jpa.hibernate.ddl-auto= create-drop
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/361313.html
上一篇:無法在多模塊Maven專案中決議值“${test.name}”中的占位符“test.name”-SpringBoot
下一篇:具有可變段數的彈簧路徑
