gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 05:27:41 29 4
gpt4 key购买 nike

我有一些 POJOS 如下:

  1. 学生.java
  2. 反馈.java
  3. 论坛.java
  4. 解决方案.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("harshal@gmail.com");
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)

虽然错误看起来不言自明,但我在序列中遇到了一些问题我应该在其中执行添加用户方法()中的操作。那我在这里做错了什么?

最佳答案

保存 Student 会导致 FeedbackSuggestions 实体按某种顺序保存,这与 反馈 取决于建议

尝试先保存 Suggestions 实体,然后才保存 Student(这将导致 Feedback 也被保存,但这次 < em>在 建议)之后,像那样:

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

您可能会遇到其他顺序错误的保存操作,因此您只需相应地单独保存它们即可。

关于hibernate - 保存一个或多个与未保存的 transient 实体具有不可空关联的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17907259/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com