我正在尝试使用 Spring Data JPA 和 MySql 来实现这个简单的想法。我有桌子:
╔════╦═════════════════════╗
║ 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 ║
╚════╩════════╩═════════╩════════════╝
然后我想得到结果为User+Question+Answer
. 标准的 Spring Data 查询很好,answersRepository.findAll();
我得到了结果:
╔════════╦═════════╦═════════════════════╗
║ 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? ║
╚════════╩═════════╩═════════════════════╝
但是如果问题可以改变,如何实现这个逻辑呢?例如User1
回答了这个Who are you?
问题,并且在这个问题被更改为Who are u?
并User2
在第二个版本上回答之后。
我需要结果:
╔════════╦═════════╦═════════════════════╗
║ 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? ║
╚════════╩═════════╩═════════════════════╝
我尝试使用 Spring Data Envers (Hibernate Envers) 来实现这个逻辑,但我认为这不是发明审计的原因。那么我该怎么做呢?也许我需要使用事件溯源技术或其他东西?