Hibernate many to many mapping example

This is 5 of 8 parts of tutorial series

Tutorial Content:

In this example we will see how to implement many to many relationship using annotations.
Lets take example of Country and Language.One Country can have n number of languages and one language can be spoken by n number of countries.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 Language.java.

1.Country.java

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

The @MamyToMany annotation is used to create the many-to-many relationship between the Country and Language entities. The @JoinTable annotation is used to create the COUNTRY_LANGUAGE link table and @JoinColumn annotation is used to refer the linking columns in both the tables.

2.Langauge.java

Language class will be used to create LANGUAGE table in database.
Create Langauge.java in src->org.arpit.javapostsforlearning.

3.Hiberante.cfg.xml:

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.

5.SQL output:

COUNTRY table in database

LANGUAGE table in database

COUNTRY_LANGUAGE table is created to link above two tables.

Source code:

Was this post helpful?

Related Posts

  • Multiline comment in PowerShell
    05 January

    PowerShell – Multiline Comment

    Table of ContentsUsing <# #> TagsUsing Shortcut Keys Using <# #> Tags Use the <# tag at the beginning of comments and the #> tag at the end of comments to create multiline comments in PowerShell scripts. All lines inside the comment block are part of the comments. [crayon-63b843e1c8ba1337555544/] In programming, we use a comment […]

  • Call PowerShell Script from another Script
    05 January

    Call PowerShell Script from Another Script

    Table of ContentsUsing . OperatorUsing & OperatorUsing Invoke-Expression CmdletUsing Start-Process Cmdlet Using . Operator To call a PowerShell script from another: Create two scripts, Script1.ps1 and Script2.ps1. Use the . operator to call Script2.ps1 from Script1.ps1. [crayon-63b843e1c9213019335277/] [crayon-63b843e1c9219556787263/] [crayon-63b843e1c921a915312928/] [crayon-63b843e1c921b738951829/] Both scripts (Script1.ps1 and Script2.ps1) were located in the E:\Test directory. When we ran Script1.ps1, […]

  • awk print $2
    05 January

    awk print $2

    In this tutorial, we will see about what is meant by awk print $2. awk is interpreted programming language. It is very powerful programming language and used for text processing. Awk stands for the names of its authors “Aho, Weinberger, and Kernighan”. awk print $2 As said before, awk can be used for text processing. […]

  • Check if Service is running in PowerShell
    05 January

    Check if Service is Running in PowerShell

    Table of ContentsUsing Get-Service CmdletUse Get-Service Cmdlet with if-else BlockUsing Get-SPServiceApplication Cmdlet Using Get-Service Cmdlet Use Get-Service with the Where-Object cmdlet to check if a particular service is running on our local machine. [crayon-63b843e1c995d059397110/] [crayon-63b843e1c9962758894948/] If you need to put if condition, then head over to this section . We used the Get-Service cmdlet to […]

  • Run reg file in PowerShell
    05 January

    Run .reg File Using PowerShell

    Table of ContentsUsing Invoke-Item CmdletUsing Start-Process CmdletUsing iex OperatorUsing . OperatorUsing reg import Command Using Invoke-Item Cmdlet Use the Invoke-Item cmdlet to run the .reg file using PowerShell. [crayon-63b843e1c9ecf400495574/] For the above command, Invoke-Item performed its default action, which means it allowed us to execute a .reg file. We can also use it to run […]

  • PowerShell restart Computer
    05 January

    PowerShell – Restart Computer

    Table of ContentsUsing Restart-Computer cmdletUse Restart-Computer cmdlet for Local ComputersUse Restart-Computer cmdlet for Remote ComputerUsing Shutdown CommandUse Shutdown Command for Local ComputersUse Shutdown Command for Remote ComputersExamplesExample 1: Restart local computerExample 2: Restart multiple computersExample 3: Restart remote computer with wait, timeout, delay and for parameters. Using Restart-Computer cmdlet Use the Restart-Computer cmdlet to restart […]

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.