Hibernate one to many mapping example

This is 4 of 8 parts of tutorial series

Tutorial Content:

In this example we will see how to implement one to many relationship using annotations.
Lets take example of Country and state.One Country can have n number of states.Following is relationship diagram among them.

Now to create above tables in database, you need to create two java files i.e. Country.java and State.java.


Country class will be used to create COUNTRY table in database.
Create Country.java in src->org.arpit.javapostsforlearning.

The @OneToMany annotation is used to create the one-to-many relationship between the Country and State entities. The @JoinTable annotation is used to create the COUNTRY_STATE link table and @JoinColumn annotation is used to refer the linking columns in both the tables.


State class will be used to create STATE table in database.
Create State.java in src->org.arpit.javapostsforlearning.


Create a file named “hibernate.cfg.xml” in src folder.

4.Main Class:

Project Struture:

5.Run it:

When you run it,you will get following output.

6.SQL output:

COUNTRY table in database

STATE table in database

COUNTRY_STATE table is created to link above two tables.

Source code:

Was this post helpful?

Related Posts

  • Call Function with Parameters in PowerShell
    08 January

    PowerShell – Call Function with Parameters

    Table of ContentsUsing Positional ParametersUsing Named Parameters Using Positional Parameters To call a function, use positional parameters separated by space. Please note that you need to pass parameters in sequence while calling the function. [crayon-63bae9c0cc2d6218196728/] [crayon-63bae9c0cc2dc496669718/] Microsoft’s PowerShell consists of a command-line shell and scripting language. It is based on the .NET framework for task […]

  • 08 January

    Press Any Key to Continue in PowerShell

    Table of ContentsSolutions for PowerShell Integrated Scripting Environment(ISE) and Command Line ConsoleUsing Read-Host CmdletUsing Message Box UISolutions for PowerShell Command Line ConsoleUsing ReadKey() Method with/without [void]Using RawUI.ReadKey() MethodUsing cmd /c pause CommandUsing timeout /t Command Solutions for PowerShell Integrated Scripting Environment(ISE) and Command Line Console Using Read-Host Cmdlet Use the Read-Host command to enable press […]

  • 08 January

    PowerShell – Unzip File

    Table of ContentsUsing Expand-Archive CmdletUsing System.IO.Compression.ZipFile NamespaceUsing 7Zip Module Using Expand-Archive Cmdlet Use the Expand-Achrive cmdlet to unzip a file/folder in PowerShell. [crayon-63bae9c0cc649530365820/] Please note that Expand-Archive cmdlet is available from PowerShell 5 onwards. Print envionment variable $PSVersionTable.PSVersion to check version of your PowerShell. We used the Expand-Achrive cmdlet to unzip a folder from the […]

  • PowerShell - Write log to file
    07 January

    PowerShell – Write to Log File

    Table of ContentsUsing the TranscriptUsing the Add-Content CmdletUsing the Write-Output CmdletUsing the Out-File Cmdlet Using the Transcript To write to a log file using Transcript: Use the Start-Transcript cmdlet to start writing to the log file. Perform operations normally without writing anything to the file. Use the Stop-Transcript to stop the transcript. [crayon-63bae9c0cc766768598831/] [crayon-63bae9c0cc769408654988/] A […]

  • Get size of folder in PowerShell
    07 January

    PowerShell – Get Size of Folder

    Table of ContentsUsing Get-ChildItem with Measure-ObjectGet File Size of a Certain TypeGet Directory Size, Including Sub-foldersUsing the PSFolderSize Module Using Get-ChildItem with Measure-Object Use Get-ChildItem with the Measure-Object cmdlet to get the size of files in the specified directory. [crayon-63bae9c0cc88f078466720/] [crayon-63bae9c0cc891757384454/] Please note that above sum doesn’t include subfolder sizes. If you want to include […]

  • PowerShell - read file line by line
    07 January

    PowerShell – Read File Line by Line

    Table of ContentsUsing Get-Content CmdletUse Get-Content Cmdlet to Read Partial DataUse Get-Content Cmdlet with -Like OperatorUsing switch StatementUsing the ReadLines() of [System.IO.File] ClassUsing the StreamReader Class Using Get-Content Cmdlet Use the Get-Content cmdlet to read file line by line in PowerShell. [crayon-63bae9c0cca2e713324760/] [crayon-63bae9c0cca31728047030/] We used the Get-Content cmdlet to read the specified file line by […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.