Hibernate one to one mapping example

This is 3 of 8 parts of tutorial series

Tutorial Content:

In this example, we will see how to implement one to one relationship using annotations.

Lets take example of Country and Capital.One Country has one capital.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 Capital.java.

1.Country.java

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

@OneToOne annotation is used to create one to one relationship between Country and Capital entities.
@joinColumn
is used to specify a mapped column for joining an entity association.

2.Capital.java

Capital class will be used to create CAPITAL table in database.
Create Capital.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.SQL output:

COUNTRY table in database

CAPITAL table in database

Source code:

Related Posts

  • Multiple classes in one file in Java
    18 October

    Multiple classes in one file in Java

    Table of ContentsIntroductionMethods to Implement Multiple Classes In One Java Program1) Nested classes2) Multiple non-static nested classes In this post, we will see how to have multiple classes in one file in java. Introduction You need to have any number of classes in a single Java file, but there is a restriction that you can […]

  • How to initialize empty array in PHP
    18 October

    Declare empty array in php

    Table of ContentsUsing square brackets to declare empty array in PHPUsing array() to declare empty array in PHP An array is a data structure where you can store the same or different types of data in a single variable. In PHP, you can declare empty array using square brackets ([]) or using array() function. Using […]

  • Find duplicate elements in the Stream
    17 October

    Java 8 – Find duplicate elements in Stream

    Table of ContentsIntroductionUsing distinct()Using Collections.frequency()Using Collectors.toSet()Using Collectors.toMap()Using Collectors.groupingBy()Conclusion Introduction When working with a collection of elements in Java, it is very common to have duplicate elements, and Java provides different APIs that we can use to solve the problem. Java 8 Stream provides the functionality to perform aggregate operations on a collection, and one of […]

  • 17 October

    Java AES 256 Encryption Decryption Example

    Table of ContentsIntroductionGenerate a shared keyEncrypt a random textDecrypt the encrypted textConclusion Introduction Advanced encryption standard (AES) is the most secure encryption standard compared to RSA, which is vulnerable to brute force attacks. This is the main reason that AES was established by the National Institute of Standard and Technology (NIST) in 2001 and is […]

  • 16 October

    Java AES Encryption Decryption Example

    Table of ContentsIntroductionGenerate a shared keyEncrypt a random textDecrypt the encrypted textConclusion Introduction AES stands for advanced encryption standard and is the most commonly used symmetric algorithm to encrypt sensitive data and can be used in both software and hardware. The AES algorithm is symmetric, meaning that it uses only one key for encryption and […]

  • How to comment out multiple lines in python
    16 October

    How to comment out multiple lines in python

    Table of ContentsMultiple Line Comments in PythonUsing the # characterUsing triple quotesUsing IDEs and Text Editors In this article, we will see about how to comment out multiple lines in python. The concept of comments is present in most programming languages. We use comments for documentation purposes. The compiler ignores them, but the user can […]

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.