• Register
0 votes
174 views

Problem :

I am facing the following error when I save the object using Hibernate:
object references an unsaved transient instance - save the transient instance before flushing:
6 5 3
6,930 points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

Please include cascade="all" if using xml or cascade=CascadeType.ALL if using annotations on your collection mapping.

This is happening because you have the collection in your entity, and that collection has one or more items as well which are not present in the database. So by specifying the above options you are telling hibernate to save them to the database while saving their parent.

If you follow above procedure then your issue will be resolved.

9 7 4
38,600 points
0 votes

Solution:

You must add cascade="all" (if using xml) or cascade=CascadeType.ALL (in case using annotations) on your collection mapping.

This occurs since you have a collection in your entity, and that collection has one or more items which are not present in the database. By secluding the above options you say hibernate to save them to the database at the time saving their parent.

I conceive this might be only repeat answer, however only to clarify, I obtained this on a @OneToOne mapping as well as a @OneToMany. In both instances, it was the fact that the Child object I was including to the Parent wasn't saved in the database yet. Therefore at the time I included the Child to the Parent, then saved the Parent, Hibernate would toss the "object references an unsaved transient instance - save the transient instance before flushing" message at the time saving the Parent.

including in the cascade = {CascadeType.ALL} on the Parent's reference to the Child solved the problem in both instances. This saved the Child and the Parent.

@OneToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "performancelog_id")
public PerformanceLog getPerformanceLog() {
    return performanceLog;
}

This occurs at the time saving an object at the time Hibernate thinks it requires to save an object that is associated with the one you are saving.

I had this issue and did not want to save changes to the referenced object. Therefore I wanted the cascade type to be NONE.

The trick is to make sure that ID and VERSION in the referenced object is place so that Hibernate does not figure out that the referenced object is a new object that requires saving. This performed for me.

Look through all of the relationships in the class you are saving to perform out the associated objects (and the associated objects of the associated objects) and make sure the ID and VERSION is place in all objects of the object tree.

Or, in case you want to employ minimal "powers" (for example in case you don't want a cascade delete) to achieve what you want, use

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

...

@Cascade({CascadeType.SAVE_UPDATE})
private Set<Child> children;

In my instances it was happened by not having CascadeType on the @ManyToOne side of the bidirectional relationship. To be more precise, I had CascadeType.ALL on @OneToMany side and did not have it on @ManyToOne. Including CascadeType.ALL to @ManyToOne resolved the problem. One-to-many side:

@OneToMany(cascade = CascadeType.ALL, mappedBy="globalConfig", orphanRemoval = true)
private Set<GlobalConfigScope>gcScopeSet;

Many-to-one side (caused the problem)

@ManyToOne
@JoinColumn(name="global_config_id")
private GlobalConfig globalConfig;

Many-to-one (fixed by including CascadeType.PERSIST)

@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name="global_config_id")
private GlobalConfig globalConfig;

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 45 views
45 views
Problem: why I cant fix this : Unable to create requested service [org.hibernate.engine.jdbc.env.spi.jdbcenvironment] .
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 4.1K views
4.1K views
Problem : I want to configure the hibernate orm mapping tool to my java class and want to use PostgreSQL as my database also I have configured the password as "password". But when I tried to run the application, I am facing following error ... in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
asked Dec 4, 2019 alecxe 7.5k points
0 votes
2 answers 1.9K views
1.9K views
Problem : I have some objects in the following configuration: · A has the many-to-many relationship with B. (B has inverse="true") · B has the many-to-one relationship with C. (I have cascade set to "save-update") · C is the kind of type or ... that for architectural reasons beyond my control each read or write needs to be done in a separate session. Does anybody has any solution on my issue?
asked Nov 27, 2019 alecxe 7.5k points
0 votes
1 answer 23 views
23 views
Problem: Why I got the error :javax.persistence.transactionrequiredexception: executing an update/delete query.
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 3.5K views
3.5K views
Problem : I am getting bellow error while using hibernate JPA with spring and mongodb and running my application on Glassfish-4.0. javax.persistence.transactionrequiredexception: executing an update/delete query
asked Oct 19, 2019 peterlaw 6.9k points
0 votes
1 answer 1 view
1 view
Problem: please solve it ... From the official documentation:
asked Apr 1 Ifra 24.4k points
0 votes
1 answer 10 views
10 views
Problem: I need help in solving this problem: The internal connection pool has reached its maximum size and no connection is currently available!
asked Mar 14 Wafa Abu Yousef 6.1k points
0 votes
1 answer 7 views
7 views
Problem: the database returned no natively generated identity value.
asked Feb 22 Dan phillip 4.7k points
0 votes
1 answer 1.7K views
1.7K views
Problem : I am using the Hibernate for a first time for my project and I am very new to Hibernate. I think I have followed all the instructions written in some tutorials but still I keep getting the below Exception that is in my title: Exception in thread "main" ... should just change a connection.pool_size property, but if I do so it guesses that I only will get more errors. Can anyone help me?
asked Jan 30, 2020 jwilliam 3.9k points
0 votes
2 answers 146 views
146 views
Problem : Here I want to copy all the objects in the List with object1 to a list with object2: Following is my code snip: List<EmployeeAmManegmentModel> AM = employee.amEmployeeDeatails(); List<EmployeeRmManegmentModel> RM = employee.rmEmployeeDeatails(); List< ... Long status; private String employeeType; How can I copy the result from one list of different object type to another?
asked Dec 3, 2019 alecxe 7.5k points