In this post you learn about Hibernate composite Key example using XML mapping. composite primary key by combining more than one key.
1. Overview of Hibernate composite Key Example
Why Composite primary key required in your application for any database? when you have to combine multiple keys to be a prat of primary key. and would like to form a primary key. When you want a primary key by combining more than one keys. And you know, the primary key is unique and not nullable in the database.
You can visit our hello world Hibernate program to download required jar files. You can find download source code at the end of hello worlds article, in which you can put this code step by step and run this example of composite key.
2. Overview of composite key
First of all, you should understand, why a composite key is required?
By concept, the primary key is a unique and not null column, if you want to a primary key by combining multiple keys, what will you do? You can combine multiple keys and form a primary key. At the end of the post, you learn how to craete a composite primary key in Hibernate by using XML mapping.
3. POJO/domain model class
File: com/mysoftkey/composite/Book.java
package com.mysoftkey.composite; import java.io.Serializable; public class Book implements Serializable { private int bookId; private String bookName; private String author; private String category; private Double price; // generate setter/getter property, removed this for simplicity }
4. Hibernate mapping file for Hibernate Composite Key
This is a mapping XML file between Java and Hibernate. here, primary key by combining of bookId, bookName, and author i.e compositePrimaryKey(bookId, bookName, author)
File: book.hbm.xm
5. Hibernate configuration xml
com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/hibernate_example root root 1 org.hibernate.dialect.MySQLDialect true create
6. Run Composite key example in Hibernate
This is main class to run the composite key example in Hibernate.
File: com/mysoftkey/composite/HibernateCompositeKeyExample.java
package com.mysoftkey.composite; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; import com.mysoftkey.util.HibernateUtil; /** * this the entry point of the application to test composite key in hiberante. * * @author ranjeet jha * */ public class HibernateCompositeKeyExample { /** * This is main method to run the application. * * @param args */ public static void main(String[] args) { // Open Hibernate session from sessionFactory. Session session = HibernateUtil.getSessionFactory().openSession(); Transaction transaction = null; try { // begin Trasaction transaction = session.beginTransaction(); // create POJO and populate info, for composite primary key example Book book = new Book(); book.setBookId(1); book.setBookName("Hibernate Professional"); book.setAuthor("wrox"); book.setCategory("advance"); book.setPrice(new Double("1234.78")); // save book object into database session.save(book); // commit transaction transaction.commit(); } catch (HibernateException e) { // roll back transaction transaction.rollback(); e.printStackTrace(); } finally { session.close(); } } }
7. Output of Hibernate composite key example
Eclipse Console ouptput:
14 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.3.0.GA 31 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found 38 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist 45 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling 87 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml 209 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : com/mysoftkey/composite/Book.hbm.xml 261 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: com.mysoftkey.composite.Book -> book_compsite_key 897 [main] INFO org.hibernate.tool.hbm2ddl.SchemaExport - schema export complete Hibernate: insert into book_compsite_key (category, price, book_id, book_name, author) values (?, ?, ?, ?, ?)
mysql console output:
mysql> select * from book_compsite_key; +---------+------------------------+--------+----------+---------+ | book_id | book_name | author | category | price | +---------+------------------------+--------+----------+---------+ | 1 | Hibernate Professional | wrox | advance | 1234.78 | +---------+------------------------+--------+----------+---------+ 1 row in set (0.00 sec) mysql>
8. Reference
- Reference:
Hibernate site
Thanks for visiting this post for learning composite primary key in hibernate using XML. You can also visit Java Hibernate ORM Tutorial Listing page for more articles on Hibernate ORM framework.
Happy Learning 🙂 for Hibernate composite primary key using XML.
Your comments are welcome to improve this post (how to use the composite key in Hibernate).