Hibernate Criteria One-To-Many Many-To-One Association

Prerequisites

Development environment

  1. JDK 1.8 Downloads
  2. Eclipse Mars Download
  3. MySql Installation

Required jar files

In this example, you don’t need to create table in advance. But test schema (MySql default schema) is requried. The option hbm2ddl.auto option in hibernate.cfg.xml allows you to create table automatically:

validate: is used to validate database schema. This option doesn’t change your DB.

update: create the table if it doesn’t exist or update the existing table.

create: drop table if exists & create the table.

create-drop: drop table if exists & create the table. At the end of the session, drop the table.

Step 1 Create New Maven-based Dynamic Web Project

Step 1.1 Create New Java Project

  • Launch Eclipse IDE;
  • Go to File -> New -> Others… -> Java Project.
  • Click Next.
  • Choose any project name you want then click Finish.

Step 1.2 Convert The Project To Maven Project

  • Right click to the project;
  • select Configure->Convert to Maven Project.
  • Click Finish.
  • Eclipse will generate Project Object Model (pom.xml) file. Open the file by Eclipse text editor then replace its content by the one below:

Note: you can create normal java project and add jar files to your project build path. But it’s easier & faster using maven as maven is integrated with Eclipse IDE; so you don’t need to manually install it. 

Step 2 Create Hibernate Configuration File

  • Right click to the project or src folder; select New -> Package.
  • Enter info.java.tips.config in Name field.
  • Click Finish.
  • Right click to the package; select New -> Others… Select XML File under XML category.
  • Click Next.
  • Name the file hibernate.cfg.xml.
  • Click Finish.
  • Replace the content by the following:

Step 3 Create Hibernate Mapping Files

We need 2 mapping files in this example. To create hibernate mapping file; follow the steps bellow:

  • Under package info.java.tips.config; select New -> Others… Select XML File under XML category.
  • Click Next.
  • Enter the name.
  • Click Finish.
  • Replace the content by the following:

Step 3.1 Supplier.hbm.xml

Step 3.2 Product.hbm.xml

Step 4 Create Java Bean Class

  • Copy the codes below to clipboard.
  • Select src folder in your project (left click to the folder).
  • Press CTRL + V.
  • Eclipse IDE will automatically create package & class file with the code that’s pasted from clipboard.

Step 4.1 Supplier Class

Step 4.2 Product Class

Step 5 Create Test Class

Create new class with the content as below:

Step 6 Clean & Build

Clean & build the project to download & add maven dependencies to the project build path as well as clean old classes file (if any) & force the IDE to recompile java code.

  • Go to Project on menu bar; select Clean…
  • Select Clean all projects (default)
  • Click Ok to proceed clean & build.

Step 7 Run the application

Right click to the class; select Run As -> Java Application.

Output