我对此问题感到困惑,请任何人都可以帮忙解决这个问题。 我将嵌套的Json作为控制器的输入,然后将其转换为MarketPrice对象类型。一切正常,只有在保存面临问题时。第一条记录将被保存多次。但是在forecah循环中第二次是另一个值将在调试时显示。
MarketPrice.java
@Entity
@Table(name = "MarketPrice")
public class MarketPrice {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "itemId")
private Long itemId;
@Column(name = "state")
private String state;
@Column(name = "district")
private String district;
@Transient
public Items currentItem;
@Column(name="itemName")
private String itemName;
@Column(name="minimumPrice")
private Float minimumPrice;
@Column(name="maximumPrice")
private Float maximumPrice;
}
Items.java
public class Items implements Serializable {
private static final long serialVersionUID = -2428562977284114465L;
public String itemName;
public Float minimumPrice;
public Float maximumPrice;
}
控制器
@RequestMapping(value = {"/save"} , method = RequestMethod.POST,consumes = "application/json")
@ResponseBody
public MarketPrice bulkSaveMarketAnalysis(@RequestBody
String marketPrices, HttpServletResponse response,
HttpServletRequest request) throws JsonProcessingException, IOException, JSONException{
MarketPrice marketPrice1 = new MarketPrice();
Gson gson = new Gson();
MarketPrice marketPrice = gson.fromJson(marketPrices, MarketPrice.class);
if(marketPrice.getState() != null){
String marketDataResponse = analyserService.saveListOfMarketPrice(marketPrice);
System.out.println(marketDataResponse);
return marketPrice1;
}
DAO.java
public String saveListOfMarketPrice(MarketPrice marketPrice) {
final Session session = getSession();
Transaction tx = session.beginTransaction();
marketPrice.setAnalysisDate(new Date());
for (Items item : marketPrice.marketPrices) {
marketPrice.currentItem = item;
marketPrice.setItemName(marketPrice.currentItem.getItemName());
marketPrice.setUnitofPrice(marketPrice.currentItem.getUnitofPrice());
marketPrice.setMinimumPrice(marketPrice.currentItem.getMinimumPrice());
marketPrice.setMaximumPrice(marketPrice.currentItem.getMaximumPrice());
session.save(marketPrice);
tx.commit();
}
session.close();
return "success";
}
Json数据到控制器
{"marketPrices":[{"itemName":"Grapes","unitofPrice":"Kg","minimumPrice":"11","maximumPrice":"22"},{"itemName":"Mango","unitofPrice":"Quintal","minimumPrice":"55","maximumPrice":"66"}],"state":"xyz","district":4,"marketPlace":5001,"marketName":"Apmc","category":"Fruits"}
我面临的问题是,只有一条记录会多次添加到数据库中。
答案 0 :(得分:2)
您再次使用相同的参考市场价格。您必须创建新引用才能创建新行,否则它将继续更新同一个管理实体。此外,在保存所有实体后提交您的交易。
public String saveListOfMarketPrice(MarketPrice marketPrice) {
final Session session = getSession();
Transaction tx = session.beginTransaction();
for (Items item : marketPrice.marketPrices) {
MarketPrice marketPriceToSaveInDBAsNewRow = new MarketPrice();
marketPriceToSaveInDBAsNewRow.setAnalysisDate(new Date());
marketPriceToSaveInDBAsNewRow.currentItem = item;
marketPriceToSaveInDBAsNewRow.setItemName(marketPriceToSaveInDBAsNewRow.currentItem.getItemName());
marketPriceToSaveInDBAsNewRow.setUnitofPrice(marketPriceToSaveInDBAsNewRow.currentItem.getUnitofPrice());
marketPriceToSaveInDBAsNewRow.setMinimumPrice(marketPriceToSaveInDBAsNewRow.currentItem.getMinimumPrice());
marketPriceToSaveInDBAsNewRow.setMaximumPrice(marketPriceToSaveInDBAsNewRow.currentItem.getMaximumPrice());
session.save(marketPriceToSaveInDBAsNewRow);
}
tx.commit();
session.close();
//session.getTransaction().commit();
return "success";
}