Association Mappings:


Association Mappings:

One of the essential components of JPA and Hibernate is association mapping. They use characteristics in your domain model to represent the relationship between two database tables, which makes browsing your domain model's relationships, and JPQL or Criteria queries simple.

The most challenging item to accomplish right is often association mappings.We will go through some canonical situations one by one in this section, starting with the indirect maps one at a time and moving on to the two oriented maps. In all cases, we'll utilise the terms Person and Address.

Multiplicity and whether or not they map to an intervening join table will be used to classify associations.

Because nullable foreign keys aren't regarded as best practise in traditional data design, we don't utilise them in our examples. Hibernate does not require this, and the mappings will still operate if the nullability restrictions are removed.

We may have a collection of objects that are not value types but instead shared objects, and the parent object cannot determine their life duration in such circumstances. The enclosed objects will have their own identity and, as a result, their lifetime in such cases.

The types of association mapping are listed below.

  1. Many to One
  2. One to Many
  3. Many to Many
  4. One to One

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/tutorial
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
password
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>

<property name="hibernate.format_sql">true</property>
<property name="show_sql">true</property>

<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<mapping resource="association-mapping.hbm.xml" />
</session-factory>
</hibernate-configuration>