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

  • Home
  • SEARCH
  • 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 6689181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:29:23+00:00 2026-05-26T05:29:23+00:00

I have two entitys, A and B. A has a one to one relationship

  • 0

I have two entitys, A and B. A has a one to one relationship of B.

    class A {
        String aValue;
        B b;
    }
    class B {
       String bValue;
    }

The class B stuff are already pre populated. Now, a user on a website selects what B he wants to submit. On the server we get an Id of what B that was.

The Question:
How do I create a new A, without actually do a DB query asking for the B that has this id. I mean, the A table in database only has a id reference. One should be able to set that Id without fetching the B.

  • 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-26T05:29:23+00:00Added an answer on May 26, 2026 at 5:29 am

    If you are using JPA and Assuming the following:

    @Entity
    @Table(name="A")
    class A {
        @Id
        @Column(name="id")
        private int id;
        String aValue;
        @OneToOne
        B b;
        // Getters, setters and other stuff
    }
    
    @Entity
    @Table(name="B")
    class B {
       @Id
       @Column(name="id")
       private int id;
       Integer Id;
       String bValue;
       // Getters, setters and other stuff
    }
    

    If you now the id of entity B. You can simply make persist as follows:

    B b = new B();
    b.setId(1000); //Assuming that you know the id. 
                   //There is no necessary fill all the object. Just the PK is needed.
    A a = new A();
    a.setId(100);
    a.setAValue("nothing");
    a.setB(b);
    em.persist(a);
    

    I have tested this with Hibernate as Persistence Provider. If you are using other ORM please specify it.

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

Sidebar

Related Questions

I have two entities: products and bundles. Each one has its class. A product
I have a data model which has two entities in a relationship of one
I have two entities, Entity1 and Entity2, on a one to many relationship. On
I have two custom entites, Product and ProductType, linked together in many-to-one relationship. Product
I have this problem: have two entyties with one to one relationship, boot of
I have two tables in my database which have a one-to-one relationship. I want
i have two entities named Parent and Child , linked in a one-to-many relationship.
I have two classes university and department, suppose there is one to many relationship
I have two Entity Framework objects with a one-to-many relationship: widget(parent) and widgetNail(child) Widget
I have the two entity classes, User and MyCharacter. User has a list of

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.