How do I send an Object containing another object to the server?
I am developing an application to collect 开发者_Go百科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?
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.
精彩评论