0

In the Hibernate documentation, there is such a sentence:

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.

I found this sentence to be very confusing, can somebody explain what it means? Also, on the Best Pratices page, you will find

Write fine-grained classes and map them using <component>

I'm not sure what to make of these. Is mapping using <component> when you use Hibernate API instead of JPA? How does it translate to JPA if this is Hibernate specific?

Dkyc
  • 89
  • 1
  • 16
  • Look at my answer. I tried to explain the above queries in ur question. I hope it helps you. – OO7 Nov 06 '14 at 11:46

1 Answers1

3

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.

OO7
  • 2,785
  • 1
  • 21
  • 33
  • Thanks so much for your answer. In case 1, could you also contrast that to **composite element mapping** – Dkyc Nov 09 '14 at 11:05
  • 1
    @Dkyc I have updated my answer with difference in Association & Composition. Have a look at it. – OO7 Nov 10 '14 at 13:44