Explanation on Hibernate – inverse=”false” lazy=”true” fetch=”select” – (Many To Many Relationship )

Explanation on Hibernateinverse=”falselazy=”truefetch=”select” – (Many To Many Relationship )

Hibernate Mapping XML (hbm)
Following are the hibernate mapping files for each enitity Role and Transactions.

1) Role.hbm.xml

<hibernate-mapping>
<class name=”com.fndong.customer.Role” table=”role” catalog=”hibernate” optimistic-lock=”version”>
<id name=”roleId” type=”java.lang.Integer“>
<column name=”role_id” />
<generator class=”identity” />
</id>
<property name=”roleName” type=”string“>
<column name=”role_name” length=”30” not-null=”true” />
</property>
<set name=”transactionses” table=”trans_role” inverse=”false” lazy=”true” fetch=”select” cascade=”all“>
<key>
<column name=”role_id” not-null=”true” />
</key>
<many-to-many entity-name=”com.fndong.customer.Transactions”>
<column name=”transaction_id” not-null=”true” />
</many-to-many>
</set>
</class>
</hibernate-mapping>

2) Transactions.hbm.xml

<hibernate-mapping>
<class name=”com.fndong.customer.Transactions” table=”transactions” catalog=”hibernate” optimistic-lock=”version“>
<id name=”transactionId” type=”java.lang.Integer“>
<column name=”transaction_id” />
<generator class=”identity” />
</id>
<property name=”transactionName” type=”string“>
<column name=”transaction_name” length=”30” not-null=”true” />
</property>
<set name=”roles” table=”trans_role” inverse=”true” lazy=”true” fetch=”select“>
<key>
<column name=”transaction_id” not-null=”true” />
</key>
<many-to-many entity-name=”com.fndong.customer.Role“>
<column name=”role_id” not-null=”true” />
</many-to-many>
</set>
</class>
</hibernate-mapping>

9.3) Explanations on “<set name=”transactionses” table=”trans_role” inverse=”false” lazy=”true” fetch=”select” cascade=”all“>

  • inverse=”false” =  Meaning : Inverse Attribute present in Many-to-Many and One To-Many relationship, and denotes which side is responsible  for the relationship. 
    • inverse = “true”– It defines which side is responsible to take care the relationship.
    • for value “false” – which means both sides are the owner of the relationship. In Hibernate, it will enable two sides to update the foreign key in composite table – trans_roles.
  • lazy=”true attribute , Meaning : “true” value , is enable lazy loading of the parent and child collections.
    • what is lazy loading ?
      • Lazy fetching decides whether to load child objects while loading the Parent Object. You need to do this setting respective hibernate mapping file of the parent class.  Lazy = true (means not to load child)
      • lazy=false, and hibernate will load the child when parent is loaded from the database.
      • More info on lazy loading… more info:
  • fetch=”select” – Select fetching: a second SELECT is used to retrieve the associated entity or collection. Unless you explicitly disable lazy fetching by specifying lazy="false", this second select will only be executed when you access the association.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s