tags:

views:

387

answers:

0

Hello All,

I am developing a web application using Struts 2 (struts 2.0.14) and Hibernate (3.2.6). I am facing a problem while directly populating a Hibernate Entity object using the form data. Let me give some background information regarding my source.


There is a one-to-many mapping between Table1 and Table2, which is implemented by introducing a primary key of table 1 as a foreign key in Table2. The primary key for table 2 thus is a composite key containing two columns (f1 - unique key from Table 2) and (f2 - primary key from Table 1).

The corresponding Hibernate entities for this schema is as follows.

class Model1 (Corresponds to Table 1) : defines a property 'components' of type Set, containing Model2 objects class Model2 (Corresponds to Table 2) : contains a property 'id' of type Model3 which uniquely identifies the Model2 object. class Model3 (Corresponds to composite primary key for class Model2) : contains two primitive properties f1 and f2 both of type Integer.

Here after populating the data, I persist the object of type Model1 first and retrieve its primary key (which is autogenerated) which is then used to persist a set of Model2 objects.

The corresponding HTML form consists of a set of comobox controls each accepting a value (lets say for field f3) for one of the elements (of type Model2) in Model1 object. The Model1 object is exposed through the corresponding Action using the ModelDriven pattern. Since the Model2 objects are stored in Model1 using a Set datastruture, I am using the Struts collection indexing mechanism by specifying the Element and Key type values through Model1-conversion.properties.

Using a Prepare interceptor, I ensure that the Model1 object is prepopulated with the Model2 objects containing partial Model3 objects (only containing f1 field information which is static). This should help to idenify Model2 objects during the population of Model2 entity objects from the Form data.

I faced couple of problems in this regard,

At first I specified the key property as id.f1 (Here id => Model2 and f1 => Model3), with the expectation that Struts 2 framework would use the nested property (id.f1) for comparison while iterating over the collection. But sadly this did not happen. I get following exception,

SEVERE: ParametersInterceptor - [setParameters]: Unexpected Exception caught setting 'components(2).f3' on 'class com.sample.web.ManageReview: Error setting expression 'components(2).f3' with value '[Ljava.lang.String;@150ed68'

Here please note that in 'componets(2).f3' the value 2 corresponds to the id.f1 value for one of the Model2 objects. The Model1-conversion.properties file consists of following information,

KeyProperty_components=id.f1
Element_reviewAnswers=Model2
CreateIfNull_reviewComponents=false

Then I defined a Model2-conversion.properties file and a custom converter class. The converter class accepts a String[] value and returns an object of type Model3 by initializing the field 'f1' with the first String value. The Model2-conversion.properties file have following mapping

id=class_name_of_custom_converter

The Model1-conversion.properties file consists of following information,

KeyProperty_components=id
Element_reviewAnswers=Model2
CreateIfNull_reviewComponents=false

Now I expected that Struts 2 framework would invoke my converter to get a reference of Model3 object before iterating over the Set. But sadly this did not happen. I got a similar exception.

As a last attempt, I modified Model2 class to contain field f1 additionally. Now the Model1-conversion.properties file consists of following information,

KeyProperty_components=f1
Element_reviewAnswers=Model2
CreateIfNull_reviewComponents=false

This solution works as expected and I could see the data populated in the Entity object.

Although third solution worked, I am reluctant to go with it since I will need to modify the autogenerated Model classes. Can someone look into this problem and provide some insights?