0

私は休止状態を使用しています。2 つのテーブルにデータを挿入し、1 つのテーブルを更新しています。更新中に例外が発生します-オブジェクトが保存されていない一時的なインスタンスを参照しているため-save the transient instance before flushing: com.treamis.inventory.goodsReceived.GoodsRecieved.. 更新関連のコードを削除すると、正常に動作します

try {
            SalesInventoryDAO dao = new SalesInventoryDAO();
            sess = HibernateUtil.getSessionFactory().openSession();
            Transaction tr = sess.beginTransaction();
            GoodsRecievedForm item = (GoodsRecievedForm) form;
            GoodsRecieved bk = new GoodsRecieved();
            bk.setGoodsId(item.getGoodsId());
            InventoryOrder order = (InventoryOrder) sess.get(InventoryOrder.class, item.getOrderNo());
            bk.setOrderNo(order);
//            if (order.getQuotation().getQuotationNo() != null) {
//                bk.setQuotation(order.getQuotation().getQuotationNo());
//            } else {
//                bk.setQuotation(null);
//            }

            java.util.Date temp = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(item.getRecievedDate());
            java.sql.Date temp1 = new java.sql.Date(temp.getTime());
            bk.setRecievedDate(temp1);
            bk.setOrderQty(order.getTotalqty());
            bk.setReceivedPersonName(item.getReceivedPersonName());
            bk.setReceivedQty(item.getReceivedQty());
            bk.setConditionOfMaterial(item.getConditionOfMaterial());
            UserEntity msg;
            HttpSession session = request.getSession(false);
            msg = (UserEntity) session.getAttribute("user");
            bk.setAddedBy(msg);
            bk.setAddedDate(new Date());
            int[] item1111 = item.getGoodsDetails();
            String[] productre = item.getGoodsDetailsName();
            float proqty[] = item.getGoodsDetailsQty();
            float price[] = item.getGoodsDetailsPrice();
            float receivedqty[] = item.getReceivedquantity();
            GoodsReceivedDetails mb;
            Set<GoodsReceivedDetails> purDetails = new HashSet<GoodsReceivedDetails>();
            for (int i = 0; i < productre.length; i++) {
                mb = new GoodsReceivedDetails();
                mb.setGoodsDetailsName(productre[i]);
                mb.setGoodsDetailsQty(proqty[i]);
                mb.setGoodsDetailsPrice(price[i]);
                mb.setReceivedquantity(receivedqty[i]);
                //System.out.println("productre" + productre[i]);
                int id3 = item1111[i];
                //System.out.println("id3id3id3id3" + id3);
                // int id3 = Integer.parseInt(productre[i]);
                Item idf = (Item) sess.get(Item.class, id3);
                System.out.println("mb.getReceivedquantity()" + mb.getReceivedquantity());
                float qty = (idf.getItemStock() + mb.getReceivedquantity());
                System.out.println("qtyqty" + qty);
                mb.setItemId(idf);
                Query qry = sess.createQuery("UPDATE Item set itemStock='" + qty + "' where itemId='" + idf.getItemId() + "'");
                qry.executeUpdate();
//                dao.updateitem(qty, idf,sess);
                //dao.updateitem(idf);
                mb.setGoodsId(bk);
                sess.save(mb);
                purDetails.add(mb);
            }
            bk.setGoodsDetails(purDetails);
            sess.save(bk);
            tr.commit();
            //System.out.println("comming");
//            List ls = gdao.getOrderItems(order.getOrderId());
//            for (Iterator it = ls.iterator(); it.hasNext();) {
//                InventoryOrderDetails inv = (InventoryOrderDetails) it.next();
//                gdao.updateitem(inv.getItemId().getItemStock() + bk.getReceivedQty(), inv.getItemId());
//            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sess.close();
        }

これは私のスタックトレースです:

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.treamis.inventory.goodsReceived.GoodsRecieved
    at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219)
    at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
    at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:242)
    at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:597)
    at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:3123)
    at org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:479)
    at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:204)
    at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:127)
    at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196)
    at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
    at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:35)
    at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:969)
    at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:1136)
    at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:94)
    at com.treamis.inventory.goodsReceived.AddGoodsRecievedAction.addGoodsReceived(AddGoodsRecievedAction.java:112)
    at com.treamis.inventory.goodsReceived.AddGoodsRecievedAction.execute(AddGoodsRecievedAction.java:54)
    at org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
    at org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
    at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
    at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
    at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
    at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
    at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.treamis.academics.examlist.SessionFilterServelet.doFilter(SessionFilterServelet.java:190)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:125)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1008)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

これは 1 つのエンティティ GoodsReceived です:

@Entity
public class GoodsRecieved implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int goodsId;
    @OneToOne
    private InventoryOrder orderNo;
//    private String quotation;
    private float orderQty;
    private String receivedPersonName;
    private float receivedQty;
    private String conditionOfMaterial;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date recievedDate;
    @OneToOne
    private UserEntity addedBy;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date addedDate;
    @OneToOne
    private UserEntity modifiedBy;
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private Date modifiedDate;
      @OneToMany(targetEntity = GoodsReceivedDetails.class, fetch = FetchType.LAZY)
    @JoinColumn(name = "goodsDetails", referencedColumnName = "goodsId")
    @Cascade(CascadeType.ALL)
    private Set<GoodsReceivedDetails> goodsDetails;

    public int getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(int goodsId) {
        this.goodsId = goodsId;
    }

    public InventoryOrder getOrderNo() {
        return orderNo;
    }

    public void setOrderNo(InventoryOrder orderNo) {
        this.orderNo = orderNo;
    }

    public float getOrderQty() {
        return orderQty;
    }

    public void setOrderQty(float orderQty) {
        this.orderQty = orderQty;
    }

    public String getReceivedPersonName() {
        return receivedPersonName;
    }

    public void setReceivedPersonName(String receivedPersonName) {
        this.receivedPersonName = receivedPersonName;
    }

    public float getReceivedQty() {
        return receivedQty;
    }

    public void setReceivedQty(float receivedQty) {
        this.receivedQty = receivedQty;
    }

//    public String getQuotation() {
//        return quotation;
//    }
//
//    public void setQuotation(String quotation) {
//        this.quotation = quotation;
//    }

    public Date getRecievedDate() {
        return recievedDate;
    }

    public void setRecievedDate(Date recievedDate) {
        this.recievedDate = recievedDate;
    }

    public UserEntity getAddedBy() {
        return addedBy;
    }

    public void setAddedBy(UserEntity addedBy) {
        this.addedBy = addedBy;
    }

    public Date getAddedDate() {
        return addedDate;
    }

    public void setAddedDate(Date addedDate) {
        this.addedDate = addedDate;
    }

    public UserEntity getModifiedBy() {
        return modifiedBy;
    }

    public void setModifiedBy(UserEntity modifiedBy) {
        this.modifiedBy = modifiedBy;
    }

    public Date getModifiedDate() {
        return modifiedDate;
    }

    public void setModifiedDate(Date modifiedDate) {
        this.modifiedDate = modifiedDate;
    }

    public String getConditionOfMaterial() {
        return conditionOfMaterial;
    }

    public void setConditionOfMaterial(String conditionOfMaterial) {
        this.conditionOfMaterial = conditionOfMaterial;
    }

    public Set<GoodsReceivedDetails> getGoodsDetails() {
        return goodsDetails;
    }

    public void setGoodsDetails(Set<GoodsReceivedDetails> goodsDetails) {
        this.goodsDetails = goodsDetails;
    }   
}

これは私の別のエンティティ GoodsReceivedDetails です:

    @Entity
    public class GoodsReceivedDetails implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int goodsDetailsId;
    private String goodsDetailsName;
    private float goodsDetailsQty;
    private float goodsDetailsPrice;
    private float receivedquantity;
    @ManyToOne
    @JoinColumn(name = "goodsId")
    private GoodsRecieved goodsId;
    @OneToOne()
    private Item itemId;

    public int getGoodsDetailsId() {
        return goodsDetailsId;
    }

    public void setGoodsDetailsId(int goodsDetailsId) {
        this.goodsDetailsId = goodsDetailsId;
    }

    public String getGoodsDetailsName() {
        return goodsDetailsName;
    }

    public void setGoodsDetailsName(String goodsDetailsName) {
        this.goodsDetailsName = goodsDetailsName;
    }

    public float getGoodsDetailsQty() {
        return goodsDetailsQty;
    }

    public void setGoodsDetailsQty(float goodsDetailsQty) {
        this.goodsDetailsQty = goodsDetailsQty;
    }

    public float getGoodsDetailsPrice() {
        return goodsDetailsPrice;
    }

    public void setGoodsDetailsPrice(float goodsDetailsPrice) {
        this.goodsDetailsPrice = goodsDetailsPrice;
    }

    public GoodsRecieved getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(GoodsRecieved goodsId) {
        this.goodsId = goodsId;
    }

    public Item getItemId() {
        return itemId;
    }

    public void setItemId(Item itemId) {
        this.itemId = itemId;
    }

    public float getReceivedquantity() {
        return receivedquantity;
    }

    public void setReceivedquantity(float receivedquantity) {
        this.receivedquantity = receivedquantity;
    }
}
4

3 に答える 3

0

goodsIdプロパティの注釈の下には、 を参照する外部キーでGoodsReceivedDetailsある列があることが示されています。goodsIdGoodsRecieved

@ManyToOne
@JoinColumn(name = "goodsId")
private GoodsRecieved goodsId;

@JoinColumnしたがって、 for goodsDetailsinは必要ありませんGoodsRecieved。そのマッピングを次のように変更します。

@OneToMany(mappedBy = "goodsId", fetch = FetchType.LAZY)
@Cascade(CascadeType.ALL)
private Set<GoodsReceivedDetails> goodsDetails;

ここで@Cascade(CascadeType.ALL)は推移的永続性を紹介します。これにより、 new をインスタンス化してGoodsReceivedDetailsに追加すると、GoodsRecievedGoodsReceivedDetailsすぐに永続化されます。Session インターフェイスGoodsReceivedDetailsを呼び出して明示的に永続化する必要はありません。save()

したがって、このコード ブロックを置き換えることができます。

mb.setGoodsId(bk);
sess.save(mb);
purDetails.add(mb);
}
bk.setGoodsDetails(purDetails);
sess.save(bk);

これとともに:

mb.setGoodsId(bk);
purDetails.add(mb);
}
bk.setGoodsDetails(purDetails);
sess.save(bk);

カスケード アノテーションは、が永続的な によって参照されているGoodsReceivedDetails場合、新しいインスタンスを永続化する (つまり、データベースに保存する) ようにHibernate に指示します。GoodsReceivedDetailsGoodsRecieved

于 2013-10-29T10:32:11.797 に答える
0

あなたの問題はここにあると思います:

    ...
    mb.setGoodsId(bk);
    sess.save(mb);
    purDetails.add(mb);
}
bk.setGoodsDetails(purDetails);
sess.save(bk);
tr.commit();
...

mb永続化する前に、永続化しようとしていbkます。bk最初に持続するようにしてください。

于 2013-10-29T10:19:17.580 に答える