Javatpoint Logo

91-9990449935

 0120-4256464

Mapping List in Collection Mapping (using xml file)

If our persistent class has List object, we can map the List easily either by <list> element of class in mapping file or by annotation.

Here, we are using the scenario of Forum where one question has multiple answers.

Mapping List in hibernate

Let's see how we can implement the list in the mapping file:

List and Map are index based collection, so an extra column will be created in the table for indexing.


Example of mapping list in collection mapping

In this example, we are going to see full example of collection mapping by list. This is the example of List that stores string value not entity reference that is why are going to use element instead of one-to-many within the list element.

1) create the Persistent class

This persistent class defines properties of the class including List.


2) create the Mapping file for the persistent class

Here, we have created the question.hbm.xml file for defining the list.


3) create the configuration file

This file contains information about the database and mapping file.


4) Create the class to store the data

In this class we are storing the data of the question class.


How to fetch the data of List

Here, we have used HQL to fetch all the records of Question class including answers. In such case, it fetches the data from two tables that are functional dependent.

Next TopicList One To Many