我正在尝试使用 hibernate envers 连接 2 个表以具有相同的版本号?

I am trying to connect 2 tables to have same rev number using hibernate envers?

我的客户实体有账户列表,每个实体都审计了 tables,因此生成的 tables 将是: (顾客 ,账户 ,CUSTOMERS_AUD ,ACCOUNTS_AUD)

如何在一个rev number中关联账户变更和客户变更? hibernate envers 为每个 table ?

提供单独的版本(修订版号)

我的代码生成的tables:

CUSTOMERS_AUD TABLE

   | ID  | REV   | REVTYPE |name  |account_num|ACCOUNTID|
   |:----|------:|:-------:|:----:|:---------:|:-------:|
   | 1   |  1    |     0   |Ann   |1234567897 |1        |
   | 1   |  3    |     1   |Alex  |1234567897 |1        |
   | 1   |  5    |     1   |Alex  |7777777777 |1        |

ACCOUNTS_AUD TABLE

   | ID         | REV   | REVTYPE |name  |account_num|
   |:-----------|------:|:-------:|:----:|:---------:|
   | 1          |  2    |     0   |Ann   |1234567897 |
   | 1          |  4    |     1   |Alex  |7777777777 |

示例实体

    @Entity

    @Table(name="CUSTOMERS")
    @Audited
    public class Customer  implements Serializable {
    
    .
    .
    
    @ManyToOne
    @org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.REPLICATE)
    @JoinColumn(name="ACCOUNTNUMBERCOMBOBOXID", nullable=true)
    private Account accountNumberComboBox;
    
    public static final String REF_CUSTOMERS_ACCOUNTS = "refCustomersAccounts";
    @OneToMany(mappedBy = "refCustomers")
    @org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.REPLICATE)
    private List<Account> refCustomersAccounts;
    .
    .
    .
    
    }
    
    @Entity
    
    @Table(name="ACCOUNTS")
    @Audited
    public class Account  implements Serializable {
    
    .
    .
    
    public static final String REF_CUSTOMERS = "refCustomers";
    @ManyToOne
    @org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.REPLICATE)
    @JoinColumn(name="REFCUSTOMERSID", nullable=true)
    private Customer refCustomers;
    .
    .
    .
    
    }

我想要的结果

CUSTOMERS_AUD TABLE

   | ID  | REV   | REVTYPE |name  |account_num|ACCOUNTID|
   |:----|------:|:-------:|:----:|:---------:|:-------:|
   | 1   |  1    |     0   |Ann   |1234567897 |1        |
   | 1   |  2    |     1   |Alex  |1234567897 |1        |
   | 1   |  3    |     1   |Alex  |7777777777 |1        |

ACCOUNTS_AUD TABLE

   | ID         | REV   | REVTYPE |name  |account_num|
   |:-----------|------:|:-------:|:----:|:---------:|
   | 1          |  1    |     0   |Ann   |1234567897 |
   | 1          |  2    |     0   |Ann   |1234567897 |
   | 1          |  3    |     1   |Alex  |7777777777 |

Spring 启动应用程序

@SpringBootApplication
public class JpaDemoApplication implements CommandLineRunner {

    @Autowired
    private ApplicationContext context;

    public static void main(String[] args) {
        SpringApplication.run(JpaDemoApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        CustomerRepo customerRepo = context.getBean(CustomerRepo.class);
        AccountRepo accountRepo = context.getBean(AccountRepo.class);
        AccountTypeRepo accountTypeRepo = context.getBean(AccountTypeRepo.class);

        Accounts account = new Accounts();
        Customers customer = new Customers();

        customer.setId(1L);

        account.setId(1L);
        account.setFromDate(new Date());
        account.setToDate(new Date());
        account.setNewAcountNumber("1234567897");
        account.setOwner("Ann");


        customer.setAccountNumber(account.getNewAcountNumber());
        customer.setCode("CODE");
        customer.setName(account.getOwner());
        customer.setFromDate(new Date());
        customer.setToDate(new Date());
        customer.setAccountNumberComboBox(account);

        accountRepo.save(account);

        customerRepo.save(customer);//0

        customer.setName("Alex");
        customerRepo.save(customer);//1

        account.setNewAcountNumber("7777777777");
        accountRepo.save(account);

    }
}

帐户回购

@Repository
public interface AccountRepo extends CrudRepository<Accounts,Long> {
}

客户回购

@Repository
public interface CustomerRepo extends CrudRepository<Customers, Long> {
}

修订号与交易相关联,因此您在单个交易中执行的任何操作都将始终使用相同的修订号进行审核和存储。

由于您没有显示您的持久性代码,我现在只能猜测该代码可能会在一个事务中保存 Customer 实体,而在后续事务中您将保存 Account实体。

如果您将它们保存在同一个事务中,它们将获得分配给它们审计行的相同修订号。