Pa3u3u / Surreal-Travel

Travel Agency System
0 stars 0 forks source link

Problem with colection in jsp. #25

Closed kiclus closed 9 years ago

kiclus commented 9 years ago

I program all day tehe next milestone, but now I have a problem and I can fix it now. it is the problem with colection im my reservation, every time I get this exception. org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: cz.muni.pa165.surrealtravel.entity.Reservation.excursions, could not initialize proxy - no Session org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857) in some forum I find something but it doesn work in my code.http://stackoverflow.com/questions/11746499/how-to-resolve-failed-to-lazily-initialize-a-collection-of-role-exception In Trip There are colection to, Roman have you same problem like me???

Pa3u3u commented 9 years ago

Remove cascade={CascadeType.PERSIST,CascadeType.MERGE} from your entity class on excursions. I had a similar problem, persisting a reservation with excursions that are already in the database, yet are detached, will try to persist them again and throw an exception about shit gone real and what not.

FiXerCz commented 9 years ago

But that will fail the tests in DAO.

EDIT: Alright, nevermind, I changed the tests and it will work with after removing that line you said to remove.

Pa3u3u commented 9 years ago

Yes, it will break two of them. But these can be "fixed" far easier than THIS problem with that nasty exception OR you can come up with a better idea, nobody is stopping you :smiley: but unfortunately, I am still fighting with adding excursions to the trip via checkboxes, so I don't have time for that.

EDIT: My drugs started to work because I remembered I DID actually find some solution, give me a moment...

FiXerCz commented 9 years ago

Wouldn't it be easier for you to add excursions from select box with miltiple selectable options? http://www.w3schools.com/tags/att_select_multiple.asp You can try it, maybe it will help.

Pa3u3u commented 9 years ago

The problem isn't with the checkboxes themselves, but with the model attribute, as it does not hold the values AFTER they get posted.

Pa3u3u commented 9 years ago

Ad the main question: The problem is caused by the fact that lazy collection requires an active transaction in order to be iterated (accessed). So, when your service returns a reservation and you access excursions, it is NOT in any active transaction, hence, it throws that cryptic exception.

So, you DON'T need to remove cascading, about that I was wrong. Add @Transactional(readonly = true) to all methods that don't already have the annotation (mainly getAllReservations).

EDIT (explanation): Afair this should preserve some read-only transaction in the context of that object. EDIT 2: but you might eventually encounter a problem with that cascading :smiley: so that needs to be solved as well

Pa3u3u commented 9 years ago

I tried something and turns out you will definitely run into a problem with cascading when you implement new reservation controller method and will try to persist a reservation, with excursions that are already in the database. But @FiXerCz already "amended" the tests (thanks for that), so you can happily remove the cascading :smirk:

FiXerCz commented 9 years ago

Note for Roman's problem with binding excursions to the model: I don't see the code so maybe I am completely "off", but this might be of help to you: http://stackoverflow.com/questions/4331532/multiple-select-in-spring-3-0-mvc The guy has multiple select with IDs of some objects and in Controller's binder he converts the ids into the objects that are supposed to be in a model.

Pa3u3u commented 9 years ago

Thank you, I have actually seen that, but it didn't work for me for some arcane reason. And even if it did, it's extremely ineffective as it would access the database for each excursion to be added into the reservation.

Besides, I already have a "cleaner" solution, I just need to do one more thing with error reporting.

EDIT: Look at me, talking about effectivity while programming in Java :pensive:

kiclus commented 9 years ago

somethigm must change in trip because before I made pull it works(1h ago) now I have this exceptiop. I I Can make reservationService.addreservation(res) persistnace exceptioin on trip.................now i write the exception Error creating bean with name 'reservationController': Invocation of init method failed; nested exception is javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: cz.muni.pa165.surrealtravel.entity.Trip org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136)

hour ago it works my init now, I try to change in intit but 99% same like hour ago public void init() {

    CustomerDTO c1 = new CustomerDTO();
    c1.setName("Honza");
    c1.setAddress("Olomouc");

    CustomerDTO c2 = new CustomerDTO();
    c2.setName("Eva");
    c2.setAddress("Olomouc");
    customerService.addCustomer(c1);
    customerService.addCustomer(c2);
    TripDTO trip= new TripDTO();

    Calendar calendar = new GregorianCalendar();
    calendar.set(2014, 6, 21);
    Date from= calendar.getTime();
    calendar.set(2014,7,3);
    Date to= calendar.getTime();

    trip.setDateFrom(from);
    trip.setDateTo(to);
    trip.setDestination("palava");
    trip.setCapacity(15);
    trip.setBasePrice(new BigDecimal(500));

    ExcursionDTO e1 = new ExcursionDTO();
    e1.setDestination("Afgánistán");
    e1.setDescription("Best of war");
    e1.setDuration(2);
    e1.setExcursionDate(from);
    e1.setPrice(new BigDecimal(0));

    ExcursionDTO e2 = new ExcursionDTO();
    e2.setDestination("Afgánistán");
    e2.setDescription("Best of army stock");
    e2.setDuration(5);
    e2.setExcursionDate(from);
    e2.setPrice(new BigDecimal(10));
    excursionService.addExcursion(e1);
    excursionService.addExcursion(e2);
    List<ExcursionDTO> excursions= new ArrayList<>();
    excursions.add(e1);
    excursions.add(e2);

    trip.setExcursions(excursions);
    tripService.addTrip(trip);

    ReservationDTO res1 = new ReservationDTO();
    res1.setExcursions(excursions);
    res1.setCustomer(c2);
    res1.setTrip(trip);

    ReservationDTO res2= new ReservationDTO();
    res2.setExcursions(excursions);
    res2.setTrip(trip);
    res2.setCustomer(c1);

    reservationService.addReservation(res1);
FiXerCz commented 9 years ago

DId you clean and build all modules?

Pa3u3u commented 9 years ago

I checked the code in the repository, your example should work, try as @FiXerCz suggests and then reply here with the result.

kiclus commented 9 years ago

still doesnt work I have a similar mistake some hours ago , I have trip.setId(someLong) and then make service.add(trip). Ind it was same exception, than I delete trip.setId befor service and it works now it doesnt work again.

Pa3u3u commented 9 years ago

"it works now it doesn't work again" ... that sounds like SchrodingerTripService, use DefaultTripService instead (sorry, I had to, fat chance :smiley:)

I will try running it locally again, give me a moment.

Pa3u3u commented 9 years ago

Mmm, it works fine here. Try running $ mvn clean then check that Tomcat is stopped and recompile the code again from NetBeans. After that, run tomcat7 : run and it should be fine. Otherwise give us precise line in your example where it fails.

kiclus commented 9 years ago

reservationService.addReservation(res1); if you delete this line it works, the last line in my init() see up. I try to restart netbeans and tomcats now

Pa3u3u commented 9 years ago

Oh I see now. But did you really remove the cascading from Reservation entity? And did you recompile the entire Maven project (not only the *Web part)? Because that exception indicates otherwise.

kiclus commented 9 years ago

I control the cascade now, And still dont work.. I will push the comit with comented the line reservationService.addreservatio(res1) try to uncomit someone the line and run if you have same mistake

EDIT: I want to try to write uncoment no uncomit :-\

Pa3u3u commented 9 years ago

All right, I will look into that.

kiclus commented 9 years ago

I thing when we erase the persistence from the trip and reservation we totaly broke it

Pa3u3u commented 9 years ago

Nonsense, it works with the trip, there must be a problem somewhere else. And I'm not going to sleep until I find it or drop dead (that would make many people happy, so I will try to avoid it)

FiXerCz commented 9 years ago

Alright I got it. Let me sumarize it into readable paragraph, I will add it as an edit to this comment.

EDIT: I just made commit. Just removed persisting of trip and customer in JPAReservationDAO (needed to be removed after removing of cascading stuff), changed the test accordingly and added still missing @Transactional(readOnly = true) annotation, These changes fixed it.

kiclus commented 9 years ago

ok I wait ,, maybe the problem is here @Override public void addReservation(Reservation reservation) { if(reservation == null) throw new NullPointerException("reservation doesnt exist."); if(reservation.getId() < 0) throw new IllegalArgumentException("reservation id must be positiv number."); if(reservation.getCustomer() == null) throw new NullPointerException("customer in reservation is null."); if(reservation.getCustomer().getClass() != Customer.class ) throw new IllegalArgumentException("customer is not customer is empty string."); if(reservation.getTrip()==null) throw new NullPointerException("trip in reservatio doesnt exist.");

    entityManager.persist(reservation.getTrip());
    entityManager.persist(reservation.getCustomer());
    entityManager.persist(reservation);

}

I persist the trip and customer, but they must exist before but I dont know .. wait for fixers coment

Pa3u3u commented 9 years ago

Of course they have to exist, but they do exist in the database according to your code, that's why it complains about persisting a detached entity.

FiXerCz commented 9 years ago

So at least this is solved. Check out edit note above. Just pull the changes, uncomment ending of ReservationController init method and that is it.

Pa3u3u commented 9 years ago

Wait a second, why did you manually persist trip and customer when you had cascading? Even so, why would you do that since it "overrides" validation and service? Why didn't it fail in tests? Why did I miss it when I checked the code (two times already)? Why... meh, screw that. I'll become a brainfuck programmer, I think I'm already mentally ill :scream:

FiXerCz commented 9 years ago

The cascading was I think just for excursions. Not for customer and trip.

Pa3u3u commented 9 years ago

Oh, right... that supports the latter observation :smiley: but still... well, if it works for you now, @kiclus , please close this issue

FiXerCz commented 9 years ago

So to complete listing of reservations you should do:

Did I miss sth?

Pa3u3u commented 9 years ago

That should be enough. You can use list of trips as a template for hidden rows, but keep in mind that it's an older snapshot, you should also specify width for columns (see comments on b770b2c).

kiclus commented 9 years ago

thanks guys a lot... I must today 1-2 hour work on it and tomorow I will continue... I must do better gui for reservation , make update for reservation , translation to eng and czech. I dont count with the persistence fuck with me so hard.

kiclus commented 9 years ago

I have same problem in row 153 in reservationControler ...reservation = reservationService.getReservationById(id); the exception is catch, but never make a reservation and in edit html every time redirect to list because of exception it it same exception problem of lazines of excursions (my list of excursions).

kiclus commented 9 years ago

I dont fix it but I made a bipas you can see in my code I dont use getReservationbyId but normal getAll rereservation. IT is not good solution but now is the best to move on. but im must be fix later

FiXerCz commented 9 years ago

@kiclus, I will make some changes to your list and form templates. Please dont commit (or change them locally to avoid collisions) them for a while. Kudos on structure of form.jsp. I have never seen anything like this :smile:

kiclus commented 9 years ago

shit i make comit just now, there is only one change add <form:select path=.... insted of <select path=

FiXerCz commented 9 years ago

Nevermind, I will deal with it.

kiclus commented 9 years ago

I Done delete and work... I dont commit jet(I wait) , but this is done

FiXerCz commented 9 years ago

I just pushed some changes, you can pull it and make delete method related commit.

kiclus commented 9 years ago

thanks a lot. I will look at it, bud i have bad feelings that How I make the entities hole in select to value(value={customer}) the POSTER think this is a string not a entity. Is posible to change it somehow? .... delete method addet and work corekt(I think)

FiXerCz commented 9 years ago

Ok, as we are in quite a time press, I will finish ReservationController, ok? I have already finished form for new reservation where excursions for the trip are loaded via ajax and now I am working on storing it into the DB, but I need to modify the controller a lot, so there would be conflicts.

kiclus commented 9 years ago

Ok , I dont go to controller- on your way

kiclus commented 9 years ago

OK any change? I feel like I dont work for last hour and must something do to pass the milestone. You use properties now too. there are some unwrited message but I dont want to make the conflict. How you see the new reservation. The update work is in queue in front of my (our) day. And I Feel in air that something hapend when we try to make updatereservation (like some exception or something will hapend because today is totaly fu***g day).

Pa3u3u commented 9 years ago

@FiXerCz I have finished my edit. Do you need help with something or should I start checking the code?

kiclus commented 9 years ago

now I make update for reservation, in progres maybe (I think fixer now have litle break, if i understand it correct because he commit support for me) EDIT: wery bad english corect

FiXerCz commented 9 years ago

You can start checking. I am just dealing with one last thing concenrning edit form for Reservations and then it will be done. I will push what I have to make the code in repository as complete as possible.

@kiclus Sry I wasnt reading messages till now. Reservation is almost done, i will finish it to make it as fast as possible. Please dont commit anything yet. I dont have not time nor energy to deal with version conflicts.

kiclus commented 9 years ago

ok you make update to???

FiXerCz commented 9 years ago

What? :)

FiXerCz commented 9 years ago

I already pushed another commit if that is what you mean..

kiclus commented 9 years ago

I am on start I only ask I nothing commit

Pa3u3u commented 9 years ago

Whoa, Ajax O_o Good work indeed, but ... why bother? You risked a severe brain damage with this... :smiley: Still, very good work indeed.

2014-11-28 19:46 GMT+01:00 kiclus notifications@github.com:

I am on start I only ask I nothing commit

— Reply to this email directly or view it on GitHub https://github.com/Cweorth/Surreal-Travel/issues/25#issuecomment-64920750 .