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 4053836
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:29:52+00:00 2026-05-20T14:29:52+00:00

I have two classes say Foo and Bar mapped as @OneToOne (bidirectional) using Hibernate

  • 0

I have two classes say Foo and Bar mapped as @OneToOne (bidirectional) using Hibernate (3.6.1 final) with JPA (2.0) like –

@Entity
public class Foo{
    @Id
    private Long id;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "foo")
    private Bar bar;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "foo")
    private Qux qux;

    @Version
    private int version;

    // getters and setters omitted
}

@Entity
public class Bar{
    @Id
    private Long id;

    @OneToOne
        @JoinColumn(name = "foo_id",  nullable = false)
    private Foo foo;

    // getters and setters omitted
}

@Entity
public class Qux {
    @Id
    private Long id;

    @OneToOne
        @JoinColumn(name = "foo_id",  nullable = false)
    private Foo foo;

    // getters and setters omitted
}

Note that – Bar and Qux doesn’t have @Version column

If we update Bar then hibernate will not increment the version of Foo and same for Qux. But our business logic needs – if someone updates Bar in the Foo and other thread is trying to update the Qux of the same Foo but doesn’t have updated Bar and vice versa then such updates should fail.
Since hibernate doesn’t update the version property of Foo if we update the Bar, we decided to update the version of Foo manually (I know it’s pretty weird and not recommended) if we update the Bar and Qux.
It works perfectly fine.. but I’m worry about some corner cases in concurrency which may fail this or will have unintended behavior.
Is it safe to do this kind of tweak with version for this purpose? OR is there any other better alternative to this (I’ve already tried optimistic/pessimistic force increament)

  • 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-20T14:29:53+00:00Added an answer on May 20, 2026 at 2:29 pm

    The correct way to force version update is the following:

    em.lock(entity, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
    

    It’s intended to be used in such cases.

    Other methods of EntityManager that take LockModeType can be used as well.

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

Sidebar

Related Questions

I have two classes, Foo and Bar, that have constructors like this: class Foo
Let's say I have two classes Foo and Bar, and I want to make
Let's say we have two classes, Foo and Foo Sub, each in a different
Say I have two classes like this: class A{ private static Random random =
Suppose I have two classes like so: class Parent def say I am a
I have got two classes, one a subclass of the other (say Animal and
Let's say I have two models, Classes and People. A Class might have one
I have two classes declared like this: class Object1 { protected ulong guid; protected
Say I have a class foo with an object of class bar as a
Let's say that I have two classes A and B. class A { private:

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.