Hibernate Many To One Relation Mapping Example

Hibernate Many To One Relation Mapping Example explains about how to implement a Many To One relationship by using Hibernate

many-to-one entity relationship, multiple records of an entity have single record in other associated entity

On this standalone Hibernate Many To One Mapping Example, we are using Hibernate With MySQL Database.

Note

You can also check following related Hibernate Tutorial Series

1)    Hibernate One To One Relation Mapping Example
2)    Hibernate One To Many Relation Mapping Example
3)    Hibernate Many To One Relation Mapping Example
4)    Hibernate Many To Many Relation Mapping Example

Let us see how to implement many-to-one relationship using Hibernate

Required Libraries

You need to download

  1. JDK 7
  2. Eclipse 3.7
  3. Hibernate 4.3.1
  4. Mysql 5.1.30

Following jar must be in classpath

  1. antlr-2.7.7.jar
  2. dom4j-1.6.1.jar
  3. hibernate-commons-annotations-4.0.4.Final.jar
  4. hibernate-core-4.3.1.Final.jar
  5. hibernate-entitymanager-4.3.1.Final.jar
  6. jandex-1.1.0.Final.jar
  7. dom4j-1.6.1.jar
  8. jboss-logging-3.1.3.GA.jar
  9. hibernate-jpa-2.1-api-1.0.0.Final.jar
  10. jboss-transaction-api_1.2_spec-1.0.0.Final.jar
  11. mysql-connector-java-5.1.28-bin.jar
Note

By using hibernate.hbm2ddl.auto property, hibernate automatically create below table structure, so you only need to have the following property in hibernate.cfg.xml

<property name="hibernate.hbm2ddl.auto" value="create-drop" />

Table structure

CREATE TABLE `company` (
  `ADDRESSID` bigint(20) NOT NULL AUTO_INCREMENT,
  `CITY` varchar(255) DEFAULT NULL,
  `COUNTRY` varchar(255) DEFAULT NULL,
  `STATE` varchar(255) DEFAULT NULL,
  `STREET` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`ADDRESSID`)
);

CREATE TABLE `employee` (
  `EMPLOYEEID` bigint(20) NOT NULL AUTO_INCREMENT,
  `EMPLOYEENAME` varchar(255) DEFAULT NULL,
  `COMPANY_EMPLOYEE` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`EMPLOYEEID`),
  KEY `FK_j1a2fo8yfvds4h8fr03ydga18` (`COMPANY_EMPLOYEE`),
  CONSTRAINT `FK_j1a2fo8yfvds4h8fr03ydga18` FOREIGN KEY (`COMPANY_EMPLOYEE`) REFERENCES `company` (`ADDRESSID`)
) ;
1 2 3 Next





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