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 Framework, JDBC Connection, Guide to ORM, Introduction to Spring Boot.
Please refer to our guided pathways on Code studio to learn more about DSA, Competitive Programming, JavaScript, System 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!