Example & Tutorial understanding programming in easy ways.

What are managed associations and hibernate associations?

Associations are used to represent relationships between classes.  Managed association means that, if a change is made to one end of the association, it will be reflected at the other end. So when changes are made to the attributes participating in this association, they will be reflected at the other end automatically. The developer doesn’t have to mange the associations manually. 

Hibernate mainly supports two types of associations:
  1. One-to-Many
  2. Many-to-One


Managed Association: Basically association are used to represent relationship between class. Managed Association means that, if a change is made to one end to the association, it will be relected at the other end. So when changes are made to the attributes participating in this association, they will be reflected at the end automatically. The developer does not have manage association manually. 

Hibernate mainly supports two type of association those are:
1. One to Many
2. Many to One

Hibernate Association: If any table i db could be connected to other tables in same or other DB and these tables could be associated with each other by some key. Those kind of scenario can be handled with association.

Example: There are two table in DB, Student  & Subject. A student can study one subject or more then one subject. In that case every student will have only one entry in Student table but Subject table could contain more than one record for corresponding record in Student table.

Association mapping are used to map a java object to DB table.

Entities envoled in mappings:

Class(Persistence) (PoJo)
DB table
Mapping file(.hbm)

Rules for association with POJO class:

pojo class need to have default constructor as it is required by Hibernate.
Lazy loading does not work with final class.its preferable to have Non Final classes.

Types of Association:

many to one with xml
many to one annotation
one to one xml
one to one annotation
one to many xml
one to many annotation

Undirectional Association with join table;

One to Many XML 
One to Many Annotation
Many to One XML
Many to One Annotation
One to One XML 
One to One Annotation
Many to Many XML
Many to Many Annotation

Bidirectional association:

one to many
one to one xml
one to one annotation

Bidirectional association with join table;

one to many xml
many to many xml

Read More →