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



我有客户实体有帐户列表,每个实体都有审计表,所以生成的表将是: (客户 帐户 ,CUSTOMERS_AUD ,ACCOUNTS_AUD)

如何在一个修订版中将帐户更改与客户更改相关联? 休眠版本为每个表提供单独的版本(修订号)?

生成的代码表:

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

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

相关内容

  • 没有找到相关文章

最新更新