HQL Query Error: object references an unsaved transient instance

I am getting the following error when trying to execute an HQL query:

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

The object it is complaining about is a new object that I created. But that new object has no impact on the results of my query. The model object is only very loosely related to any object selected by my query. I do not understand why Hibernate is throwing an exception.

I have worked around this in the past by keeping new objects hidden from Hibernate until after I run my queries. If I create the model objects but don’t associate them to other existing objects, then Hibernate does not know they exist yet. I create the associations right before I am ready to save the new data. I cannot do that in this case. There has to be a better way to do this.

Why does Hibernate think that my new data will have an impact on the results of the query? Is it because of the way I set up my relationships? Almost all of my relationships in my database are defined in Hibernate, which means that nearly every object could be linked to any other object in some way.

I investigated the issue more. It makes more sense now, I think. The error message still seems incorrect, but I see now how there could be an error.

The relationships I am dealing with are similar to:
A --> B --> C
A --> D --> E

During this process, A, B and D are retrieved from the database and updated. I add C and E records. Then I query the database to find more B objects to link to A and to add more C objects. The error I get is that there are unsaved E objects, which doesn’t make sense. I think the real problem is that there are unsaved B objects. Maybe Hibernate is selected the lowest level unsaved object when it generates the exception?

I know from the data that the B objects I am retrieving are not going to be the B objects that were updated, but I can see why Hibernate might not realize that. Is there any way I can tell Hibernate to ignore that possible conflict? Or is there a way I can modify my query to exclude the objects that are already loaded and updated?