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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:29:35+00:00 2026-06-06T04:29:35+00:00

I am trying to learn one to many Mapping in Hibernate . The below

  • 0

I am trying to learn one to many Mapping in Hibernate .

The below are my two java classes
Shelf.java

    public class Shelf {
    private Integer id;
    private String code;
    private Set<Book> books = new HashSet<Book>();

//setter - getter methods

}


Book.java

    public class Book {
    private String name;
    private Integer id;
    private Shelf shelf;

//setter - getter methods
}

I have following queries with respect to the above code

  1. Should be treat Shelf as the Master Table and Book as the Child Table ?? (Because Shelf.java contains many Objects of Object Book ?? or there isn’t any such thumb rule to decide which is Master and Child Table )

2.

 <many-to-one name="shelf" class="Shelf" >
            <column name="SHELF_ID" not-null="true"></column>
        </many-to-one>

Inside hbm Mapping files in Book.hbm.xml file we have

why many-to-one tag here ??

  • 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-06T04:29:36+00:00Added an answer on June 6, 2026 at 4:29 am

    [1] In a 1-N relationship like that what you can configure is which should be the owner side. The owner side is responsible for managing the relationship, it’s the programmer’s responsibility to set the other side, so the object graph will be consistent. (If you forget to do this, Hibernate will save the relationship, however your object graph will be temporarily inconsistent).

    In this case, the owning side could be either Shelf or Book. Unless you have some reason to choose otherwise, it is more logical to pick Shelf as the owner, it will make your life easier when working with detached entities later on.

    [2] On the Book side you need a Many-To-One relationship, on the Shelf side, you need a One-To-Many relationship, see in the below mapping. The reason you need this both is to let Hibernate know that the two ends of the relationship should be attached to form one relationship and should NOT be handled as separate (one sided) relationships.

    Inside the .hbm file, you do:

    <class name="com.company.Shelf" table="SHELF">
            <id name="id" type="integer" column="ID">
                <generator class="increment" />
            </id>
            <property name="code" type="string" not-null="true" length="100" column="CODE" />
            <set name="books" table="BOOKS" cascade="all">
                <key column="SHELF_ID" />
                <one-to-many class="com.company.Book" />
            </set>
    </class>
    
    <class name="com.company.Book" table="BOOK">
            <id name="id" type="int" column="id">
                <generator class="increment" />
            </id>
            <property name="name" type="java.lang.String" column="name" length="255" />
            <many-to-one name="shelf" class="com.company.shelf" column="SHELF_ID" />
    </class>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to learn more about regular expressions I have one below that
Ive been playing around with delegates trying to learn and I ran into one
I'm trying to learn how to use WinForms databinding, but one thing that's confusing
I am trying to learn JPA with Hibernate and binding it to a GUI
I have been trying to learn about classes in PHP and part of my
I'm trying to learn to work with Hibernate but probably i don't understand @ManyToOne
In the Squeak Smalltalk environment, I am trying to learn Morphic. There are many,
Hello I'm an amateur trying to learn/improve my understanding of Java by writing a
I am trying to learn iPhone development as I've read many others on here
Comming from a C# background I'm now trying to learn Delphi. There are many

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.