Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
77 views
in Technique[技术] by (71.8m points)

java - How to query for historical data

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It really depends on the semantics of changing the question and the business relevance of it.

So let us consider a couple of variants.

  1. Envers is the right tool if this is really about auditing. I.e. 99.9% of your application don't care about the different variants. Queries tend to get rather complicated since you have to figure out the correct revisions while revisions of different entities are completely independent. This is why you want to use it only for very few scenarios.

  2. You don't care really about all the variants of the question that existed, you just need to make sure that for an answer you have the exact question that was present when the answer was posted. In this case you should just copy the question text into an extra column of the answer table. This sounds like data duplication, but it isn't. The question table holds the current questions. The answer table holds the question at the time of answering it. When one gets updated the other one won't.

  3. If you need to keep track of all the variants of a question, you should add a variant field in the question table which will be part of the primary key and instead of changing a question it gets copied with an incremented variant-value.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...