I'm trying to implement the simple idea with Spring Data JPA and MySql. I have tables:
╔════╦═════════════════════╗
║ Id ║ Question ║
╠════╬═════════════════════╣
║ 1 ║ Who are you? ║
║ 2 ║ Whats your name? ║
║ 3 ║ Where are you from? ║
╚════╩═════════════════════╝
╔════╦════════╦═════════╦════════════╗
║ Id ║ UserId ║ Answer ║ QuestionId ║
╠════╬════════╬═════════╬════════════╣
║ 1 ║ 1 ║ Answer1 ║ 1 ║
║ 2 ║ 1 ║ Answer2 ║ 2 ║
║ 3 ║ 1 ║ Answer3 ║ 3 ║
║ 4 ║ 2 ║ Answer4 ║ 1 ║
║ 5 ║ 2 ║ Answer5 ║ 2 ║
║ 6 ║ 2 ║ Answer6 ║ 3 ║
╚════╩════════╩═════════╩════════════╝
And then I want to get the result as User+Question+Answer
. It's good with standard Spring Data queries like answersRepository.findAll();
And I get the result:
╔════════╦═════════╦═════════════════════╗
║ UserId ║ Answer ║ Question ║
╠════════╬═════════╬═════════════════════╣
║ 1 ║ Answer1 ║ Who are you? ║
║ 1 ║ Answer2 ║ Whats your name? ║
║ 1 ║ Answer3 ║ Where are you from? ║
║ 2 ║ Answer4 ║ Who are you? ║
║ 2 ║ Answer5 ║ Whats your name? ║
║ 2 ║ Answer6 ║ Where are you from? ║
╚════════╩═════════╩═════════════════════╝
But how to implement this logic if the questions can be changed?
Something like User1
answered the Who are you?
question and after this question has been changed to Who are u?
and User2
answered on the second version for example.
I need result:
╔════════╦═════════╦═════════════════════╗
║ UserId ║ Answer ║ Question ║
╠════════╬═════════╬═════════════════════╣
║ 1 ║ Answer1 ║ **Who are you?** ║
║ 1 ║ Answer2 ║ Whats your name? ║
║ 1 ║ Answer3 ║ Where are you from? ║
║ 2 ║ Answer4 ║ **Who are u?** ║
║ 2 ║ Answer5 ║ Whats your name? ║
║ 2 ║ Answer6 ║ Where are you from? ║
╚════════╩═════════╩═════════════════════╝
I tried to implement this logic using Spring Data Envers (Hibernate Envers) but this is not why the audit was invented, I think. So how can I do this? Maybe I need to use something like Event Sourcing technology or something else?
question from:
https://stackoverflow.com/questions/65946778/how-to-query-for-historical-data 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…