InterviewSolution
| 1. |
How many types of collection are there in Hibernate? Explain Collection Mapping? |
|
Answer» The HIBERNATE collections BEHAVE like HashMap, HashSet, TreeMap, TreeSet or ArrayList. Collection instances are like value types and are automatically persisted when referenced by a persistent object and deleted when unreferenced. There are five types of collections in Hibernate. They are:
To map these collections, the type of collection must be declared from one of the following:
The Hibernate mapping element used for mapping a collection depends UPON the type of INTERFACE. The basic syntax for mapping is: <hibernate-mapping> <class name="<event.Class>" table="<TableName>"> <id name="id" column="<ColumnName>"> <generator class="native"/> </id> <property name="<property>" type="<type>" column="<ColumnName>"/> <property name="<property>"/> <set name = “<name of set>”> <key column=“<column name>” not-null= “true”/> <one-to-many class=“<associated class>”/> </set> </class> </hibernate-mapping>The <set> can be replaced with map, list etc. There are different associations for mapping the class with the CORRESPONDING table. The types of associations are as below:
When using a map for mapping, a map-key has to be provided along with the key column. <map name="developmentLanguages" table="tdevelopmentlanguage" cascade="all"> <key column="developer_id" not-null="true"></key> <map-key type="string" column="shortname"></map-key> <element column="name" type="string"></element> </map> |
|