Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4039382
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:36:12+00:00 2026-05-20T12:36:12+00:00

im using JPA2 with Hibernate 3.6.x I have made a simple testing on the

  • 0

im using JPA2 with Hibernate 3.6.x

I have made a simple testing on the @Version.

Let’s say we have 2 entities,

  1. Entity Team has a List of Player Entities, bidirectional relationship, lazy fetchtype, cascade-type All
  2. Both entities have @Version

And here are the scenarios :

  1. Whenever a modification is made to one of the team/player entity, the team/player’s version will be increased when flushed/commited (version on the modified record is increased).

  2. Adding a new player entity to team’s collection using persist, the entity the team’s version will be assigned after persist (adding a new entity, that new entity will got it’s version).

  3. Whenever an addition/modification/removal is made to one of the player entity, the team’s version will be increased when flushed/commited. (add/modify/remove child record, parent’s version got increased also)

I can understand the number 1 and 2, but the number 3, i dont understand, why the team’s version got increased ?

And that makes me think of other questions :

  1. What if i got Parent <-> child <-> granchildren relation ship. Will an addition or modification on the grandchildren increase the version of child and parent ?
  2. In scenario number 2, how can i get the version on the team before it’s commited, like perhaps by using flush ? Is it a recommended way to get the parent’s version after we do something to the child[s] ?

Here’s a code sample from my experiment, proving that when ReceivingGoodDetail is the owning side, and the version got increased in the ReceivingGood after flushing. Sorry that this use other entities, but ReceivingGood is like the Team, ReceivingGoodDetail is like the Player. 1 ReceivingGood/Team, many 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());
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:36:12+00:00Added an answer on May 20, 2026 at 12:36 pm

    It looks like a bug in Hibernate.

    JPA Specification says:

    All non-relationship fields and properties and all relationships owned by the entity are
    included in version checks

    However, Hibernate also increments version after change of non-owned relationship properties (whereas, for example, EclipseLink doesn’t do it). This behaviour can be disabled by setting @OptimisticLock(exclude = true) on the property.

    Note that it’s only applicable to changes of relationship property itself, not to changes in the state of referenced objects, so that version of parent wouldn’t be changes due to changes in collection of grandchildren.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using JPA 2 with Hibernate 3.6.8 as the implementation. Let's say we have
I am trying to design an entity model using JPA2.0 via hibernate. I have
I have an application using Spring 3.0.5, JPA2 and Hibernate 3.6.7. Maven's handling my
If I'm using an ORM like JPA2 - where I have my entities that
I am developing a tool that handles some database entities using JPA2, with hibernate
Hi I got a problem with mapping my entities. I'm using JPA2 and Hibernate
I'm using JPA 2 with Hibernate 3.x. I have two Entity objects Foo and
Hi I am using JPA2 with Hibernate implementation and I got a simple mapping
I have two classes say Foo and Bar mapped as @OneToOne (bidirectional) using Hibernate
I using Hibernate 3.6 Final, Guice-persist and JPA2. So good, I have everything configured,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.