Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving one or more entities that have a non-nullable association with an unsaved transient entity

Tags:

hibernate

I have some POJOS which are as follows:

  1. Student.java
  2. Feedback.java
  3. Forum.java
  4. Solution.java
  5. Suggestions.java

There is one more class through which i am adding a user to the mysql database using hibernate. This is the method that does that:

            import java.util.List;
            import org.hibernate.Transaction;
            import java.util.Date;
            import java.util.HashSet;
            import java.util.Set;
            public class HibernateExample 
            {
                public static void main(String[] args)
                {
                    addUser();
                }

                private static void addUser() 
                {
                    Student user = new Student();
                    user.setEmailId("[email protected]");
                    user.setFname("fd");
                    user.setLname("dsfds");
                    user.setStuId(43);

                    Set<Feedback> feeds=new HashSet<Feedback>();
                    Feedback feed=new Feedback();
                    feed.setFeedId(1);
                    feed.setFeedDate(new Date());
                    feed.setStudent(user);
                    Suggestions suggestions=new Suggestions();
                    suggestions.setSugession("imrove hjds");
                    suggestions.setSugessionId(43);
                    suggestions.setFeedbacks(feeds);
                    feed.setSuggestions(suggestions);
                    feeds.add(feed);
                    user.setFeedbacks(feeds);


                    Set<Forum> forums=new HashSet<Forum>();
                    Forum fo=new Forum();
                    fo.setQuestion("what's the dks?");
                    fo.setQuestionDate(new Date());
                    fo.setQuestionId(23);
                    fo.setQuestionTitle("how rto");

                    Solution solution=new Solution();
                    solution.setSolId(4554);
                    solution.setSolution("get taht girl");
                    fo.setStudent(user);
                    forums.add(fo);
                    solution.setForums(forums);
                    fo.setSolution(solution);

                    user.setFeedbacks(feeds);
                    // 2. Create DAO
                    StudentDAO dao = new StudentDAO();

                    // 3. Start the transaction
                    Transaction tx = dao.getSession().beginTransaction();

                    // 4. Add user
                    dao.save(user);

                    // 5. Commit the transaction (write to database)
                    tx.commit();

                    // 6. Close the session (cleanup connections)
                    dao.getSession().close();
                }
            }   

The error that i am getting is :

            WARN: HHH000437: Attempting to save one or more entities that have a non-nullable association with an unsaved transient entity. The unsaved transient entity must be saved in an operation prior to saving these dependent entities.
                Unsaved transient entity: ([Suggestions#43])
                Dependent entities: ([[Feedback#1]])
                Non-nullable association(s): ([Feedback.suggestions])
            Exception in thread "main" org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation: Feedback.suggestions -> Suggestions
                at org.hibernate.action.internal.UnresolvedEntityInsertActions.checkNoUnresolvedActionsAfterOperation(UnresolvedEntityInsertActions.java:135)
                at org.hibernate.engine.spi.ActionQueue.checkNoUnresolvedActionsAfterOperation(ActionQueue.java:240)
                at org.hibernate.internal.SessionImpl.checkNoUnresolvedActionsAfterOperation(SessionImpl.java:709)
                at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:759)
                at org.hibernate.internal.SessionImpl.save(SessionImpl.java:749)
                at org.hibernate.internal.SessionImpl.save(SessionImpl.java:745)
                at StudentDAO.save(StudentDAO.java:32)
                at HibernateExample.addUser(HibernateExample.java:144)
                at HibernateExample.main(HibernateExample.java:15)

Although the error looks self-explanatory i am having some problems in the sequence in which i should perform the operations in add user method(). So what am i doing wrong here?

like image 705
user1788048 Avatar asked Oct 23 '25 16:10

user1788048


2 Answers

Saving version information to existing entities helped me here.

It was a different problem for me, but the error was the same.

For ex.

Entity1 was already saved in the database.

Entity2 I was trying to save, whereas Entity 1 was linked to Entity 2.

While saving Entity I was getting the same type of error message hence after checking the version information on Entity 1, I found that the version was null hence I set the version to 0 and then tried saving new Entity 2, It worked.

like image 115
Himanshu Jain Avatar answered Oct 26 '25 06:10

Himanshu Jain


Saving the Student causes both the Feedback and the Suggestions entities to be saved by some order which is unrelated to the fact that Feedback is dependent on Suggestions.

Try saving the Suggestions entity first and only then the Student (which will cause the Feedback to also be saved, but this time after Suggestions), like that:

dao.save(feed);
dao.save(user);

You might encounter additional mal-ordered save operations so you'll just have to save them separately accordingly.

like image 35
yair Avatar answered Oct 26 '25 06:10

yair



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!