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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:57:02+00:00 2026-05-27T01:57:02+00:00

I have 2 test classes: Class A{ B b; // some other properties }

  • 0

I have 2 test classes:

Class A{
   B b;
   // some other properties
}

Class B{
   // some properties
}

every instance of A has only one instance of class B, but an instance of class B can be assigned to more than one instance of class A

like :

B b = new B();
A a1 = new A();

a1.setB(b);

A a2 = new A();
a2.setB(b);

what type of association is this?
At first I was thinking about a one-to-one unidirectional but maybe is a many-to-one? but I don’t have any collection of A objects on B.

Can someone explain me what is the correct way to implements this association (using annotation)?

  • 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-27T01:57:03+00:00Added an answer on May 27, 2026 at 1:57 am

    every instance of A has only one instance of class B, but an instance
    of class B can be assigned to more than one instance of class A

    So , A to B is the many-to-one relationship . B to A is the one-to-many relationship.

    The following shows the bi-directional mapping between A and B using annotation:

    @Entity
    @Table(name="tableA")
    Class A{
    
       @ManyToOne
       @JoinColumn(name = "B_ID")
       B b;
    
       // some other properties
    
    }
    
    @Entity
    @Table(name="tableB")
    Class B{
    
       @OneToMany(mappedBy = "b")
       List Set<A> listOfA= new ArrayList<A>();  
    
      // some other properties
    }
    

    Important points:

    • @Entity marks the java class as an hibernate entity. It is mapped to the name of the table specified in the @Table

    • If no @Table is specified ,by default , it is mapped to the table with the name that is equal to the unqualified class name of the entity.

    • @ManyToOne defines Class A to Class B ‘s relationship is many-to-one

    • In the relational database , one-to-many relationship is expressed by using the following foreign key constraint :

      “Many side table” has a FK column which only accepts the PK of the “one side table”.

      This name of this FK column can be defined explictly by the name attribute of @JoinColumn. If @JoinColumn is not specified , then default value(s) with be used for this FK column , which concatenates with the name of “one side table”, _ (underscore), and the name of the PK in the “one side table”.

    • @OneToMany defines Class B to Class A ‘s relationship is one-to-many.

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

Sidebar

Related Questions

I am new to testing.I have to test some C# classes.Kindly let me know
If I have some php classes inside a namespace com\test and want to import
I'm trying to test some DAO Classes that inherit from a Generic one, and
I have a container class for adding some properties to standard data types like
I've tryed mylyn but i cant find that feature, if anyone have test mantis
I have a class which has a internal method and i want to mock
I have had some difficulties in using extended classes in rails, in particular extending
I have a structure of simple container classes like so (in pseudo ruby): class
I have a base class that represents a database test in TestNG, and I
I have two classes (A and B) which depend on each other in following

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.