PDA

View Full Version : Updating a many-to-one FK column inside many-to-many set


wesleyhales
Jun 1st, 2006, 10:35 PM
I am using a many-to-many set (Services that a business provides) inside a business profile:
----------------------------------------
<class name="com.domain.BusinessProfile" table="User_Profile" lazy="false">

<id name="id" type="int">
<column name="id"/>
<generator class="native"/>
</id>

.... *more columns*

<set name="services" table="Service_Offer">
<key column="User_Profile_id"/>
<many-to-many column="Service_id" class="com.domain.Service" lazy="false"/>
</set>
------------------------------------
Inside the "Service" class I have a many-to-one pointing to a lookup for the type of service:
-------------------------------------
<class name="com.domain.Service" table="Service" lazy="false">

<id name="id" type="int">
<column name="id"/>
<generator class="native"/>
</id>

<property name="price" type="float">
<column name="price"/>
</property>

<property name="name" type="string">
<column name="name"/>
</property>

<many-to-one name="serviceType"
class="com.domain.ServiceType"
column="Service_Type_id"
foreign-key="Service_Type_id"
lazy="false"
/>
----------------------------------------
The graph/data is retrieved fine, but when I store BusinessProfile in the command object for a SimpleFormController and submit the form to do an update on the graph. I get unpredictable behaviour, this is the html/velocity to bind the set of Service objects:
-----------------------------------------
#foreach ($service in $command.businessProfile.services)

<tr>
<td style="width:200px">
#springFormInput("command.businessProfile.services[$myCount].name" "size=20")

</td>
<td style="width:200px">
#springFormInput("command.businessProfile.services[$myCount].price" "size=20")

</td>
<td style="width:200px">

#springBind( "command.businessProfile.services[$myCount].serviceType.id" )

<select name="${status.expression}">
#foreach ($serviceType in $thisView.serviceTypes)
<option value="$serviceType.id"
#if ("$serviceType.id" == "$status.value")
selected
#end
>$serviceType.value</option>
#end
</select>

</td>
<td style="width:40px">
<a href="">remove</a>
</td>
</tr>

#set ($myCount = $math.add($myCount,1))
#end
-----------------------------------------------
The form submits and validates fine. When I change the list box (the foreign key value on the many-to-one in Service object) nothing happens, all of the other data in the entire graph submits fine but the FK value will not change. BUT when I submit the last object in the set (say there are 3 com.domain.Service objects in the form and I change the value of the last one's drop down) It will update all 3 (or however many) columns for that foreign key.

It's like the value for the last object in the set is binding to the command object and updating ALL the rows in the database. But none of the other object many-to-one values will bind. *It's not anything to do with Velocity either, I tried to bind with regular html form elements and got same result.

Here is the code for retrieving the graph:
----------------------------------------
businessGroup = (BusinessGroup) session
.createQuery("select bg from BusinessGroup bg " +
"left join fetch bg.businessProfile.services se " +
"where bg.user = :user")
.setParameter("user", user)
.uniqueResult();
---------------------------------------

This is really strange and I know a lot of people have had trouble binding on domain object graphs but my life would be perfect if I could only get this one issue resolved. I think it may be in the realtionship settings, but I have tried every possible combo. Any help is greatly appreciated.