数据库中的Tobuilder()与设置器
我试图以双向方式将数据库保存在数据库中。我已经使用了获取器和设置器,并在数据库中获得了不同的结果。 通过使用setter:
Question question = Question.builder().quest("What is Pythonn ?").answer(null).build();
Answer answer = Answer.builder().answer("Python is language").que(question).build();
answer.setQue(question);
question.setAnswer(answer);
Question question2 = Question.builder().quest("How are you ?").answer(null).build();
Answer answer2 = Answer.builder().answer("I am fine").build();
answer2.setQue(question2);
question2.setAnswer(answer2);
session.save(question);
session.save(question2);
中的结果
db <
a href =“ https://i.sstatic.net/a3ef2.png” 。 sstatic.net/aookn.png“ rel =“ nofollow noreferrer”>答案表
通过使用Tobuilder,
answer.toBuilder().que(question).build();
question = question.toBuilder().answer(answer).build();
answer2.toBuilder().que(question2).build();
question2 = question.toBuilder().answer(answer2).build();
DB中的结果: Quartion_table
为什么在设置器正常工作时,dobuilder不起作用?
I am trying to save data in database with OneToOne mapping in bidirectional way. I have used toBuilder and setter and got different result in Database.
By using setter:
Question question = Question.builder().quest("What is Pythonn ?").answer(null).build();
Answer answer = Answer.builder().answer("Python is language").que(question).build();
answer.setQue(question);
question.setAnswer(answer);
Question question2 = Question.builder().quest("How are you ?").answer(null).build();
Answer answer2 = Answer.builder().answer("I am fine").build();
answer2.setQue(question2);
question2.setAnswer(answer2);
session.save(question);
session.save(question2);
the result in db
By using toBuilder
answer.toBuilder().que(question).build();
question = question.toBuilder().answer(answer).build();
answer2.toBuilder().que(question2).build();
question2 = question.toBuilder().answer(answer2).build();
The result in db:
question_table
Why toBuilder is not working while setter is working ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不会在答案2中保存对问题2的引用。这就是为什么答案2对问题列的零值。
然后您在构建器中使用问题2。这就是为什么问题1有多个条目。
对于第一个问题行带有零值的答案,我不确定它来自哪里。
You don't save a reference to question2 in answer2. That's why answer 2 has a null value for the question column.
And you use question in the builder for question2. That's why there are multiple entries for question 1.
As for first question row with a null value for answer, I'm not sure where that's coming from.