Well I've more or less given up on getting a reply to 
http://www.nabble.com/Best-way-to-save-ManyToOne-relationships-with-Hibernate-and-Spring-MVC-tf4553758s2369.html
my previous post . After searching for two days I haven't found anything
remotely like a 'Silver Bullet' that will do what I need to do, namely with
these classes:

class Establishment
{
  private List<EstablishmentContainer> establishmentContainers;

class EstablishmentContainer
{
  private Long idEstablishment;
  private Long idContainer;
  private int quantity;

to take data from a form like this:

<input type="text" id="establishmentContainers.establishment.id" value="1">
<input type="text" id="establishmentContainers.container.id" value="1">
<input type="text" id="establishmentContainers.quantity" value="5">

<input type="text" id="establishmentContainers.establishment.id" value="1">
<input type="text" id="establishmentContainers.container.id" value="2">
<input type="text" id="establishmentContainers.quantity" value="10">

package each one into an EstablishmentContainer object, add it to a List and
assign the List to the establishmentContainers field of the Establishment
class. I've looked at PropertyEditorSupport and CustomCollectionEditor,
neither one seems to do the trick.

So I have decided to do it manually. The problem I am having is that when I
do this in onSubmit():

String[] idContainers =
request.getParameterValues("establecimientoContainers.container.id");
String[] quantities =
request.getParameterValues("establecimientoContainers.quantity");

List<EstablishmentContainer> establishmentContainers = new
ArrayList<EstablishmentContainer>();
for (int i=0; i<idContainers.length; i++)
{
  EstablishmentContainer establishmentContainer = new
EstablishmentContainer();
  establishmentContainer.setEstablishment(establishment);
  establishmentContainer.setContainer(containerManager.get(new
Long(idContainerss[i])));
  establishmentContainer.setQuantity(new Integer(quantities[i]));
  establishmentContainer.add(establishmentContainer);
}

establishment.setEstablishmentContainer(establishmentContainer);

... Hibernate simply adds then new EstablishmentContainers, without
overwriting the old ones. Which means if I started with 2, and do a submit
without changing anything, I now have 4! If I add 1 I get 5! I am sure this
is Hibernate 101 and it's looked down upon to mess with the data like this
but I don't see any other option.

Or should I be doing this in formBackingObject()? Of course since
formBackingObject is called both when you load and save, I'd need a way to
tell Spring only to do it when a save (submit) is performed.

How can I do this?

Thanks!
Bob
-- 
View this message in context: 
http://www.nabble.com/How-to-manually-change-a-Collection-with-Hibernate---Spring-MVC-tf4568055s2369.html#a13037569
Sent from the AppFuse - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to