• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1264
  • Last Modified:

Getting a Hibernate TransientObjectException when attempting to do a session.saveOrUpdate

I am new to Hibernate but we are using it in our project.  I am attempting to save a class called FENode that is used to build a tree using 2-D graphics in a Java application.  The FENodes can have a single parent and one or more child nodes.  Both the parent and children nodes are also FENodes.  The FENodes use a class called IOPad that is their connector through which the parent and children FENodes are connected.  When I attempt to add a new child FENode to a another FENode in the graph I get the hibernate exception:

 "org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: IOpad".

I am getting the session and then doing a
session.saveOrUpdate(FENodeRoot);

where FENodeRoot is the root node of the 2-D graph.

Thank You
0
gdkinney_2
Asked:
gdkinney_2
  • 4
  • 4
1 Solution
 
Bart CremersJava ArchitectCommented:
You need to call save on the IOpad first and then on the FENodeRoot.

Or you need to make sure you get the cascade setting right in the mapping:

     cascade="save-update,persist"

for the child nodes and the IOPad property in the FENodes.
0
 
gdkinney_2Author Commented:
Here is the part of my code that does the saving I recursively make a pass through the FeNode tree and save all the IOPads each FeNode has one output IOPad and one or more input IOPads I first save all these for each FENode in the tree.  

Then I recursively make another pass through the FENode tree and save all the FENodes in the tree.

But I am still getting the same error.  Here is the code that does this:

 public boolean doWriteChanges()  {
            saveIOPads(trees.get(0));
            saveTree(trees.get(0));
            capKey.setType(trees.get(0).getOutputType());
            model.put(capKey,  trees.get(0));
            return true;
    }

    private void saveIOPads(FENode n)  {
        if (n == null) {
            return;
        }
        FENode child = null;

        // Save the FENode output IOPad
        saveIt(n.getOutputPad());

        for (IOpad iPad : n.getInputs()) {
            saveIt(iPad);
            if ((child = iPad.getAttachedNode()) != null) {
                saveIOPads(child);
            }
        }  
    }    
   
    private void saveTree(FENode n)  {
        if (n == null) {
            return;
        }
        FENode child = null;
        saveIt(n);
        for (IOpad iPad : n.getInputs()) {
            if ((child = iPad.getAttachedNode()) != null) {
                saveTree(child);
            }
        }  
    }
   
    public void saveIt(Object obj)  {
        try {
            session.saveOrUpdate(obj);
            session.flush();
        } catch (ObjectBrokerException ex) {
            ex.printStackTrace();
        }
    }
0
 
Bart CremersJava ArchitectCommented:
I think it would be easier to make sure you get the cascades right in the mapping. By getting those right you can simply save the root and hibernate will take care of the rest.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
gdkinney_2Author Commented:
I tried setting those to the setting you gave but that did not work that is why I tried this approach.  The hbm file is very complicated and I am afraid by altering it I might effect someone elses code.
0
 
Bart CremersJava ArchitectCommented:
The change you'll affect someone else code by simply adding those cascade settings is very small.
0
 
gdkinney_2Author Commented:
But why does my method of saving all the FENodes IOPads first then saving the FENodes themsevles fail where I still get the same error message?
0
 
gdkinney_2Author Commented:
Besides I tried the change to the hbm file for the cascade and it did not work either so I backed out of that.
0
 
Bart CremersJava ArchitectCommented:
The cause for your method failing is that you probable miss some entities using your method. Altering the hbm and getting that right will force hibernate to do all the work for you.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now