views:

275

answers:

1

I have a collection of Answer's (IList<Answer>) and two tables(Answer and AnswerHist). I want to do something along the lines of:

Get the current answers from db.

save(Answers, "AnswerHist");
delete(Answers, "Answer");

Answers = questions
                            .Select(x => new Answer(response)
                            {
                                QuestionID = x.QuestionID,
                                Value = x.Answer.Value,
                                Bool = x.Answer.Bool,
                                Date = x.Answer.Date,
                                Number = x.Answer.Number,
                                Text = x.Answer.Text
                            })
                            .ToList<Answer>();

save(Answers, "Answer");

Basicly, save the answers to the history table, delete the answers from the answer table, then update the answers and insert them in the answer table.

Any easy way to make hibernate save the same class vs multiple identical tables?

+1  A: 

As far as I know, the mapping between class and table is static and cannot be changed. You could make a AnswerHistory class and map that to the AnswerHist table:

public class AnswerHistory : Answer {
    public AnswerHistory (Answer answer) {
        this.QuestionID = answer.QuestionID;
        this.Value = answer.Value;
        // ...
    }
}

Then you would use it like this:

save(Answers.Select(answer => new AnswerHistory(answer)));
delete(Answers);

Answers = new stuff;
save(Answers);

Another option is to add a boolean in the answer whether or not it is in the history; then you would only have to set that boolean and save (to the same table).

configurator