Nearby lessons
8 of 19Hibernate - Composite Keys
- 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;
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
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
abc.properties — Employee.java
abc.properties — ClientApp.java
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 ideas of Hibernate - Composite Keys explained simply
- Ready-to-use code examples
- Exam-style questions at the end