3 Replies Latest reply on Sep 25, 2011 2:54 PM by Leo van den berg

    object references an unsaved transient instance - save the transient instance before flushing

    Sameera Abeysekara Gunawardena Newbie

      When merge call its give below exception. Couldn't understand what is the reason.
      Any forum member can be advice really appreciate.
       


      Caused by: java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: org.domain.cpms.entity.SrrTxn.subLocationMastByFromSubLocationId org.domain.cpms.entity.SubLocationMast

        • 1. Re: object references an unsaved transient instance - save the transient instance before flushing
          Leo van den berg Master

          Hi,


          The object you are merging has a reference to another unsaved entity. So in your model you have probably a OneTomany to a Collection. Because the collection member is not saved yet, the error is produced. You can solve this by adding a cascade attribute on your collection mapping. Look at the Hibernate/JPA documentation how that's is done. If that's not clear enough send some code and see what can be done.


          Leo


          P.S. These questions belong to the Hibernate forum, where there is a better chance that the're being answered fast.

          • 2. Re: object references an unsaved transient instance - save the transient instance before flushing
            Sameera Abeysekara Gunawardena Newbie
            "You can solve this by adding a cascade attribute on your collection mapping."

            How can be set cascade attribute on your collection mapping?

            I try to find but didnt get below i have post my code.

            public void initiateSrrHeader() {

                            srrTxnHome.setInstance(srrTxn);
                           
                            srrTxn.setUserMastByCreatedByUserId(
                                            getCreatedByUser(1));
                            srrTxn.setUserMastByRequestedByUserId(userMastReq);

                            srrTxn.setSiteWorkTxnByFromSiteWorkId(siteWorkTxn);
                            srrTxn.setSiteWorkTxnByToSiteWorkId(siteWorkTxn);

                            srrTxn.setPriorityMast(priorityMast);
                            srrTxn.setSubLocationMastByFromSubLocationId(
                                            subLocationMast);
                            srrTxn.setLocationMastByFromLocationId(locationMast);
                            srrTxn.setSubLocationMastByToSubLocationId(
                                            subLocationMast);
                            srrTxn.setLocationMastByToLocationId(locationMast);

                            srrTxn.setSrrId(Long.parseLong(genarateSRRNumber()));
                            srrTxn.setCreatedDate(this.getCreatedDate());
                            srrTxn.setDateRequested(this.getRequestedDate());
                            projectMast.setProjectId(1);
                            srrTxn.setProjectMast(projectMast);

                    }

            public String save() {
                            srrTxn.setQsApprovedDate(createdDate);
                            SrrTxn newSrrTxn = srrTxnHome.getEntityManager().merge(srrTxn);
                            Iterator<SrrItemTxn> it = SRRItems.iterator();
                            SrrItemTxn newSrrItem;

                            while (it.hasNext()) {
                                    srrItemTxnHome.clearInstance();
                                    newSrrItem = (SrrItemTxn) it.next();
                                   
                                    if (newSrrItem.getReturnReasonMast().isDamage() ||
                                                    newSrrItem.getReturnReasonMast().isAbsent()){
                                            approvalRequired = true;
                                    }
                                    newSrrItem.setSrrTxn(newSrrTxn);
                                    newSrrItem.getSrrTxn().setSrrId(newSrrTxn.getSrrId());
                                    srrItemTxnHome.getEntityManager().merge(newSrrItem);
                            }

                            SrrId = newSrrTxn.getSrrId();
                            srnGenerated = true;
                            if (srrTxn.getSiteWorkTxnByToSiteWorkId().getSiteWorkId() != null) {
                                    approvalRequired = true;
                            }
                            return null;
                    }

            • 3. Re: object references an unsaved transient instance - save the transient instance before flushing
              Leo van den berg Master

              Hi,


              I assume you have Entities. The srrTxn is such an Entity. This entity contains a Collection with other entities. There is your problem. You must also know that managing complex objecttrees with the Home component is not the best approach, because Home uses a find-method to retrieve an Entity.


              Leo


              P.S: if this doesn't make much sense, you should learn the basics of JPA first, before you proceed with Seam.