Persistence.
I'll repeat my plea for patience as I'm new to Java and Wicket, but have some minimal experience with ASP.net forms (not MVC). I've reached that point in the learning process where I want to interact with a database and I wish to use entities and Hibernate to make it easier. From what I've seen in various blogs and forums, some say you shouldn't use entities as models, yet others do it with LDMs. Since I'm taking baby steps, I want to start by using entities as models, unless someone convinces me I'm wasting my time. Following the basic Hibernate tutorials for persisting simple classes, I've managed to make the following work in Wicket. In the LDM load... @Override public Person load() { Session session = WicketApp.sessionFactory.openSession(); session.beginTransaction(); Person person = (Person) session.get(Person.class, 1L); session.getTransaction().commit(); session.close(); return person; } In the form I update the evil entity model with text boxes, using a CPM containing the LDM. In the submit button... public void onSubmit() { Session session = WicketApp.sessionFactory.openSession(); session.beginTransaction(); session.update(ldm.getObject()); session.getTransaction().commit(); session.close(); this.setResponsePage(EndPage.class); } The sessionFactory is a static member of the WicketApp application class, initialized in the init() method. This seems to work, but I suppose there are all kinds of faulty design patterns used here. My main concern is... how can I do this without opening a new Hibernate session in onSubmit()? During postback, I think I should be able to reuse the same session opened at ldm.load() in onSubmit() also, as it all occurs in the same request. Is this possible? Thanks for your assistance. Please feel free to point out every flaw.
RE: Store models short-term
Thanks dretzlaff for your reponse. Indeed overriding detach elicits the behavior I desired. Per your warnings, I should determine if this approach is really more efficient. I wonder how many occasions in my programming career I've assumed my way is best, when the complete opposite is true... > Date: Fri, 4 May 2012 15:15:08 -0700 > Subject: Re: Store models short-term > From: dretzl...@gmail.com > To: users@wicket.apache.org > > Hi, Jason. Welcome to Wicket! > > If you want to tie an entity to a page, best save the entity within the > page itself. You can do this by using a simple o.a.w.model.Model. If you > don't want to detach between requests, then LDM is not a good fit. > > There are use cases where serializing entities at the app level (instead of > the database) makes sense, such as wizards where saving prematurely results > in an invalid data model. However, if your motivation is simply performance > then I suggest you verify your assumptions. Retrieving entities by primary > key is generally very fast, and if it's not fast then it's because it's > really large and you probably don't want that serialized in the session > anyway. The "optimization" is especially moot in clusters where the > undetached entities are replicated across the network as part of the HTTP > session. And there are other disadvantages such as having to deal with > detached entities, with potentially stale state. > > I'll mention one hack for which another Wicket user should rightly > reprimand me. As I mentioned recently, Wicket keeps the most recently > accessed page is a deserialized state to optimize serving the next request. > All components are still detached, but if you override IModel#detach() in > your LDM and suppress super.detach() then your entity will hang around. > This has the behavior you describe, since (1) the entity does not need to > be reloaded on subsequent requests, and since it's object reference is > transient (2) it goes away as soon as another page is accessed, and (3) it > does not get replicated among the cluster. > > Best of luck, > Dan > > On Fri, May 4, 2012 at 2:40 PM, JASON HOLT wrote: > > > > > I'm new to Java and Wicket. My only previous experince with web > > applications has been with Asp.net forms (not MVC). Please be patient; > > coming from the postback event paradigm, I'm struggling to grasp the > > concepts in Wicket. In my simple scenario, assume there is no AJAX. I need > > to build the model from a database. If I use an LDM, on a postback Wicket > > calls to the database to rebuild the model before updating it with the new > > values. But if I don't use an LDM, Wicket will serialize the model in the > > PageMap. I would like to keep the model 'in memory' long enough to process > > the postback to avoid unecessary calls to the database, but release it when > > I have moved on to a different page. I thought of something like this... In > > the LDM @Override > > public Object load() > > { > > ...somehow get the session. > > if (session.getAttribute("PageAModel")!=null) > > { > >return (PageAModel)session.getAttribute("PageAModel"); > > } > > else > > { > >PageAModel pageAModel = ...build from database. > >session.setAttribute("PageAModel", PageAModel); > >return pageAModel; > > } > > } Then in the Page... @Override > > public void onSubmit() > > { > > PageAModel pageAModel=(PageAModel)session.getAttribute("PageAModel"); > > ...update the database with PageAModel pageAModel = > > (PageAModel)ldm.getObject(); > > ... //removes the model from session? > > session.setAttribute("PageAModel")=null; > > this.setResponsePage(...); > > } I suspect there is a better way to handle this. Can I avoid using an > > LDM, but somehow remove the model from the PageMap after leaving the page? > >
Store models short-term
I'm new to Java and Wicket. My only previous experince with web applications has been with Asp.net forms (not MVC). Please be patient; coming from the postback event paradigm, I'm struggling to grasp the concepts in Wicket. In my simple scenario, assume there is no AJAX. I need to build the model from a database. If I use an LDM, on a postback Wicket calls to the database to rebuild the model before updating it with the new values. But if I don't use an LDM, Wicket will serialize the model in the PageMap. I would like to keep the model 'in memory' long enough to process the postback to avoid unecessary calls to the database, but release it when I have moved on to a different page. I thought of something like this... In the LDM @Override public Object load() { ...somehow get the session. if (session.getAttribute("PageAModel")!=null) { return (PageAModel)session.getAttribute("PageAModel"); } else { PageAModel pageAModel = ...build from database. session.setAttribute("PageAModel", PageAModel); return pageAModel; } } Then in the Page... @Override public void onSubmit() { PageAModel pageAModel=(PageAModel)session.getAttribute("PageAModel"); ...update the database with PageAModel pageAModel = (PageAModel)ldm.getObject(); ... //removes the model from session? session.setAttribute("PageAModel")=null; this.setResponsePage(...); } I suspect there is a better way to handle this. Can I avoid using an LDM, but somehow remove the model from the PageMap after leaving the page?
RE: log4j.properties
Thanks for your help. I included the properties file in a separate jar. > From: toriv...@arrive.no > To: users@wicket.apache.org > Date: Thu, 29 Mar 2012 08:38:57 +0200 > Subject: RE: log4j.properties > > > Additionally you can put log4j.properties in its own jar and put it in > > $tomcat/lib. > > That's the default for Tomcat 6+, but formally the location for shared > classpath entries is defined in the property common.loader in > $tomcat/conf/catalina.properties. So e.g. to "re-establish" the older > structure with common/classes etc. you would edit this entry accordingly. > > - Tor Iver > > B�CB��[��X��ܚX�KK[XZ[�\�\��][��X��ܚX�P�X��]�\X�K�ܙ�B��܈Y][ۘ[��[X[��K[XZ[�\�\��Z[�X��]�\X�K�ܙ�B�
log4j.properties
I'm new to Java, Tomcat, and Wicket. I apologize in advance for asking the obvious. Tomcat logs complain that log4j is not properly configured. I placed my log4j.properties file in the /WEB-INF/classes folder and errors stop. I would like ALL Wicket applications to share a single log4j.properties file so I don't have to remember to include it in every application I deploy. Can I configure Tomcat to accomplish this? If so, how? Using Tomcat 7.026, JDK 7u3, and Wicket 1.5.5 Thanks.