sql >> Databasteknik >  >> RDS >> Mysql

Sparar kapslade JSON-data i en MySQL-databas med Hibernate

Som diskuterats i kommentarerna kan du ändra din kod enligt nedan för att få den att fungera som förväntat.

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;
}

@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 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";
    }


  1. MySQL:Inbäddad JSON vs tabell

  2. BESTÄLL AV med inre fråga, vilket ger ORA-00907 saknad höger parentes

  3. SQL Server RAISERROR-uttalande med enkla exempel

  4. Introduktion till temporära tabeller i SQL Server