本文介绍了使用Hibernate将嵌套的JSON数据保存到MySQL数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我对这个问题深感不安。我为嵌套JSON创建了一个POJO,并获取 MarketPrice 对象中的数据,其中 marketPrices 是一个 ArrayList 它有两个元素。 这是MarketPrice POJO类,实际上我需要将它保存到 MarketPrice 表。即,整个JSON对象。但我有两个实体。这怎么可能? MarketPrice.java @Entity @Table(name =MarketPrice) public class MarketPrice { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name =itemId) private长itemId; @Column(name =analysisDate) private分析日期; @Column(name =marketName) private String marketName; @Column(name =category) private String category; @Column(name =marketPlace) private String marketPlace; @Column(name =state) private String state; @Column(name =district) private String分区; public ArrayList< Items> marketPrices; Items.java public class Items implements Serializable { private static final long serialVersionUID = -2428562977284114465L; @Id @Column(name =id) private int id; @Column(name =itemName) private String itemName; @Column(name =unitofPrice) private String unitofPrice; @Column(name =minimumPrice,columnDefinition =Float(10,2)) private Float minimumPrice; @Column(name =maximumPrice,columnDefinition =Float(10,2)) private Float maximumPrice; 这是我从控制器的服务器端获得的嵌套JSON数据: $ b marketPrices 中的JSON数据 marketPrices:[{itemName:Mango,unitofPrice:Kg, minimumPrice:10,maximumPrice:20},{itemName:Grapes,unitofPrice:Kg, minimumPrice:30,maximumPrice:40}],state:xyz,district: 4,marketPlace:5001,marketName:pmc,category:Fruits} Controller.java @RequestMapping(value = {/ saveAnalysis},method = RequestMethod.POST,consumes =application / json) @ResponseBody public MarketPrice bulkSaveMarketAnalysis(@RequestBody String坊间tPrices,HttpServletResponse响应, HttpServletRequest请求)抛出JsonProcessingException,IOException,JSONException { MarketPrice marketPrice1 = new MarketPrice(); System.out.println(Json Data+ marketPrices); //这里从UI获取有效的嵌套json Gson gson = new Gson(); MarketPrice marketPrice = gson.fromJson(marketPrices,MarketPrice.class); //将其转换为实体类型所有值都存在于其中。 //在此之后被攻击,如何将嵌套的json保存到DB中。 字符串marketDataResponse = analyserService.saveListOfMarketPrice(marketPrice); marketPrice1.setStatusMessage(成功); return marketPrice1; DAO.java public String saveListOfMarketPrice(MarketPrice marketPrice){ System.out.println(In Analyzer DAO fro bulk saving); final Session session = getSession(); session.beginTransaction(); marketPrice.setAnalysisDate(new Date()); (物品项目: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); } session.getTransaction()。commit(); session.close(); 返回成功; 对DAO进行这些更改后,最终保存了谢谢。 解决方案正如评论中所讨论的那样,您可以修改您的代码,使其按照预期工作。 b $ b MarketPrice.java @Entity @Table(name =MarketPrice) public class MarketPrice { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column (name =itemId) private长itemId; @Column(name =analysisDate) private分析日期; @Column(name =marketName) private String marketName; @Column(name =category) private String category; @Column(name =marketPlace) private String marketPlace; @Column(name =state) private String state; @Column(name =district) private String分区; @Transient public Items currentItem; @Column(name =itemName) public String getItemName(){ return this.currentItem.itemName; @Column(name =unitofPrice) public String getUnitofPrice(){ return this.currentItem.unitofPrice; @Column(name =minimumPrice,columnDefinition =Float(10,2)) public Float getMinimumPrice(){ return this。 currentItem.minimumPrice; @Column(name =maximumPrice,columnDefinition =Float(10,2)) public Float getMaximumPrice(){ return this。 currentItem.maximumPrice; } @Transient public ArrayList< Items> marketPrices; Items.java public class Items implements Serializable { private static final long serialVersionUID = -2428562977284114465L; @Id @Column(name =id) private int id; public String itemName; public String unitofPrice; public Float minimumPrice; public Float maximumPrice; DAO.java public String saveListOfMarketPrice(MarketPrice marketPrice){ System.out.println(In Analyzer DAO fro bulk saving); final Session session = getSession(); session.beginTransaction(); (物品项目:marketPrice.marketPrices){ marketPrice.currentItem = item; session.save(marketPrice); } session.getTransaction()。commit(); session.close(); 返回成功; } I am strucked with this issue. I have created a POJO for nested JSON and I am getting data in MarketPrice object where marketPrices is an ArrayList which has two elements.This is MarketPrice POJO class and actually I need to save it into the MarketPrice table. I.e, entire JSON object. But I have two entities. How can this be possible?MarketPrice.java@Entity@Table(name = "MarketPrice")public class MarketPrice {@Id@GeneratedValue(strategy = GenerationType.AUTO)@Column(name = "itemId")private Long itemId;@Column(name = "analysisDate")private Date analysisDate;@Column(name = "marketName")private String marketName;@Column(name = "category")private String category;@Column(name = "marketPlace")private String marketPlace;@Column(name = "state")private String state;@Column(name = "district")private String district;public ArrayList<Items> marketPrices;Items.javapublic class Items implements Serializable { private static final long serialVersionUID = -2428562977284114465L; @Id @Column(name="id") private int id; @Column(name = "itemName") private String itemName; @Column(name = "unitofPrice") private String unitofPrice; @Column(name = "minimumPrice",columnDefinition = "Float(10,2)") private Float minimumPrice; @Column(name = "maximumPrice",columnDefinition = "Float(10,2)") private Float maximumPrice;This is my nested JSON data I'm getting from at the server side in the controller:JSON data in marketPrices{ "marketPrices": [{ "itemName": "Mango", "unitofPrice": "Kg", "minimumPrice": "10", "maximumPrice": "20" }, { "itemName": "Grapes", "unitofPrice": "Kg", "minimumPrice": "30", "maximumPrice": "40" }], "state": "xyz", "district": 4, "marketPlace": 5001, "marketName": "pmc", "category": "Fruits"}Controller.java@RequestMapping(value = {"/saveAnalysis"} , method = RequestMethod.POST,consumes = "application/json")@ResponseBodypublic MarketPrice bulkSaveMarketAnalysis(@RequestBody String marketPrices, HttpServletResponse response, HttpServletRequest request) throws JsonProcessingException, IOException, JSONException{ MarketPrice marketPrice1 = new MarketPrice(); System.out.println("Json Data"+marketPrices);//here am getting valid nested json from UI Gson gson = new Gson(); MarketPrice marketPrice = gson.fromJson(marketPrices, MarketPrice.class);//converting it into Entity type all values are present in it. //Am strucked after this,How to save nested json into DB. String marketDataResponse = analyserService.saveListOfMarketPrice(marketPrice); marketPrice1.setStatusMessage("success"); return marketPrice1;}DAO.javapublic String saveListOfMarketPrice(MarketPrice marketPrice) { System.out.println("In Analyser DAO fro bulk saving"); final Session session = getSession(); 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); } session.getTransaction().commit(); session.close(); return "success";}After making these changes to DAO it saved finallyThank you. 解决方案 As discussed in the comments, you can modify your code as below to make it work as expected.MarketPrice.java@Entity@Table(name = "MarketPrice")public class MarketPrice {@Id@GeneratedValue(strategy = GenerationType.AUTO)@Column(name = "itemId")private Long itemId;@Column(name = "analysisDate")private Date analysisDate;@Column(name = "marketName")private String marketName;@Column(name = "category")private String category;@Column(name = "marketPlace")private String marketPlace;@Column(name = "state")private String state;@Column(name = "district")private String district; @Transient public Items currentItem;@Column(name = "itemName")public String getItemName() { return this.currentItem.itemName;}@Column(name = "unitofPrice")public String getUnitofPrice() { return this.currentItem.unitofPrice;}@Column(name = "minimumPrice",columnDefinition = "Float(10,2)")public Float getMinimumPrice() { return this.currentItem.minimumPrice;}@Column(name = "maximumPrice",columnDefinition = "Float(10,2)")public Float getMaximumPrice() { return this.currentItem.maximumPrice;}@Transientpublic ArrayList<Items> marketPrices;Items.javapublic class Items implements Serializable { private static final long serialVersionUID = -2428562977284114465L; @Id @Column(name="id") private int id; public String itemName; public String unitofPrice; public Float minimumPrice; public Float maximumPrice;DAO.javapublic String saveListOfMarketPrice(MarketPrice marketPrice) { System.out.println("In Analyser DAO fro bulk saving"); final Session session = getSession(); session.beginTransaction(); for (Items item : marketPrice.marketPrices) { marketPrice.currentItem = item; session.save(marketPrice); } session.getTransaction().commit(); session.close(); return "success"; } 这篇关于使用Hibernate将嵌套的JSON数据保存到MySQL数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-23 17:17