繁体   English   中英

我正在尝试使用休眠envers连接2个表以具有相同的转速编号?

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

我有具有帐户列表的客户实体,并且每个实体都有经过审核的表,因此生成的表将是:(CUSTOMERS,ACCOUNTS,CUSTOMERS_AUD,ACCOUNTS_AUD)

如何在一个转帐号中将帐户更改与客户更改联系起来? 休眠envers为每个表提供单独的版本(版本号)?

我的代码生成的表:

CUSTOMERS_AUD表格

   | 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表格

   | 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表格

   | 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表格

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

春季启动应用程序

@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实体。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM