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 <[EMAIL PROTECTED]> 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: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>