Monday, July 3, 2017

Hibernate Mapping - OneToOne Relationship

Hello friend , Knowing one to one relation is very easy to understand for the interview aspirant , but to implement the knowledge into programming is the key which makes one a good programmer.

Today we will learn more about hibernate mapping -One to one

Breif- There are FOUR relationship mapping between object (tables)

  • ONE TO ONE
    • Unidirectional 
    • bidirectional
  • ONE TO MANY
    • Unidirectional 
    • bidirectional
  • MANY TO ONE
    • Unidirectional 
    • bidirectional
  • MANY TO MANY 
    • Unidirectional 
    • bidirectional

One to One mapping- In One to One mapping one object (row of a table ) is associated only with one another object(row of a table)

Ex - A user have only one System Credential.

When we say Uni directional - it means we dont have target to source mapping , we only have source to target mapping.
But in bidirectional we will have source to target to and fro mapping.

In Today's post we will see the ONE TO ONE UNIDIRECTION PROGRAM-

The Project Structure should be like below













SQL Scripts
CREATE TABLE `finances_user` (
  `USER_ID` bigint(20) NOT NULL AUTO_INCREMENT,
  `FIRST_NAME` varchar(45) NOT NULL,
  `LAST_NAME` varchar(45) NOT NULL,
  `BIRTH_DATE` date NOT NULL,
  `EMAIL_ADDRESS` varchar(100) NOT NULL,
  `LAST_UPDATED_BY` varchar(45) NOT NULL,
  `LAST_UPDATED_DATE` datetime NOT NULL,
  `CREATED_BY` varchar(45) NOT NULL,
  `CREATED_DATE` datetime NOT NULL,
  `USER_ADDRESS_LINE_1` varchar(100) DEFAULT NULL,
  `USER_ADDRESS_LINE_2` varchar(100) DEFAULT NULL,
  `CITY` varchar(100) DEFAULT NULL,
  `STATE` varchar(2) DEFAULT NULL,
  `ZIP_CODE` varchar(5) DEFAULT NULL,
  PRIMARY KEY (`USER_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;


CREATE TABLE `credential` (
  `CREDENTIAL_ID` bigint(20) NOT NULL AUTO_INCREMENT,
  `USER_ID` bigint(20) NOT NULL,
  `USERNAME` varchar(50) NOT NULL,
  `PASSWORD` varchar(100) NOT NULL,
  PRIMARY KEY (`CREDENTIAL_ID`),
  UNIQUE KEY `USER_ID_UNIQUE` (`USER_ID`),
  CONSTRAINT `FINANCES_USER_FK` FOREIGN KEY (`USER_ID`) REFERENCES `finances_user` (`USER_ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;


DOWNLOAD - SOURCE CODE (if deleted let us know)

Let us know if you dont understand anything in this post .

Thanks for reading 
-Noeik




0 comments:

Post a Comment