Case 1 : The default semantics of a one-to-many association in Hibernate are much less close to the usual semantics of a parent/child relationship than those of a composite element mapping. means :-
Suppose, you have two classes named, A
& B
. A has primary key aId
, which is foreign key in B. Then in this case, when you map them using Hibernate, A can be called as parent of B &/or B is a child of A. Because, B is dependent on A for it's primary key. If you try to add entry in B
class before A
then it gives error something like ConstraintViolationException
.
Case 2 : Write fine-grained classes and map them using <component>
:-
The word component in Hibernate refers to a contained object in another object & not as a separate entity in database. Word component describes composition in Hibernate. More precisely, it means create an entity in database table with two separate objects in Java.
For example : You have a Person
object containing various fields namely, id, firstName, lastName, address, age, etc.
Then you can create a single table in database as
drop table if exists Person
create table Person(
id int,
firstName varchar(50),
lastName varchar(50),
street varchar(50),
state varchar(50),
zipcode int(8),
age int(3)
);
Now, you need to create two pojo classes namely, Person
& Address
.
public class Person {
private int id;
private String firstName;
private String lastName;
private int age;
private Address address;
//getter/setter
}
public class Address {
private String street;
private String state;
private int zipCode;
//getter/setter
}
To introduce composition in this scenario you can use <component>
tag in case of XML configuration or @Embedded & @Embeddable annotations.
If you create XML file for the Person class then is likely as follows :
<hibernate-mapping>
<class name="Person" table="Person">
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<component name="address" class="Address">
<property name="street" column="street" type="String"/>
<property name="state" column="state" type="String"/>
<property name="zipcode" column="zipcode" type="int"/>
</component>
<property name="firstName" column="firstName" type="String"/>
<property name="lastName" column="lastName" type="String"/>
<property name="age" column="age" type="int"/>
</class>
</hibernate-mapping>
On the other hand, using @Embedded & @Embeddable annotations in POJO classes as :-
public class Person {
@Id
@GeneratedValue
private int id;
private String firstName;
private String lastName;
private int age;
@Embedded
private Address address;
//getter/setter
}
@Embeddable
public class Address {
private String street;
private String state;
private int zipCode;
//getter/setter
}
The @Embedded annotation is used to specify; the Address entity should be stored in the Person table as a component.
@Embeddable annotation is used to specify; the Address class will be used as a component.
The Address class cannot have a primary key of its own, it uses the enclosing class primary key.
Persisting Person In DB :
While performing insertion operation on database. You need to first create an object of Address class & then Person class.
Address address = new Address("ABC Road", "MH", "440022");
Person person = new Person("A", "Z", 10, address);
session.save(person);
To check data in Person table you can fire below query :-
mysql> select id, firstName, lastName, street, state from Person;
---------------------------------------------------
| id | firstName | lastName | street | state |
---------------------------------------------------
| 1 | A | Z | ABC Road | MH |
| 2 | B | Y | XYZ Road | MP |
---------------------------------------------------
2 rows in set (0.00 sec)
mysql>
EDIT : CASE 1 vs CASE 2 (Association vs Composition) :
Case 1 represents Association whereas, Case 2 represents Composition. Association is meant to represent relationship between two distinct entities, such as one-to-one, one-to-many, many-to-one, many-to-many. On the other hand, composition is commonly known as HAS A relationship between two entities, where one entity is a part of another entity, if the contained entity cannot exist without the existence of container entity.
For More Explaination : Association, Aggregation and Composition
I hope this helps you.