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

0 votes

I receive following error when I save the object using Hibernate.

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

How can we solve this error?

Sep 26, 2018 in Java by Daisy
• 8,050 points
4,004 views

1 answer to this question.

0 votes
You should add cascade="all" (if using xml) or cascade=CascadeType.ALL (if using annotations) on your collection mapping.

This happens because you have a collection in your entity, and that collection has one or more items which are not present in the database. By specifying the above options you tell hibernate to save them to the database when saving their parent.
answered Sep 26, 2018 by parth295
• 4,620 points

Related Questions In Java

0 votes
1 answer

Creating an instance using the class name and calling constructor

Yes: Class<?> clazz = Class.forName(className); Constructor<?> ctor = clazz.getConstructor(String.class); Object ...READ MORE

answered Oct 9, 2018 in Java by Daisy
• 8,050 points
35 views
0 votes
2 answers

What are all the different ways to create an object in Java?

There are different ways you could do this ...READ MORE

answered Aug 19 in Java by Sirajul
• 25,700 points
69 views
0 votes
2 answers

In Java, what is the best way to determine the size of an object?

I happened to find a java class "jdk.nashorn.internal.ir.debug.ObjectSizeCalculator", ...READ MORE

answered Aug 19 in Java by Sirajul
• 25,700 points
112 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
325 views
+1 vote
10 answers

Hibernate hbm2ddl.auto possible values and their uses

hibernate.hbm2ddl.auto (e.g. none (default value), create-only, drop, create, create-drop, validate, and update) Setting to perform SchemaManagementTool actions automatically as ...READ MORE

answered Dec 7, 2018 in Java by Shuvodip
4,432 views
0 votes
1 answer

Hibernate show real SQL

If you want to see the SQL ...READ MORE

answered Jul 5, 2018 in Java by Sushmita
• 6,880 points
78 views
0 votes
1 answer

What's the difference between @JoinColumn and mappedBy when using a JPA @OneToMany association?

The annotation @JoinColumn indicates that this entity is the owner of ...READ MORE

answered Oct 18, 2018 in Java by Sushmita
• 6,880 points
2,534 views
0 votes
1 answer

Difference between FetchType LAZY and EAGER in Java Persistence API?

Sometimes you have two entities and there's ...READ MORE

answered Dec 5, 2018 in Java by Daisy
• 8,050 points
434 views
0 votes
2 answers

One line initialization of an ArrayList object in Java

In Java 8 or earlier: List<String> string = ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,190 points
145 views
0 votes
2 answers

How to sort an ArrayList of custom object by property in Java?

You can Sort using java 8 yourList.sort(Comparator.comparing(Classname::getName)); or  yourList.stream().forEach(a -> ...READ MORE

answered Aug 13, 2018 in Java by samarth295
• 2,190 points
77 views