You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by blekit <bl...@gmail.com> on 2008/11/30 23:40:59 UTC
Problem with detaching elements
Hello,
I'm writing an web application using Spring, Wicket and Hibernate. It's the
first time I use this technologies, so forgive me if my question is trivial.
In my application i have a many to one relationship between Hotel and Room.
On admin panel I'd like to have possibility to edit Hotel data - including
modifying data of rooms that belong to this hotel. Unfortunately, when i try
to save modified hotel, only it's data are changed - rooms stay they were
before modification. And when i try to delete a room, i get an exception
that says i try to delete detached entity
(java.lang.IllegalArgumentException: Removing a detached instance
pl.molejnik.hotbookings.model.Room#1).
I've spent whole day looking for solutions for this, but I failed... I'd be
very grateful if anyone could tell me what i am doing wrong.
Here are relevant fragments of my code:
Room.java:
@ManyToOne(targetEntity = Hotel.class)
@JoinColumn(name = "Hotel_id")
private Hotel hotel;
Hotel.java
@OneToMany(mappedBy = "hotel", cascade = { CascadeType.PERSIST,
CascadeType.MERGE, CascadeType.REMOVE })
private List<Room> rooms;
DomainObjectModel
public class DomainObjectModel<T extends DomainObject> extends
LoadableDetachableModel
{
@SpringBean
private IHotelbookingsService service;
private final Class<T> type;
private final Long id;
public DomainObjectModel(Class<T> type, Long id) {
InjectorHolder.getInjector().inject(this);
this.type = type;
this.id = id;
}
@SuppressWarnings("unchecked")
public DomainObjectModel(T domainObject)
{
super(domainObject);
InjectorHolder.getInjector().inject(this);
this.type = (Class<T>) domainObject.getClass();
this.id = domainObject.getId();
}
@Override
protected Object load() {
return service.load(type, id);
}
}
EditHotelPanel.java
public class EditHotelPanel extends Panel
{
private IModel model;
private Form form;
PageableListView rooms;
@SpringBean
IHotelbookingsService service;
public EditHotelPanel(String id, Long hotelId) {
super(id);
this.setOutputMarkupId(true);
model = new CompoundPropertyModel(new
DomainObjectModel<Hotel>(Hotel.class,
hotelId));
setModel(model);
form = new Form("form");
form.setOutputMarkupId(true);
form.add(new TextField("name"));
form.add(new TextField("numberOfStars"));
form.add(new TextField("address.city"));
rooms = new PageableListView("rooms",
((Hotel)model.getObject()).getRooms(), 10) {
@Override
protected void populateItem(ListItem item) {
DomainObjectModel<Room> m = new DomainObjectModel<Room>((Room)
item.getModelObject());
final Room r = (Room) m.getObject();
item.add(new TextField("costPerNight", new PropertyModel(r,
"costPerNight")));
item.add(new TextField("numberOfBeds", new PropertyModel(r,
"numberOfBeds")));
item.add(new CheckBox("ensuite",
new PropertyModel(r, "ensuite")));
item.add(new Link("deleteRoom"){
@Override
public void onClick() {
service.removeRoom(r);
}
});
}
};
form.add(rooms);
SubmitLink save = new SubmitLink("saveButton") {
@Override
public void onSubmit() {
Hotel h = (Hotel)model.getObject();
service.saveHotel(h);
setResponsePage(Index.class);
}
};
form.add(save);
add(form);
}
}
EditHotelPanel.html
<html xmlns:wicket>
<body>
<wicket:panel>
<form wicket:id="form">
Nazwa: <input type="text" wicket:id="name"/>
<br/>
Liczba gwiazdek: <input type="text"
wicket:id="numberOfStars"/>
<br/>
Miasto: <input type="text" wicket:id="address.city"/>
<br/>
<div>
Pokoje:
<br/>
<div>
Liczba miejsc
Cena
Lazienka
</div>
<div wicket:id="rooms">
<input type="text" wicket:id="numberOfBeds"/>
<input type="text" wicket:id="costPerNight"/>
<input type="checkbox" wicket:id="ensuite"/>
# Usuń
</div>
</div>
<input type="submit" value="Zapisz" wicket:id="saveButton"/>
</form>
</wicket:panel>
</body>
</html>
Best regards,
Michał Olejnik
--
View this message in context: http://www.nabble.com/Problem-with-detaching-elements-tp20763305p20763305.html
Sent from the Wicket - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Problem with detaching elements
Posted by blekit <bl...@gmail.com>.
Well, that of course worked - as I wrote earlier I'm very grateful.
Thank you very much for your help once again.
igor.vaynberg wrote:
>
> final Room r = (Room) m.getObject();
> item.add(new TextField("costPerNight",
> new PropertyModel(r,
> "costPerNight")));
>
> there you are binding your model directly to the object thus keeping a
> reference, instead
> new propertymodel(m, "costPerNight");
>
> -igor
>
> On Sun, Nov 30, 2008 at 3:51 PM, blekit <bl...@gmail.com> wrote:
>>
>> Hello,
>>
>> thanks for link to the article - it was very interesting. However, I
>> still
>> don't see clear connection between this case and my problem - I use my
>> own
>> DomainObjectModel which extends LoadableDetachableModel (and after
>> reading
>> once more the section about them i still can't see any mistakes in mine
>> usage of them - maybe i need some sleep after all).
>>
>> I think i noticed where the problem lies, but still I don't have any clue
>> how to fix it. The hotel's data including rooms list are loaded correctly
>> and rendered on the first time. But when i submit the form, only hotel's
>> direct data (name, address, etc.) are updated. I discovered, that the
>> list
>> of rooms i use as a model for my PageableListView is probably detached
>> immidiately after render and the changes made on her aren't propagated to
>> hotel when i persist it. Still, as I said before, I don't know why it
>> happens and I'd be very grateful for some more explicite answer.
>>
>> Thanks in advance.
>> Best regards,
>> Michał Olejnik
>>
>> PS. If you need some more code to help me let me know and i'll paste it
>>
>> igor.vaynberg wrote:
>>>
>>> go to wicketinaction.com and search for "smart entity model"
>>>
>>> also read the wiki page called models, especially detachable models
>>> secton.
>>>
>>> -igor
>>>
>>> On Sun, Nov 30, 2008 at 2:40 PM, blekit <bl...@gmail.com> wrote:
>>>>
>>>> Hello,
>>>>
>>>> I'm writing an web application using Spring, Wicket and Hibernate. It's
>>>> the
>>>> first time I use this technologies, so forgive me if my question is
>>>> trivial.
>>>>
>>>> In my application i have a many to one relationship between Hotel and
>>>> Room.
>>>> On admin panel I'd like to have possibility to edit Hotel data -
>>>> including
>>>> modifying data of rooms that belong to this hotel. Unfortunately, when
>>>> i
>>>> try
>>>> to save modified hotel, only it's data are changed - rooms stay they
>>>> were
>>>> before modification. And when i try to delete a room, i get an
>>>> exception
>>>> that says i try to delete detached entity
>>>> (java.lang.IllegalArgumentException: Removing a detached instance
>>>> pl.molejnik.hotbookings.model.Room#1).
>>>>
>>>> I've spent whole day looking for solutions for this, but I failed...
>>>> I'd
>>>> be
>>>> very grateful if anyone could tell me what i am doing wrong.
>>>>
>>>> Here are relevant fragments of my code:
>>>>
>>>> Room.java:
>>>>
>>>> @ManyToOne(targetEntity = Hotel.class)
>>>> @JoinColumn(name = "Hotel_id")
>>>> private Hotel hotel;
>>>>
>>>> Hotel.java
>>>>
>>>> @OneToMany(mappedBy = "hotel", cascade = { CascadeType.PERSIST,
>>>> CascadeType.MERGE, CascadeType.REMOVE })
>>>> private List<Room> rooms;
>>>>
>>>> DomainObjectModel
>>>>
>>>> public class DomainObjectModel<T extends DomainObject> extends
>>>> LoadableDetachableModel
>>>> {
>>>>
>>>> @SpringBean
>>>> private IHotelbookingsService service;
>>>>
>>>> private final Class<T> type;
>>>>
>>>> private final Long id;
>>>>
>>>> public DomainObjectModel(Class<T> type, Long id) {
>>>> InjectorHolder.getInjector().inject(this);
>>>> this.type = type;
>>>> this.id = id;
>>>> }
>>>>
>>>> @SuppressWarnings("unchecked")
>>>> public DomainObjectModel(T domainObject)
>>>> {
>>>> super(domainObject);
>>>> InjectorHolder.getInjector().inject(this);
>>>> this.type = (Class<T>) domainObject.getClass();
>>>> this.id = domainObject.getId();
>>>> }
>>>>
>>>> @Override
>>>> protected Object load() {
>>>> return service.load(type, id);
>>>> }
>>>>
>>>> }
>>>>
>>>> EditHotelPanel.java
>>>>
>>>> public class EditHotelPanel extends Panel
>>>> {
>>>>
>>>> private IModel model;
>>>>
>>>> private Form form;
>>>>
>>>> PageableListView rooms;
>>>>
>>>> @SpringBean
>>>> IHotelbookingsService service;
>>>>
>>>> public EditHotelPanel(String id, Long hotelId) {
>>>> super(id);
>>>> this.setOutputMarkupId(true);
>>>> model = new CompoundPropertyModel(new
>>>> DomainObjectModel<Hotel>(Hotel.class,
>>>> hotelId));
>>>> setModel(model);
>>>> form = new Form("form");
>>>> form.setOutputMarkupId(true);
>>>> form.add(new TextField("name"));
>>>> form.add(new TextField("numberOfStars"));
>>>> form.add(new TextField("address.city"));
>>>>
>>>> rooms = new PageableListView("rooms",
>>>> ((Hotel)model.getObject()).getRooms(), 10) {
>>>>
>>>>
>>>> @Override
>>>> protected void populateItem(ListItem item) {
>>>> DomainObjectModel<Room> m = new
>>>> DomainObjectModel<Room>((Room)
>>>> item.getModelObject());
>>>> final Room r = (Room) m.getObject();
>>>> item.add(new TextField("costPerNight",
>>>> new
>>>> PropertyModel(r,
>>>> "costPerNight")));
>>>> item.add(new TextField("numberOfBeds",
>>>> new
>>>> PropertyModel(r,
>>>> "numberOfBeds")));
>>>> item.add(new CheckBox("ensuite",
>>>> new PropertyModel(r,
>>>> "ensuite")));
>>>> item.add(new Link("deleteRoom"){
>>>>
>>>> @Override
>>>> public void onClick() {
>>>> service.removeRoom(r);
>>>> }
>>>>
>>>> });
>>>> }
>>>> };
>>>> form.add(rooms);
>>>>
>>>> SubmitLink save = new SubmitLink("saveButton") {
>>>>
>>>> @Override
>>>> public void onSubmit() {
>>>> Hotel h = (Hotel)model.getObject();
>>>> service.saveHotel(h);
>>>> setResponsePage(Index.class);
>>>> }
>>>>
>>>> };
>>>> form.add(save);
>>>> add(form);
>>>> }
>>>>
>>>> }
>>>>
>>>> EditHotelPanel.html
>>>>
>>>> <html xmlns:wicket>
>>>> <body>
>>>> <wicket:panel>
>>>> <form wicket:id="form">
>>>> Nazwa: <input type="text" wicket:id="name"/>
>>>> <br/>
>>>> Liczba gwiazdek: <input type="text"
>>>> wicket:id="numberOfStars"/>
>>>> <br/>
>>>> Miasto: <input type="text" wicket:id="address.city"/>
>>>> <br/>
>>>> <div>
>>>> Pokoje:
>>>> <br/>
>>>> <div>
>>>> Liczba miejsc
>>>> Cena
>>>> Lazienka
>>>> </div>
>>>> <div wicket:id="rooms">
>>>> <input type="text" wicket:id="numberOfBeds"/>
>>>> <input type="text"
>>>> wicket:id="costPerNight"/>
>>>> <input type="checkbox"
>>>> wicket:id="ensuite"/>
>>>> # Usuń
>>>> </div>
>>>> </div>
>>>> <input type="submit" value="Zapisz"
>>>> wicket:id="saveButton"/>
>>>> </form>
>>>> </wicket:panel>
>>>> </body>
>>>> </html>
>>>>
>>>> Best regards,
>>>> Michał Olejnik
>>>>
>>>>
>>>> --
>>>> View this message in context:
>>>> http://www.nabble.com/Problem-with-detaching-elements-tp20763305p20763305.html
>>>> Sent from the Wicket - User mailing list archive at Nabble.com.
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>>>> For additional commands, e-mail: users-help@wicket.apache.org
>>>>
>>>>
>>>
>>>
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Problem-with-detaching-elements-tp20763305p20763953.html
>> Sent from the Wicket - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>
>
--
View this message in context: http://www.nabble.com/Problem-with-detaching-elements-tp20763305p20764142.html
Sent from the Wicket - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Problem with detaching elements
Posted by Igor Vaynberg <ig...@gmail.com>.
final Room r = (Room) m.getObject();
item.add(new TextField("costPerNight",
new PropertyModel(r,
"costPerNight")));
there you are binding your model directly to the object thus keeping a
reference, instead
new propertymodel(m, "costPerNight");
-igor
On Sun, Nov 30, 2008 at 3:51 PM, blekit <bl...@gmail.com> wrote:
>
> Hello,
>
> thanks for link to the article - it was very interesting. However, I still
> don't see clear connection between this case and my problem - I use my own
> DomainObjectModel which extends LoadableDetachableModel (and after reading
> once more the section about them i still can't see any mistakes in mine
> usage of them - maybe i need some sleep after all).
>
> I think i noticed where the problem lies, but still I don't have any clue
> how to fix it. The hotel's data including rooms list are loaded correctly
> and rendered on the first time. But when i submit the form, only hotel's
> direct data (name, address, etc.) are updated. I discovered, that the list
> of rooms i use as a model for my PageableListView is probably detached
> immidiately after render and the changes made on her aren't propagated to
> hotel when i persist it. Still, as I said before, I don't know why it
> happens and I'd be very grateful for some more explicite answer.
>
> Thanks in advance.
> Best regards,
> Michał Olejnik
>
> PS. If you need some more code to help me let me know and i'll paste it
>
> igor.vaynberg wrote:
>>
>> go to wicketinaction.com and search for "smart entity model"
>>
>> also read the wiki page called models, especially detachable models
>> secton.
>>
>> -igor
>>
>> On Sun, Nov 30, 2008 at 2:40 PM, blekit <bl...@gmail.com> wrote:
>>>
>>> Hello,
>>>
>>> I'm writing an web application using Spring, Wicket and Hibernate. It's
>>> the
>>> first time I use this technologies, so forgive me if my question is
>>> trivial.
>>>
>>> In my application i have a many to one relationship between Hotel and
>>> Room.
>>> On admin panel I'd like to have possibility to edit Hotel data -
>>> including
>>> modifying data of rooms that belong to this hotel. Unfortunately, when i
>>> try
>>> to save modified hotel, only it's data are changed - rooms stay they were
>>> before modification. And when i try to delete a room, i get an exception
>>> that says i try to delete detached entity
>>> (java.lang.IllegalArgumentException: Removing a detached instance
>>> pl.molejnik.hotbookings.model.Room#1).
>>>
>>> I've spent whole day looking for solutions for this, but I failed... I'd
>>> be
>>> very grateful if anyone could tell me what i am doing wrong.
>>>
>>> Here are relevant fragments of my code:
>>>
>>> Room.java:
>>>
>>> @ManyToOne(targetEntity = Hotel.class)
>>> @JoinColumn(name = "Hotel_id")
>>> private Hotel hotel;
>>>
>>> Hotel.java
>>>
>>> @OneToMany(mappedBy = "hotel", cascade = { CascadeType.PERSIST,
>>> CascadeType.MERGE, CascadeType.REMOVE })
>>> private List<Room> rooms;
>>>
>>> DomainObjectModel
>>>
>>> public class DomainObjectModel<T extends DomainObject> extends
>>> LoadableDetachableModel
>>> {
>>>
>>> @SpringBean
>>> private IHotelbookingsService service;
>>>
>>> private final Class<T> type;
>>>
>>> private final Long id;
>>>
>>> public DomainObjectModel(Class<T> type, Long id) {
>>> InjectorHolder.getInjector().inject(this);
>>> this.type = type;
>>> this.id = id;
>>> }
>>>
>>> @SuppressWarnings("unchecked")
>>> public DomainObjectModel(T domainObject)
>>> {
>>> super(domainObject);
>>> InjectorHolder.getInjector().inject(this);
>>> this.type = (Class<T>) domainObject.getClass();
>>> this.id = domainObject.getId();
>>> }
>>>
>>> @Override
>>> protected Object load() {
>>> return service.load(type, id);
>>> }
>>>
>>> }
>>>
>>> EditHotelPanel.java
>>>
>>> public class EditHotelPanel extends Panel
>>> {
>>>
>>> private IModel model;
>>>
>>> private Form form;
>>>
>>> PageableListView rooms;
>>>
>>> @SpringBean
>>> IHotelbookingsService service;
>>>
>>> public EditHotelPanel(String id, Long hotelId) {
>>> super(id);
>>> this.setOutputMarkupId(true);
>>> model = new CompoundPropertyModel(new
>>> DomainObjectModel<Hotel>(Hotel.class,
>>> hotelId));
>>> setModel(model);
>>> form = new Form("form");
>>> form.setOutputMarkupId(true);
>>> form.add(new TextField("name"));
>>> form.add(new TextField("numberOfStars"));
>>> form.add(new TextField("address.city"));
>>>
>>> rooms = new PageableListView("rooms",
>>> ((Hotel)model.getObject()).getRooms(), 10) {
>>>
>>>
>>> @Override
>>> protected void populateItem(ListItem item) {
>>> DomainObjectModel<Room> m = new
>>> DomainObjectModel<Room>((Room)
>>> item.getModelObject());
>>> final Room r = (Room) m.getObject();
>>> item.add(new TextField("costPerNight", new
>>> PropertyModel(r,
>>> "costPerNight")));
>>> item.add(new TextField("numberOfBeds", new
>>> PropertyModel(r,
>>> "numberOfBeds")));
>>> item.add(new CheckBox("ensuite",
>>> new PropertyModel(r,
>>> "ensuite")));
>>> item.add(new Link("deleteRoom"){
>>>
>>> @Override
>>> public void onClick() {
>>> service.removeRoom(r);
>>> }
>>>
>>> });
>>> }
>>> };
>>> form.add(rooms);
>>>
>>> SubmitLink save = new SubmitLink("saveButton") {
>>>
>>> @Override
>>> public void onSubmit() {
>>> Hotel h = (Hotel)model.getObject();
>>> service.saveHotel(h);
>>> setResponsePage(Index.class);
>>> }
>>>
>>> };
>>> form.add(save);
>>> add(form);
>>> }
>>>
>>> }
>>>
>>> EditHotelPanel.html
>>>
>>> <html xmlns:wicket>
>>> <body>
>>> <wicket:panel>
>>> <form wicket:id="form">
>>> Nazwa: <input type="text" wicket:id="name"/>
>>> <br/>
>>> Liczba gwiazdek: <input type="text"
>>> wicket:id="numberOfStars"/>
>>> <br/>
>>> Miasto: <input type="text" wicket:id="address.city"/>
>>> <br/>
>>> <div>
>>> Pokoje:
>>> <br/>
>>> <div>
>>> Liczba miejsc
>>> Cena
>>> Lazienka
>>> </div>
>>> <div wicket:id="rooms">
>>> <input type="text" wicket:id="numberOfBeds"/>
>>> <input type="text"
>>> wicket:id="costPerNight"/>
>>> <input type="checkbox"
>>> wicket:id="ensuite"/>
>>> # Usuń
>>> </div>
>>> </div>
>>> <input type="submit" value="Zapisz"
>>> wicket:id="saveButton"/>
>>> </form>
>>> </wicket:panel>
>>> </body>
>>> </html>
>>>
>>> Best regards,
>>> Michał Olejnik
>>>
>>>
>>> --
>>> View this message in context:
>>> http://www.nabble.com/Problem-with-detaching-elements-tp20763305p20763305.html
>>> Sent from the Wicket - User mailing list archive at Nabble.com.
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>>> For additional commands, e-mail: users-help@wicket.apache.org
>>>
>>>
>>
>>
>
> --
> View this message in context: http://www.nabble.com/Problem-with-detaching-elements-tp20763305p20763953.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
Re: Problem with detaching elements
Posted by blekit <bl...@gmail.com>.
Hello,
thanks for link to the article - it was very interesting. However, I still
don't see clear connection between this case and my problem - I use my own
DomainObjectModel which extends LoadableDetachableModel (and after reading
once more the section about them i still can't see any mistakes in mine
usage of them - maybe i need some sleep after all).
I think i noticed where the problem lies, but still I don't have any clue
how to fix it. The hotel's data including rooms list are loaded correctly
and rendered on the first time. But when i submit the form, only hotel's
direct data (name, address, etc.) are updated. I discovered, that the list
of rooms i use as a model for my PageableListView is probably detached
immidiately after render and the changes made on her aren't propagated to
hotel when i persist it. Still, as I said before, I don't know why it
happens and I'd be very grateful for some more explicite answer.
Thanks in advance.
Best regards,
Michał Olejnik
PS. If you need some more code to help me let me know and i'll paste it
igor.vaynberg wrote:
>
> go to wicketinaction.com and search for "smart entity model"
>
> also read the wiki page called models, especially detachable models
> secton.
>
> -igor
>
> On Sun, Nov 30, 2008 at 2:40 PM, blekit <bl...@gmail.com> wrote:
>>
>> Hello,
>>
>> I'm writing an web application using Spring, Wicket and Hibernate. It's
>> the
>> first time I use this technologies, so forgive me if my question is
>> trivial.
>>
>> In my application i have a many to one relationship between Hotel and
>> Room.
>> On admin panel I'd like to have possibility to edit Hotel data -
>> including
>> modifying data of rooms that belong to this hotel. Unfortunately, when i
>> try
>> to save modified hotel, only it's data are changed - rooms stay they were
>> before modification. And when i try to delete a room, i get an exception
>> that says i try to delete detached entity
>> (java.lang.IllegalArgumentException: Removing a detached instance
>> pl.molejnik.hotbookings.model.Room#1).
>>
>> I've spent whole day looking for solutions for this, but I failed... I'd
>> be
>> very grateful if anyone could tell me what i am doing wrong.
>>
>> Here are relevant fragments of my code:
>>
>> Room.java:
>>
>> @ManyToOne(targetEntity = Hotel.class)
>> @JoinColumn(name = "Hotel_id")
>> private Hotel hotel;
>>
>> Hotel.java
>>
>> @OneToMany(mappedBy = "hotel", cascade = { CascadeType.PERSIST,
>> CascadeType.MERGE, CascadeType.REMOVE })
>> private List<Room> rooms;
>>
>> DomainObjectModel
>>
>> public class DomainObjectModel<T extends DomainObject> extends
>> LoadableDetachableModel
>> {
>>
>> @SpringBean
>> private IHotelbookingsService service;
>>
>> private final Class<T> type;
>>
>> private final Long id;
>>
>> public DomainObjectModel(Class<T> type, Long id) {
>> InjectorHolder.getInjector().inject(this);
>> this.type = type;
>> this.id = id;
>> }
>>
>> @SuppressWarnings("unchecked")
>> public DomainObjectModel(T domainObject)
>> {
>> super(domainObject);
>> InjectorHolder.getInjector().inject(this);
>> this.type = (Class<T>) domainObject.getClass();
>> this.id = domainObject.getId();
>> }
>>
>> @Override
>> protected Object load() {
>> return service.load(type, id);
>> }
>>
>> }
>>
>> EditHotelPanel.java
>>
>> public class EditHotelPanel extends Panel
>> {
>>
>> private IModel model;
>>
>> private Form form;
>>
>> PageableListView rooms;
>>
>> @SpringBean
>> IHotelbookingsService service;
>>
>> public EditHotelPanel(String id, Long hotelId) {
>> super(id);
>> this.setOutputMarkupId(true);
>> model = new CompoundPropertyModel(new
>> DomainObjectModel<Hotel>(Hotel.class,
>> hotelId));
>> setModel(model);
>> form = new Form("form");
>> form.setOutputMarkupId(true);
>> form.add(new TextField("name"));
>> form.add(new TextField("numberOfStars"));
>> form.add(new TextField("address.city"));
>>
>> rooms = new PageableListView("rooms",
>> ((Hotel)model.getObject()).getRooms(), 10) {
>>
>>
>> @Override
>> protected void populateItem(ListItem item) {
>> DomainObjectModel<Room> m = new
>> DomainObjectModel<Room>((Room)
>> item.getModelObject());
>> final Room r = (Room) m.getObject();
>> item.add(new TextField("costPerNight", new
>> PropertyModel(r,
>> "costPerNight")));
>> item.add(new TextField("numberOfBeds", new
>> PropertyModel(r,
>> "numberOfBeds")));
>> item.add(new CheckBox("ensuite",
>> new PropertyModel(r,
>> "ensuite")));
>> item.add(new Link("deleteRoom"){
>>
>> @Override
>> public void onClick() {
>> service.removeRoom(r);
>> }
>>
>> });
>> }
>> };
>> form.add(rooms);
>>
>> SubmitLink save = new SubmitLink("saveButton") {
>>
>> @Override
>> public void onSubmit() {
>> Hotel h = (Hotel)model.getObject();
>> service.saveHotel(h);
>> setResponsePage(Index.class);
>> }
>>
>> };
>> form.add(save);
>> add(form);
>> }
>>
>> }
>>
>> EditHotelPanel.html
>>
>> <html xmlns:wicket>
>> <body>
>> <wicket:panel>
>> <form wicket:id="form">
>> Nazwa: <input type="text" wicket:id="name"/>
>> <br/>
>> Liczba gwiazdek: <input type="text"
>> wicket:id="numberOfStars"/>
>> <br/>
>> Miasto: <input type="text" wicket:id="address.city"/>
>> <br/>
>> <div>
>> Pokoje:
>> <br/>
>> <div>
>> Liczba miejsc
>> Cena
>> Lazienka
>> </div>
>> <div wicket:id="rooms">
>> <input type="text" wicket:id="numberOfBeds"/>
>> <input type="text"
>> wicket:id="costPerNight"/>
>> <input type="checkbox"
>> wicket:id="ensuite"/>
>> # Usuń
>> </div>
>> </div>
>> <input type="submit" value="Zapisz"
>> wicket:id="saveButton"/>
>> </form>
>> </wicket:panel>
>> </body>
>> </html>
>>
>> Best regards,
>> Michał Olejnik
>>
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Problem-with-detaching-elements-tp20763305p20763305.html
>> Sent from the Wicket - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>
>
--
View this message in context: http://www.nabble.com/Problem-with-detaching-elements-tp20763305p20763953.html
Sent from the Wicket - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Problem with detaching elements
Posted by Igor Vaynberg <ig...@gmail.com>.
go to wicketinaction.com and search for "smart entity model"
also read the wiki page called models, especially detachable models secton.
-igor
On Sun, Nov 30, 2008 at 2:40 PM, blekit <bl...@gmail.com> wrote:
>
> Hello,
>
> I'm writing an web application using Spring, Wicket and Hibernate. It's the
> first time I use this technologies, so forgive me if my question is trivial.
>
> In my application i have a many to one relationship between Hotel and Room.
> On admin panel I'd like to have possibility to edit Hotel data - including
> modifying data of rooms that belong to this hotel. Unfortunately, when i try
> to save modified hotel, only it's data are changed - rooms stay they were
> before modification. And when i try to delete a room, i get an exception
> that says i try to delete detached entity
> (java.lang.IllegalArgumentException: Removing a detached instance
> pl.molejnik.hotbookings.model.Room#1).
>
> I've spent whole day looking for solutions for this, but I failed... I'd be
> very grateful if anyone could tell me what i am doing wrong.
>
> Here are relevant fragments of my code:
>
> Room.java:
>
> @ManyToOne(targetEntity = Hotel.class)
> @JoinColumn(name = "Hotel_id")
> private Hotel hotel;
>
> Hotel.java
>
> @OneToMany(mappedBy = "hotel", cascade = { CascadeType.PERSIST,
> CascadeType.MERGE, CascadeType.REMOVE })
> private List<Room> rooms;
>
> DomainObjectModel
>
> public class DomainObjectModel<T extends DomainObject> extends
> LoadableDetachableModel
> {
>
> @SpringBean
> private IHotelbookingsService service;
>
> private final Class<T> type;
>
> private final Long id;
>
> public DomainObjectModel(Class<T> type, Long id) {
> InjectorHolder.getInjector().inject(this);
> this.type = type;
> this.id = id;
> }
>
> @SuppressWarnings("unchecked")
> public DomainObjectModel(T domainObject)
> {
> super(domainObject);
> InjectorHolder.getInjector().inject(this);
> this.type = (Class<T>) domainObject.getClass();
> this.id = domainObject.getId();
> }
>
> @Override
> protected Object load() {
> return service.load(type, id);
> }
>
> }
>
> EditHotelPanel.java
>
> public class EditHotelPanel extends Panel
> {
>
> private IModel model;
>
> private Form form;
>
> PageableListView rooms;
>
> @SpringBean
> IHotelbookingsService service;
>
> public EditHotelPanel(String id, Long hotelId) {
> super(id);
> this.setOutputMarkupId(true);
> model = new CompoundPropertyModel(new
> DomainObjectModel<Hotel>(Hotel.class,
> hotelId));
> setModel(model);
> form = new Form("form");
> form.setOutputMarkupId(true);
> form.add(new TextField("name"));
> form.add(new TextField("numberOfStars"));
> form.add(new TextField("address.city"));
>
> rooms = new PageableListView("rooms",
> ((Hotel)model.getObject()).getRooms(), 10) {
>
>
> @Override
> protected void populateItem(ListItem item) {
> DomainObjectModel<Room> m = new DomainObjectModel<Room>((Room)
> item.getModelObject());
> final Room r = (Room) m.getObject();
> item.add(new TextField("costPerNight", new PropertyModel(r,
> "costPerNight")));
> item.add(new TextField("numberOfBeds", new PropertyModel(r,
> "numberOfBeds")));
> item.add(new CheckBox("ensuite",
> new PropertyModel(r, "ensuite")));
> item.add(new Link("deleteRoom"){
>
> @Override
> public void onClick() {
> service.removeRoom(r);
> }
>
> });
> }
> };
> form.add(rooms);
>
> SubmitLink save = new SubmitLink("saveButton") {
>
> @Override
> public void onSubmit() {
> Hotel h = (Hotel)model.getObject();
> service.saveHotel(h);
> setResponsePage(Index.class);
> }
>
> };
> form.add(save);
> add(form);
> }
>
> }
>
> EditHotelPanel.html
>
> <html xmlns:wicket>
> <body>
> <wicket:panel>
> <form wicket:id="form">
> Nazwa: <input type="text" wicket:id="name"/>
> <br/>
> Liczba gwiazdek: <input type="text"
> wicket:id="numberOfStars"/>
> <br/>
> Miasto: <input type="text" wicket:id="address.city"/>
> <br/>
> <div>
> Pokoje:
> <br/>
> <div>
> Liczba miejsc
> Cena
> Lazienka
> </div>
> <div wicket:id="rooms">
> <input type="text" wicket:id="numberOfBeds"/>
> <input type="text" wicket:id="costPerNight"/>
> <input type="checkbox" wicket:id="ensuite"/>
> # Usuń
> </div>
> </div>
> <input type="submit" value="Zapisz" wicket:id="saveButton"/>
> </form>
> </wicket:panel>
> </body>
> </html>
>
> Best regards,
> Michał Olejnik
>
>
> --
> View this message in context: http://www.nabble.com/Problem-with-detaching-elements-tp20763305p20763305.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>