Monday, March 4, 2013

Hibernate Course: Section: 5 - Mapping Composite And Collection Types V27 - V31

Video 28: Mapping Composite Value Types
example, we have a bank Entity, this bank has address information, we want to set the address in a separate class, we don't have Address table in the database, address information are columns in the Bank table.

to do that you define the Address class

as you can see we annotated it with @Embeddable.

now in the Bank class

as you an see we use @Embedded here

there is another scenario where we use also the address in a different table, lets say in USER table, however the columns in the USER table have different names not similar to the names annotated in the Address class.

to solve this problem you can do this:


as you can see we use @AttributeOverrid to use different names.

NOTE: we use the term Composite or Embedded when we describe the Address class
NOTE: as we mentioned before Address doesn't have a lifecycle and doesn't have identity (you can query the address

Video 29: Mapping Collections of Basic Value Types
lets say the bank has list of contact names

private Collection<String> contacts = new ArrayList<String>();

In order to save that in the database, we should have a table to hold this information, we add this table to the database BANK_CONTACT(BANK_ID, NAME).

NOTE: THIS IS NOT ONE TO MANY RELATIONSHIP, THIS IS ELEMENT COLLECTION, AS YOU CAN SEE THE TABLE DOESNT HAVE AN ID

now in order to do the mapping
@ElementCollection
@CollectionTable(name = "BANK_CONTACT", joinColumns = @JoinColumn(name = "BANK_ID"))
@Column(name = "NAME")
private Collection<String> contacts = new ArrayList<String>();

as you can see we use ElementCollection and CollectionTable.

Note: the field shouldn't be of type Collection it could be List or Set
NOTE: VERY IMPORTANT TO NOTICE THAT BANK_CONTACT DOESNT HAVE IDENTITY (DOESNT HAVE AN ID) WHICH MEANS IT IS PART OF THE BANK ENTITY WHICH MEANS IT DOESNT HAVE A LIFE CYCLE AS WELL

Video 30: Mapping a Map of Basic Value Types
lets say we have
private Map<String, String> contacts = new HashMap<String, String>();

in order to map this to the database, we should have another table with ID,Key and value fields.
we will add this table BANK_CONTACT(BANK_ID,POSITION_TYPE,NAME)

then to do the mapping we write

@ElementCollection
@CollectionTable(name="BANK_CONTACT", joinColumns=@JoinColumn(name="BANK_ID"))
@MapKeyColumn(name="POSITION_TYPE")
@Column(name="NAME")
private Map<String, String> contacts = new HashMap<String, String>();

NOTE: VERY IMPORTANT TO NOTICE THAT BANK_CONTACT DOESNT HAVE IDENTITY (DOESNT HAVE AN ID) WHICH MEANS IT IS PART OF THE BANK ENTITY WHICH MEANS IT DOESNT HAVE A LIFE CYCLE AS WELL


Video 31: Mapping a Collection of  Composite Values
lest say that the bank has multiple addresses
in order to handle this we will create a separate table for addresses and we should add this:

and of course we created USER_ADDRESS table,
as we mentioned the relation is not one to many, this is element collection, there is no primary key here.
same thing regarding the lifecycle.




No comments:

Post a Comment