one-to-many in Hibernate
I am re-factoring one of the internal applications, so that the development can be module based. So my schema has unique module names which has n number of menu items; one-to-many! Here is my hibernate mapping.
<class name="com.eginnovations.common.entity.Module" table="MODULE_INFO">
<id column="ID" name="id" type="java.lang.Long">
<generator />
</id>
<property name="moduleName" column="MODULE_NAME" type="java.lang.String"/>
<property name="enabled" column="ENABLED" type="java.lang.Boolean"/>
<set name="menuItemList" cascade="all" inverse="true" lazy="false">
<key column="MODULE_INFO_ID"></key>
<one-to-many/>
</set>
</class>
<class name="com.eginnovations.common.entity.MenuItem" table="COMMON_MENU"> <id column="ID" name="id" type="java.lang.Long"> <generator /> </id> <property name="menuTitle" column="MENU_TITLE" type="java.lang.String"/> <property name="parentId" column="PARENT_MENU_ID" type="java.lang.Long"/> <property name="locationHref" column="LOCATION_HREF" type="java.lang.String"/> <many-to-one name="module" column="MODULE_INFO_ID" cascade="all"></many-to-one> </class>
But when I run the unit test to get the list of modules, I ended up with the exception
org.springframework.orm.hibernate3.HibernateSystemException: IllegalArgumentException occurred while calling setter of com.eginnovations.common.entity.Module.menuItemList; nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of com.eginnovations.common.entity.Module.menuItemList
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:659) at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
…
…
The problem is with data type, the menuItemList is mapped to a list instead of a set!!