Nearby lessons

8 of 19

Hibernate - Composite Keys

📌 What You Will Learn
  • Understand Hibernate - Composite Keys
  • See working code examples
  • Learn from common mistakes and Q&A

Learn Hibernate - Composite Keys step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.

Composite Keys — ClientApp.java

  • @Column(name="ENAME")
  • private String ename;
  • @Column(name="ESAL")
  • private float esal;
  • @Column(name="EADDR")
  • private String eaddr;
  • public int getEno() {
  • return eno;
  • public void setEno(int eno) {
  • this.eno = eno;
  • public String getEname() {
  • return ename;
  • public void setEname(String ename) {
  • this.ename = ename;
  • public float getEsal() {
  • return esal;
  • public void setEsal(float esal) {
  • this.esal = esal;
  • public String getEaddr() {
  • return eaddr;
  • public void setEaddr(String eaddr) {
  • this.eaddr = eaddr;
Example01
JCode Cell
1 
2package com.durgasoft.hbn.test;
3 
4import org.hibernate.Session;
5import org.hibernate.SessionFactory;
6import org.hibernate.cfg.AnnotationConfiguration;
7import org.hibernate.cfg.Configuration;
8 
9import com.durgasoft.hbn.pojo.Employee;
10 
11public class ClientApp {
12 
13public static void main(String[] args) {
14SessionFactory sessionFactory = null;
15Session session = null;
16try {
17 // Configuration cfg = new AnnotationConfiguration();
18 Configuration cfg = new Configuration();
19 cfg.setProperty("hibernate.connection.driver_Class","oracle.jdbc.OracleDriver");
20 cfg.setProperty("hibernate.connection.url","jdbc:oracle:thin:@localhost:1521:xe");
21 cfg.setProperty("hibernate.connection.username", "system");
22 cfg.setProperty("hibernate.connection.password", "durga");
23 cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.OracleDialect");
24 //cfg.addResource("Employee.hbm.xml");
25 cfg.addAnnotatedClass(com.durgasoft.hbn.pojo.Employee.class);
26 sessionFactory = cfg.buildSessionFactory();
27 session = sessionFactory.openSession();
28 Employee emp = (Employee) session.get(Employee.class, 222);
29 if (emp == null) {
30 System.out.println("Employee Not Existed");
31 } else {
32 System.out.println("Employee Details");
33 System.out.println("====================");
34 System.out.println("Employee Number :" + emp.getEno());
35 System.out.println("Employee Name :" + emp.getEname());
36 System.out.println("Employee Salary :" + emp.getEsal());
37 System.out.println("Employee Address :" + emp.getEaddr());
38 }
39} catch (Exception e) {
40 e.printStackTrace();
41} finally {
42 session.close();
43 sessionFactory.close();
44 
45}
46 
47}
48}
49

Composite Keys

In Programatic approach, if we want to change configuration details like connection properties or database properties,.... then we have to perform modifications in JAVA code, if we perform modifications in JAVA code then we must recompile the java application , it is not suggestible in enterprise applications. To overcome the problem we have to use Declarative approach.

Declarative approach

In Declarative approach, we will provide all configuration details in either properties file or in XML file then we will send configuration details to Hibernate application.

There are two ways to provide configuration details to Hibernate Applications in Declarative Approach. 1) By using properties file. 2) By Using XML file.

Using properties file in Declarative Approach

In This approach, to provide all hibernate configuration details , we have to declare a properties file with the default name "hibernate.properties" under "src" folder . In this context, when we create Configuration class object , Hibernate Software will search for the properties file with the name "hibernate.properties" and get all the configuration details into Configuration class object.

In this approach we must add mapping file explicitly to Configuration File by using the following method.

public void addResource(String mapping_File-Name)

Example:

hibernate.properties — ClientApp.java

hibernate.connection.driver_Class = oracle.jdbc.OracleDriver hibernate.connection.url = jdbc:oracle:thin:@localhost:1521:xe hibernate.connection.username = system hibernate.connection.password = durga hibernate.dialect = org.hibernate.dialect.OracleDialect

Example05
JCode Cell
1 
2class ClientApp{
3public static void main(String[] args){
4Configuration cfg = new Configuration();
5cfg.addResource("Employee.hbm.xml");
6----
7}
8}
9

hibernate.properties

Note: If we change name and location of properties file from hibernate.properties to some other abc.properties then we have to give that intemation to Hibernate software, for this, we have to create FileInputStream and Properties class object then we have to set Properties object to Configuration class object by using the following method.

public void setProperties(Properties p)

Example:

abc.properties — Employee.hbm.xml

hibernate.connection.driver_Class = oracle.jdbc.OracleDriver hibernate.connection.url = jdbc:oracle:thin:@localhost:1521:xe hibernate.connection.username = system hibernate.connection.password = durga hibernate.dialect = org.hibernate.dialect.OracleDialect

Example07
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<!DOCTYPE hibernate-mapping PUBLIC
4"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
5"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
6<hibernate-mapping>
7<class name="com.durgasoft.hbn.pojo.Employee" table="emp1">
8 <id name="eno" column="ENO"/>
9 <property name="ename" column="ENAME"/>
10 <property name="esal" column="ESAL"/>
11<property name="eaddr" column="EADDR"/>
12</class>
13</hibernate-mapping>
14

abc.properties — Employee.java

Example08
JCode Cell
1 
2package com.durgasoft.hbn.pojo;
3public class Employee {
4private int eno;
5private String ename;
6private float esal;
7private String eaddr;
8 
9public int getEno() {
10 return eno;
11}
12public void setEno(int eno) {
13this.eno = eno;
14}
15public String getEname() {
16return ename;
17}
18public void setEname(String ename) {
19this.ename = ename;
20}
21public float getEsal() {
22return esal;
23}
24public void setEsal(float esal) {
25this.esal = esal;
26}
27public String getEaddr() {
28return eaddr;
29}
30public void setEaddr(String eaddr) {
31this.eaddr = eaddr;
32}
33 
34 
35}
36

abc.properties — ClientApp.java

Example09
JCode Cell
1 
2package com.durgasoft.hbn.test;
3 
4import java.io.FileInputStream;
5import java.util.Properties;
6 
7import org.hibernate.Session;
8import org.hibernate.SessionFactory;
9import org.hibernate.cfg.AnnotationConfiguration;
10import org.hibernate.cfg.Configuration;
11 
12import com.durgasoft.hbn.pojo.Employee;
13 
14public class ClientApp {
15 
16public static void main(String[] args) {
17SessionFactory sessionFactory = null;
18Session session = null;
19try {
20 // Configuration cfg = new AnnotationConfiguration();
21 Configuration cfg = new Configuration();
22 FileInputStream fis = new FileInputStream("D:\\hibernate3\\eclipse_hibernate\\app12\\src\\abc.properties");
23 Properties p = new Properties();
24 p.load(fis);
25 cfg.setProperties(p);
26 cfg.addResource("Employee.hbm.xml");
27 //cfg.addAnnotatedClass(com.durgasoft.hbn.pojo.Employee.class);
28 sessionFactory = cfg.buildSessionFactory();
29 session = sessionFactory.openSession();
30 Employee emp = (Employee) session.get(Employee.class, 333);
31 if (emp == null) {
32 System.out.println("Employee Not Existed");
33 } else {
34 System.out.println("Employee Details");
35 System.out.println("====================");
36 System.out.println("Employee Number :" + emp.getEno());
37 System.out.println("Employee Name :" + emp.getEname());
38 System.out.println("Employee Salary :" + emp.getEsal());
39 System.out.println("Employee Address :" + emp.getEaddr());
40 }
41} catch (Exception e) {
42 e.printStackTrace();
43} finally {
44 session.close();
45 sessionFactory.close();
46 
47}
48 
49}
50 
51}
52

abc.properties

 In hibernate applications, if we use "properties" file then we are able to provide only

hibernate connection propeties, dialect propeties,... , but, we are unable to provide mapping properties,... through properties file, to provide mapping properties we have to use java methods like addResource(--) or addAnnotatedClass(--) methods from Configuration class.

 In Hibernate applications, if we want to provide all configuration details in declarative manner then we have to use XML file approach.

 If we want to use xml file to provide all configuration details then we have to use configure() method to get all configuration details from xml file. Here configure() method will search for xml file with the default name hibernate.cfg.xml under src folder inorder to get configuration details. If we change the default name and location of hibernate configuration file then we have to pass that name and location to configure(--) method as parameter.

Composite Keys

In Database applications, if one column is not sufficient to manage uniqueness as primary key then it is possible to declare more than one column combnation as primary key, such type of column combination as primary key is called as "Composite Key".

sql>create table emp1(ENO number, ENAME varchar2(10), ESAL float, EADDR varchar2(10), primary key(ENO,ENAME)); sql>commit

To rerpesent Composite keys in Hibernate applications we have to use the following xml tags in mapping file.

  • <hibernate-mapping>
  • <class name="--" table="--">
  • <composite-id>
  • <key-property name="--" column="--"/>
  • </compisite-id>
  • </class>
  • </hibernate-mapping>

 Where <composite-id> tag is representing composite key configuration in mapping file.

 Where <key-property> tag is able to represent single ID or column configuration as part of composite key.

📝 Key Takeaways
  • Key ideas of Hibernate - Composite Keys explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end