如何将包含另一个对象的对象发送到服务器?
我正在开发一个应用程序来收集问题(类名 Question
)和答案。
在我的客户端上,有一组单选按钮可用于选择问题的主题。主题(类名 Subject
)存储在数据库中。
为了存储在数据库中,我使用 hibernate。
因此,我的 Question 类有一个 Subject
类型的属性 subject
保存问题时,我获取数据库 ID。我现在如何将正确的主题存储到数据库中?
I am developing an application to collect questions (class name Question
) and answers.
On my client there is a set of RadioButtons that can used to choose the subject of the question. The subjects (class name Subject
) are stored in the database.
For storing in the database I use hibernate.
So my Question class has a property subject that is of the type Subject
When saving a question I obtain the database ID. How can I now store the proper subject to the database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设我的评论是您面临的问题,
对于您的情况,您应该有两个单独的主题和问题版本。
一组是 DTO(数据传输对象),而另一组是持久类集。
DTO 类 SubjectDTO 和 QuestionDTO 类是 GWT 编译的,并通过服务调用与浏览器发送和接收。 QuestionDTO 包含输入的问题和从单选按钮中选择的主题名称作为原始字符串。当用户单击保存问题时,您将进行服务调用并将 QuestionDTO 发送到服务器。然后,服务器在数据库上执行主题的 findByName,从数据库中获取实际预先存在的 SubjectPercient 记录,并将其与表示的实际字符串一起放入新的 QuestionPercient 中。用户输入的问题并保存。因此,您引用了一个现有主题 - 在保存问题时为该主题创建了一个实际的外键。
为 DTO 和持久类(我将它们命名为模型和域)选择您想要的任何命名方案,但随着您的应用程序变得越来越复杂,它们最终必须分开。
Assuming my comment IS the problem you are facing,
For your case, You should have 2 separate versions of your subject and question.
One set is a DTO (Data transfer object), while other set is the persistent set of classes.
The DTO classes SubjectDTO and QuestionDTO classes are GWT compiled and are sent to and from the browser with service calls. The QuestionDTO contains the entered Question and selected subject name from the radio buttons as primitive strings. When the user clicks to save the question you make a service call and send the QuestionDTO to the server. The server then does a findByName of subject on the DB, fetches the actual pre-existing SubjectPersistent record from the DB and puts it in a new QuestionPersistent along with the actual string representing the question the user entered and saves it. Thus you have referred to an existing subject - an actual foreign key is created to the subject while saving the question.
Choose whatever naming scheme you want to for the DTOs and Persistant classes (I named them Models and Domains) but they will have to be separate eventually, as your application grows more complex.