hibernate 保存与未保存的临时实体具有不可空关联的一个或多个实体

kognpnkq  于 2023-10-23  发布在  其他
关注(0)|答案(2)|浏览(113)

我有一些POJOS如下:

  1. Student.java
  2. Feedback.java
  3. Forum.java
  4. Solution.java
  5. Suggestions.java
    还有一个类,我通过它使用Hibernate向MySQL数据库添加用户。这是一个方法,可以做到这一点:
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();
                }
            }

我得到的错误是:

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)

虽然这个错误看起来不言自明,但我在执行add user方法()中的操作的顺序上遇到了一些问题。我到底做错了什么?

yhived7q

yhived7q1#

保存Student会导致FeedbackSuggestions实体都以某种顺序保存,这与Feedback依赖于Suggestions的事实无关。
尝试先保存Suggestions实体,然后才保存Student(这将导致Feedback也被保存,但这次是在 * Suggestions之后),如下所示:

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

您可能会遇到其他无序的保存操作,因此您只需相应地分别保存它们。

0yycz8jy

0yycz8jy2#

将版本信息保存到现有实体对我有帮助。

这对我来说是一个不同的问题,但错误是一样的。
为了前男友。

权限1已保存在数据库中。
实体2我试图保存,而实体1链接到实体2。

虽然保存实体我得到相同类型的错误消息,因此在检查实体1的版本信息后,我发现版本为空,因此我将版本设置为0,然后尝试保存新的实体2,它工作了。

相关问题