• Register
0 votes
124 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 2.9K views
2.9K 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.1K views
1.1K 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 2.8K views
2.8K 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.5K views
1.5K 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 103 views
103 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
0 votes
1 answer 789 views
789 views
Problem : I am trying to display Data from DB.But facing error as followsthe server encountered an unexpected condition that prevented it from fulfilling the request.
asked Nov 15, 2019 peterlaw 6.9k points
0 votes
1 answer 207 views
0 votes
1 answer 51 views
51 views
Problem: I am working on Hibernate and now I am facing what I believe is a simple problem with the Hibernate but still I can't solve it. I have written a simple class I would like to persist but still keep facing below error: SEVERE: Field 'id' doesn't have a ... that is not working for me. Initializing the id also did not help me either! I am looking for Hibernate expert to fix my above error.
asked Jul 30, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 1.8K views
1.8K views
Problem : I am a completely novice in the hibernate world and facing below error, WARN: GenerationTarget encountered exception accepting command : Error executing DDL via JDBC Statement org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement The ... hibernate 4 version all my code runs very well. I looked for many solutions but they are not working for me.
asked Feb 4, 2020 jwilliam 3.9k points
0 votes
2 answers 574 views
574 views
Problem : I am new to the Spring and JPA, wasted 10 days and no result with the searching on internet. I want to save the object to SQL SERVER, my connection is correct but when I write .flush() I get below exception &ldquo;nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress&rdquo;
asked Jan 21, 2020 jwilliam 3.9k points