Adding a map in hibernate is fairly easy. Map has key value pair and we’ll be adding a Map in the persistent class. Here’s the persistent class:
public class Student { Long id; String firstName; String lastName; Map<String,String> images = new HashMap<String, String>(); // getters and setters for the properties }
Here’s the mapping file:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.jft.prashant.Student" table="STUDENTS"> <id name="id" column="STUDENT_ID" type="long"> <generator class="increment"/> </id> <property name="firstName" column="STUDENT_FIRST_NAME" type="string" /> <property name="lastName" column="STUDENT_LAST_NAME" type="string" /> <map name="images" table="STUDENT_IMAGE"> <key column="ITEM_ID"/> <map-key column="IMAGE_NAME" type="string"/> <element type="string" column="FILE_NAME" not-null="true"/> </map> </class> </hibernate-mapping>
Primary key of the collection table will be composite of IMAGE_NAME and ITEM_ID. That’s all about adding map to the persistent class.
Recent Comments