Hibernate将空值插入表

时间:2014-11-23 20:08:08

标签: java hibernate

我正在从事一个项目,我必须在那里实施销售点系统。一切正常,除了我的历史项目没有保存在数据库中。我正在使用HSQL和Hibernate。

这是我的SoldItem,它作为List收集,然后生成一个HistoryItem。

   /**
 * Already bought StockItem. SoldItem duplicates name and price for preserving history. 
 */



@Entity
@Table(name="SOLDITEM")
public class SoldItem implements Cloneable, DisplayableItem {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    @JoinColumn(name="STOCKITEM_ID", nullable=false)
    private StockItem stockItem;

    @Column(name="NAME")
    private String name;

    @Column(name="QUANTITY")
    private Integer quantity;

    @Column(name="ITEMPRICE")
    private double price;

    @ManyToOne
    @JoinColumn(name = "SALE_ID", nullable = false)
    private ToHistoryItem historyItem;

    public SoldItem(){

   }
    public SoldItem(StockItem stockItem, int quantity) {
        this.stockItem = stockItem;
        this.name = stockItem.getName();
        this.price = stockItem.getPrice();
        this.quantity = quantity;
        this.id=stockItem.getId();


    }

    public ToHistoryItem getHistoryItem() {
        return historyItem;
    }

    public void setHistoryItem(ToHistoryItem historyItem) {
        this.historyItem = historyItem;
    }


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    public double getSum() {
        return price * ((double) quantity);
    }

    public StockItem getStockItem() {
        return stockItem;
    }

    public void setStockItem(StockItem stockItem) {
        this.stockItem = stockItem;
    }

这是我的HistoryItem

@Entity
@Table(name="HISTORYITEM")
public class ToHistoryItem implements Cloneable, DisplayableItem{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name="SOLD_DATE")
    private Date date;

    @OneToMany(mappedBy = "historyItem")
    private List<SoldItem> purchasedGoods;

    @Column(name="TOTAL_PRICE")
    private double totalPrice;

    public ToHistoryItem(){}

    public ToHistoryItem(List<SoldItem> purchasedGoods) {
        super();
        Calendar calendar = Calendar.getInstance();

        this.date = calendar.getTime();
        this.purchasedGoods = purchasedGoods;
        this.totalPrice = 0;
        for(SoldItem sold:purchasedGoods){
            this.totalPrice+=sold.getSum();

        }
    }
    @Override
    public Long getId() {
        return id;
    }


    public String getDate() {
        return new SimpleDateFormat("dd.MM.yyyy").format(date);
    }



    public String getTime() {
        return new SimpleDateFormat("HH:mm:ss").format(date);
    }


    public List<SoldItem> getPurchasedGoods() {
        return purchasedGoods;
    }


    public double getTotalPrice() {
        return totalPrice;
    }
    public void saveHistoryItemToDb(){
        Session ses=HibernateUtil.currentSession();
        ses.getTransaction().begin();

        for(SoldItem item : purchasedGoods){
            ses.save(item);;
        }
        ses.save(this);
        ses.getTransaction().commit();
    }








}

我的问题是,当我启动POS并购买时,Hibenate在控制台中说:

   [java] Hibernate: insert into SOLDITEM (id, SALE_ID, NAME, ITEMPRICE, QUANTITY, STOCKITEM_ID)
values (default, ?, ?, ?, ?, ?)
 [java] Hibernate: insert into HISTORYITEM (id, SOLD_DATE, TOTAL_PRICE) values (default, ?, ?)
 [java] Hibernate: update STOCKITEM set DESCRIPTION=?, NAME=?, PRICE=?, QUANTITY=? where id=?

0 个答案:

没有答案