Я использую JPA2 с Hibernate 3.6.x
Я провел простое тестирование @Version.
Допустим, у нас есть 2 объекта,
А вот сценарии:
Каждый раз, когда в одну из сущностей команды / игрока вносятся изменения, версия команды / игрока будет увеличиваться при сбросе / фиксации (увеличивается версия в измененной записи).
Добавление нового объекта игрока в коллекцию команды с использованием настаивать, сущность, версия команды будет назначена после сохранения (добавление новой сущности, эта новая сущность получит свою версию).
Каждый раз, когда добавление / изменение / удаление выполняется для одной из сущностей игрока, версия команды будет увеличиваться когда сброшен / зафиксирован. (добавить / изменить / удалить дочернюю запись, версия родительской также увеличилась)
Я могу понять число 1 и 2, но число 3, я не понимаю, почему версия команды увеличилась?
И это заставляет меня подумайте над другими вопросами:
Вот пример кода из моего эксперимента, доказывающий, что, когда ReceivingGoodDetail является стороной-владельцем, а версия была увеличена в ReceivingGood после промывание. Извините, что здесь используются другие сущности, но ReceivingGood похоже на команду, а ReceivingGoodDetail похоже на Player. 1 ReceivingGood / Team, многие ReceivingGoodDetail / Player.
/*
Hibernate: select receivingg0_.id as id9_14_, receivingg0_.creationDate as creation2_9_14_, .. too long
Hibernate: select product0_.id as id0_4_, product0_.creationDate as creation2_0_4_, .. too long
before persisting the new detail, version of header is : 14
persisting the detail 1c9f81e1-8a49-4189-83f5-4484508e71a7
printing the size of the header :
Hibernate: select details0_.receivinggood_id as receivi13_9_8_, details0_.id as id8_, details0_.id as id10_7_, .. too long
7
after persisting the new detail, version of header is : 14
Hibernate: insert into ReceivingGoodDetail (creationDate, modificationDate, usercreate_id, usermodify_id, version, buyQuantity, buyUnit, internalQuantity, internalUnit, product_id, receivinggood_id, supplierLotNumber, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: update ReceivingGood set creationDate=?, modificationDate=?, usercreate_id=?, usermodify_id=?, version=?, purchaseorder_id=?, supplier_id=?, transactionDate=?, transactionNumber=?, transactionType=?, transactionYearMonth=?, warehouse_id=? where id=? and version=?
after flushing, version of header is now : 15
*/
public void addDetailWithoutTouchingCollection() {
String headerId = "3b373f6a-9cd1-4c9c-9d46-240de37f6b0f";
ReceivingGood receivingGood = em.find(ReceivingGood.class, headerId);
// create a new detail
ReceivingGoodDetail receivingGoodDetailCumi = new ReceivingGoodDetail();
receivingGoodDetailCumi.setBuyUnit("Drum");
receivingGoodDetailCumi.setBuyQuantity(1L);
receivingGoodDetailCumi.setInternalUnit("Liter");
receivingGoodDetailCumi.setInternalQuantity(10L);
receivingGoodDetailCumi.setProduct(getProduct("b3e83b2c-d27b-4572-bf8d-ac32f6de5eaa"));
receivingGoodDetailCumi.setSupplierLotNumber("Supplier Lot 1");
decorateEntity(receivingGoodDetailCumi, getUser("3978fee3-9690-4377-84bd-9fb05928a6fc"));
receivingGoodDetailCumi.setReceivingGood(receivingGood);
System.out.println("before persisting the new detail, version of header is : " + receivingGood.getVersion());
// persist it
System.out.println("persisting the detail " + receivingGoodDetailCumi.getId());
em.persist(receivingGoodDetailCumi);
System.out.println("printing the size of the header : ");
System.out.println(receivingGood.getDetails().size());
System.out.println("after persisting the new detail, version of header is : " + receivingGood.getVersion());
em.flush();
System.out.println("after flushing, version of header is now : " + receivingGood.getVersion());
}