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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:04:56+00:00 2026-05-25T15:04:56+00:00

I got a class with a one-to-one relation. Basically, in class A I have

  • 0

I got a class with a one-to-one relation. Basically, in “class A” I have a one-to-one relation to “class B”. This relation uses a primary key join column. Now my issue is as follows, if I try and create a instance of A, I cant save it because I haven’t added a instance of B to it yet. But, I cant create a instance of B because I need the id of A first.

A easy solution would be to make the ID in B the automatically generated one, so I could then create a instance of B before creating a instance of A. However, I’m sure there is a better way of doing this? 🙂

I can see in the database that hibernate created a additional index on the id column of A, which im guessing is a foreign key constraint. And I can see the the documentation that the XML version of the one-to-one mapping have a attribute to specify if the relation is constrained or not, however, the @OneToOne annotation doesnt seem to have this option? :S

  • 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-25T15:04:56+00:00Added an answer on May 25, 2026 at 3:04 pm

    It seems you have two relationships between A and B tables (you have: A has a_id, b_id; B has b_id, a_id). To model one to one you need only one relationship. Determine which table is ‘main’ and then drop column from ‘secondary’ table (should be: A has a_id, b_id; B has b_id). After that hibernate (and any other schema client) will be able to insert to B first, then to A with reference to B table.

    For example for egg and chicken. There are multiple relations between eggs and chickens (one chicken can laid many eggs; one egg can produce one chicken). So for the one to one relationship egg-produces-chicken, it is reasonable to have parent_egg_id column in chicken table, so an egg can be created first and then a chicken with reference to that egg.

    Hibernate mapping could look like the following:

    In Chicken class:

    @OneToOne
    @JoinColumn(name = "parent_egg_id")
    public Egg getParentEgg() {
        return parentEgg;
    }
    

    In Egg class:

    @OneToOne(mappedBy = "parentEgg")
    public Chicken getChildChicken() {
        return childChicken;
    }
    

    Update:

    The same thing as constrained in xml, optional property in OneToOne interface will do. It is defaulted to true, so the relationship is nullable by default.

        /** 
         * (Optional) Whether the association is optional. If set 
         * to false then a non-null relationship must always exist.
         */
        boolean optional() default true;
    

    According to your comments rows to A are inserted first. I would consider having dependency from B to A, not from A to B. In this case to create item in A then in B, two insert statements are required (with relation from A to B – additional update A statement is required.).

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

Sidebar

Related Questions

I've got a class with many string arrays. I'd like to have one generic
I have this class in one file and item class in another file in
I've got a class, like this one: class A attr_accessor(:field2) attr_accessor(:field1) end What's the
In a WPF application, I've got my events centralized in one class like this:
I've got one class, that I sort it already by one attribute. Now I
I've got a class hierarchy as this one: class A { } // class
I've got the following question. I've got one class Instellingen that's a field in
I've got one really big .java class file that has a lot of members.
Say I've got a class like this: class Test { int x; SomeClass s;
I've got a class with one public method and many private methods which are

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.