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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:32:32+00:00 2026-06-18T12:32:32+00:00

I have two entity classes that are in @OneToOne relation. The example code are

  • 0

I have two entity classes that are in @OneToOne relation. The example code are as follow:

public class A {
@Id
private int id;
private String name;
@JoinColumn(name = "B_ID", referencedColumnName = "id")
@OneToOne(cascade=CascadeType.ALL)
private B b;

//setters and getters

}

public class B {
@Id
private int id;
private String name;
@OneToOne(mappedBy="b")
    private A a;
//setter and getters

}

my question here is “Can I use setA(A a) method in class B. I mean like this . .

em.getTransaction().begin();
A aa = new A();
aa.setId(1);
aa.setName("JJ");
em.persist(aa);

B bb = new B();
bb.setId(1);
bb.setName("CC");
bb.setA(aa);
em.persist(bb);
em.getTransaction().commit();

When I tried like this, the foreign_key field in table A (B_ID) was saved as null.
Please help me.

  • 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-06-18T12:32:33+00:00Added an answer on June 18, 2026 at 12:32 pm

    Here , you have specified mappedBy in class B above private A a;. In a bidirectional relationship , mappedBy means that I am not the owner. So It means that A is the owner of the relationship.

    In table of A , you will have a foreignkey for table of B. As A is the owner, A is suppose to cascade operations to B. Ideally you should try a.setB() and then persist a.

    Try below:

    em.getTransaction().begin();
    //first create B.
    B bb = new B();
    bb.setId(1);
    bb.setName("CC");
    em.persist(bb);
    
    //create A with B set in it.
    A aa = new A();
    aa.setId(1);
    aa.setName("JJ");
    aa.setB(bb);
    em.persist(aa);
    em.getTransaction().commit();
    

    Or

    em.getTransaction().begin();
    //first create B.
    B bb = new B();
    bb.setId(1);
    bb.setName("CC");
    // no need to persist bb.
    
    //create A with B set in it.
    A aa = new A();
    aa.setId(1);
    aa.setName("JJ");
    aa.setB(bb);
    em.persist(aa); // because of cascade all , when you persist A ,
    // B will also be persisted.
    em.getTransaction().commit();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two entity classes: public class Invoice { public int ID { get;
I have two classes, user and role, defined as: public class User : Entity
I have the following two entities: @Entity class Relation{ @ManyToOne private User user; //some
I have two entity classes: Person and Position . public class Person { public
I have 3 different classes, a generic entity class, and then two classes that
I have two entity classes annotated in the following way @Entity class A {
I have these two classes that implement single table inheritance strategy: @Entity @Table(name =
I have two classes, class entity and class tile. Class entity has some member
I need to derive two of my Entity classes from a base class that
I have a two entity classes called User and Group, both reserved words that

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.