I may have spoke too soon! I kind of got the custom property editor solution
to work. Unfortunately I am still having the same problem as before, namely:
1. Hibernate is not overwriting the exisitng data, it's just adding it each
time.
2. God knows what will happen if I delete an element ... :(
How I finally got the custom property editor to work is by calling all of my
inputs the same name, namely:
<input type="text" id="establishmentContainers">
instead of
<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">
What this means is that instead of receiving 3 Lists:
establishmentContainers.establishment.id: [1,1]
establishmentContainers.container.id [1,2]
establishmentContainers.quantity: [5,10]
I only receive one list [1,1,5,1,2,10]. Since it always arrives in order I
can use the same code I previously used to build EstablishmentContainer
objects using the data from the Array and stick them on a List. I put this
code in the setValue() method of EstablishmentContainerPropertyEditor, and
it is being called:
public class EstablishmentFormController
{
protected void initBinder()
{
...
EstablishmentContainerPropertyEditor ecpe = new
EstablishmentContainerPropertyEditor();
binder.registerCustomEditor(List.class, ecpe);
}
}
public class EstablishmentContainerPropertyEditor
{
public void setValue(Object object)
{
List<EstablishmentContainers> establishmentContainers = new
ArrayList<EstablishmentContainers>();
// cast Object -> String[], go through Array and create
EstablishmentContainer objects ...
super.setValue(establishmentContainers);
}
}
This method is called before onSubmit(). Oddly enough, so it getValue(), not
sure why. At any rate, once I get to onSubmit() everything looks good. But
when I save the EstablishmentContainers are added to the database, without
overwriting the ones that were there already. Could this be a cascade issue?
Currently it's like this:
public class Establishment
{
private List<EstablishmentContainer> establishmentContainers;
@OneToMany(mappedBy="establishment", fetch = FetchType.EAGER, cascade = {
CascadeType.PERSIST, CascadeType.MERGE })
public List<EstablishmentContainer> getEstablishmentContainers ()
I have tried getting rid of cascade but then I get a 'object references an
unsaved transient instance' error.
At any rate, I am glad I fiiiiiiiiiiiiiiiiiiiinally got the custom property
editor to work.
Bob
syg6 wrote:
>
> 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#a13038388
Sent from the AppFuse - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]