Table of contents
1.
Introduction
2.
Component Mapping
3.
Dependent Objects
3.1.
Example
4.
Dynamic Components
4.1.
Advantages
5.
Frequently Asked Questions
5.1.
What is hibernate caching?
5.2.
What does session.lock() method in hibernate?
5.3.
What is JPQL?
5.4.
What is hibernate?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

Component Mapping

Author Ayush Mishra
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hibernate ORM is a framework or tool for object-relational mapping in Java. It is licensed under the GNU Lesser General Public License 2.1 and is free software.

One of the most important aspects of hibernate is its mappings. They create the connection between two database tables as model attributes, making it simple for you to browse the relationships in your model and criteria queries.

In this blog, we will discuss component mapping in hibernate to map the dependent object as a component. Let's get going!

Component Mapping

A component, as opposed to an entity reference, is a contained object saved as a value type.

In hibernate, the components are reused in various settings and applications. An assignment for a class that contains a reference to another class as a member variable is known as a component mapping. It is known as a component because it is utilized in composition cases.

Dependent Objects

The term "component" refers to an idea of object-oriented composition rather than components that are part of the architecture. A component is an object confined and stored as a value type. An entity reference is not a component.

Example

public class Student
 {
	private java.util.Date Registration;
	private Name name;
	private String key;
	public String getKey() // getter method to get the key
 		{
			return key;
		}
	private void setKey(String key) // setter method to set the key
 		{
			this.key=key;
		}
	public java.util.Date getRegistration()
		{
			return registration;
		}
	public void setRegistration(java.util.Date registration)
		{
			this.registration = registration;
		}
	public Name getName() // getter method to return the name
		{
			return name;
		}
	public void setName(Name name) // setter method to set the name
		{
			this.name = name;
		}
 }
public class Name
 {
	char initial;
	String first;
	String last;
	public String getFirst()  // to return the first name
 		 {
			return first;
		 }
	void setFirst(String first) // to set the first name
		 {
			this.first = first;
		 }
	public String getLast()  //  to return the last name
		 {
			return last;
		 }
	void setLast(String last) // to set the last name
 		 {
			this.last = last;
		 }
	public char getInitial()
		 {
			return initial;
		 }
	void setInitial(char initial)
		 {
			this.initial = initial;
		 }
  }
You can also try this code with Online Java Compiler
Run Code

 

Name can be stored as a part of the Student in the example above. Getter and setter methods are provided by the component Name for their persistent properties. The below given an example contains the component's hibernate mapping.

<class table="Student">
	<id column="pid" type="string">
		</generator>
	</id>
	<property>
		<component>  <!-- class attribute  is optional -->
	</property>
	</component>
</class>

 

A "parent" subelement that maps the property of the component class as a reference back to the contained entity is allowed by the "component" element.

<class name="eg.Student" table="student">
	<id column="pid" type="string">
		</generator>
	</id>
	<property>
		<component unique="true">
		<parent/>  <!-- reference back to the person →
	</property>
	</component>
</class>

Dynamic Components

The component mapping also allows for dynamic component mapping.

Eg:

<dynamic-component name="userAttributes">
	<property column="FOO" type="string"/>
	<property column="BAR" type="integer"/>
	<many-to-one column="BAZ_ID"/>
</dynamic-component>

 

Advantages

Advantages of the dynamic component mapping are:

1) The option to modify the mapping file so that, at deployment time, the true attributes of the bean are displayed.

2) Using a DOM parser makes it easier to manipulate the mapping document in runtime.

Frequently Asked Questions

What is hibernate caching?

It is a technique for enhancing application performance by grouping objects in the cache to speed up query processing. When fetching the same data several times, Hibernate caching is especially helpful. We can access the data from the cache rather than contacting the database. The application's throughput time is shortened as a result.

What does session.lock() method in hibernate?

A disconnected object can be reattached to the session using the session.lock() method. Reattachment may result in a loss of data synchronization since the session.lock() method does not check for data synchronization between the database and the object in the persistence context.

What is JPQL?

The JPA specification is known as the Java Persistence Query Language (JPQL). When constructing the query, it is used.

What is hibernate?

A lightweight, open-source ORM tool called Hibernate is used to store, alter, and retrieve data from databases.

Conclusion

Thank you for completing the blog! The entirety of component mapping in Hibernate was addressed in this blog. In component mapping in Hibernate, we've talked about dependent objects and dynamic components.

We go into great detail about Component Mapping in this blog. If you want to explore more blogs on this topic, please follow these blogs especially curated for readers like you - Java FrameworkJDBC ConnectionGuide to ORM, Introduction to Spring Boot.

Please refer to our guided pathways on Code studio to learn more about DSACompetitive ProgrammingJavaScriptSystem Design, etc. Enroll in our courses, and use the accessible sample exams and questions as a guide. For placement preparations, look at the interview experiences and interview package.

Please do upvote our blogs if you find them helpful and informative!

Happy Learning!

Live masterclass