• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Problems using merge method to update DB record

 
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

For you to be getting what you are getting, the countryList object must evidently have already been initialized. You should have declared it thus:

private void ListDataModel<Country> countryList = null;



using the above statement results in a compile error. Using:



Works fine. My backing bean now looks like this but I'm still getting a blank page


 
Saloon Keeper
Posts: 28140
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about the extraneous (and invalid) "void" in my DataModel declaration. Don't know how that happened.

Your backingbean's "findAll()" method does a DAO find, but discards the result, so it doesn't do anything other than navigate to the "AllCountry" page.

I recommend that you code "makeCountryList" exactly as I typed it. My version will display whether or not the findCountry returned an empty list or not. That way you will have proof as to whether there's really no data to display or if there's data but it isn't displaying. To "know" something it to risk error. To have actual log data is to be certain.
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I rewrote the application from scratch, cleaned up any erronoue code and flushed the GlassFish server instance. Probably didn't need to but though I'd be on the safe side. Still getting a blank page but I discovered the possible cause. I get a blank page when I try to input new countries. When checking in the DB, all of my records are succesfully added. If I restart the server (therefore restart the session), I get the values entered in my prior session and the list is correctly populated. My guess is that SessionScoped maintains the state of the current session so the list is not updated until the subsequent session. I think I need to do 2 things to get the create function working properly under SessionScoped:

1) Refresh / update the list after creating a new record. I tried using setWrapped data which doesn't seem to work. Will destroying and re-creating the ListDataModel work? How can I achieve this?
2) Clear/ flush the prior record in the input text box for creating a new Record. I don't think this has any serious impact on the application. It's just messy and inconvenient. How can I clear or refresh the input box so that it displays a blank value for user input?
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Tim,

I got your last update while I was writing mine. You typed several things and I just want to make sure what you want me to replicate. This is how you typed makeCountryList:




Alternatively I tried:



and got:

INFO: Get Country List - START
INFO: There are 0 rows in cList
INFO: ListDataModel: javax.faces.model.ListDataModel@9b587f5
INFO: There are 0 rows in cList
INFO: ListDataModel: javax.faces.model.ListDataModel@40d4c76b
INFO: makeCountrList output: javax.faces.model.ListDataModel@40d4c76b
INFO: Country List is: : javax.faces.model.ListDataModel@9b587f5

Please let me know if thi ok as it seems to suggest that the cList is not being populated?

 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like i managed to resolve this. The solution seems to be in two areas below:



So the resetModel() re-initializes the ListDataModel. I might add that I took out the setWrappedData method in the ListDatModel getter methods since it didn't look like it was doing much. The return string of the createCountry method references the findAllCountry(0 method explicity where before I had it returning the jsf page string ("AllCountry"). Obviously that was wrong because it doesn't use the resetModel(0 method embedded in the findAllCouuntry() method, which 'resets' and allows the state of the country list to be updated.

I also checked the prepareUpdate and updateCountry methods:



The records are updateing succesfully.

It seems that I've resolved all the main issues, but i'm still concerned about scoping. We discussed in the eary part of this post to use SessionScoped as the lowest common denominator. So I propose to have the whole country entity referring to a single backing bean.

Considering your explanantioons about scoping, would it be viable to continue using SessionScoped for the lifecycle of t8he Country entity (ie: throughout every CRUD operation) or should I look at an alternative scoping.

I'd like to get your views and thanks a million for all your help and patience!
 
Tim Holloway
Saloon Keeper
Posts: 28140
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great! Sounds like you're getting the concepts now.

I myself have no option but to use Session Scope at the moment, since my major apps are based on RichFaces 3 and moving to RichFaces 4 is going to be about as much work as people had when VB.Net replaced VB 6. The people who pay me cannot spare the time or money for that at the moment. RichFaces 3 gives me some of the more useful JSF2 features, but not all of them. I could use the JSF2 Custom scope, I think, but that particular feature is very crude at the moment.

You have basically 2 options with Session Scope. One is to do nothing and let the bean remain in the session. If you do this, you'll need to re-initialize it at the beginning of each CRUD workflow you do.

The other is to make the bean delete itself at the end of each workflow. It's a good idea to also re-initialize at the beginning of the next workflow, in case the deletion doesn't get done, but by deleting the backing bean, you save session resources. I have a utility class called "JSFUtils" where I put JSF-specific utility functions in order to keep my regular backing beans as POJO-like as possible (and make them easier to test offline). One method I put in there is a "bean remover" that obtains the HttpSession object from the FacesContext and removes the bean whose name is passed to the method from the HttpSession.

Conversation Scope is doing basically the same thing, except that the mechanism is part of the newer CDI standard. As part of the re-initialize method, you would begin a conversation and when you navigate out of the workflow, the action method that leaves the workflow would end the conversation.
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes things make a lot more sense largely thanks to you. I'm curious why you would say that you have 'no choice', but to use SessionScoped? At first glance I thought SessionScoped is best suited for 'stateful' type applications, such as authentication? Anyhow I'd be interested to know how Richface compells you to use SessionScoped.

Assuming I stick with this scope, I think the option of 'reseting' the model throughout the CRUD is simple and practical enough. However I also want to look at JsfUtils classes. I'm guessing you have those in your sandbox, right?
 
Tim Holloway
Saloon Keeper
Posts: 28140
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Richfaces 3 allows some of the JSF2 functions to work, but it was not designed for JSF2, it was designed for JSF1. So some JSF2 features leak into my projects, but not because RichFaces supports them, but rather despite RichFaces. Some features I simply cannot take advantage of. For example the new tags such as f:ajax, even though RichFaces 4 removes the older RichFaces 3 ajax support tags.

Red Hat's "solution" for that is to convert to RichFaces 4, which was explicitly intended to support JSF2 (and Internet Explorer versions 9 and later). However, in contrast to the usual backwards compatibility that is a hallmark of Java, they expect you to rework signficant parts of your RF3 webapps. Or pay their consultants to do so for you. In other words, more of a "Microsoft" solution than a "Java" solution.
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I"m marking this issue as 'Resolved', before I close out I just want to know how SessionScoped resolves this annoying 'forward compatibility'. Do you mean that the pages frequently go 'out of scope' and you get erroneous null values unless you explicitly use SessionSceoped?
 
Tim Holloway
Saloon Keeper
Posts: 28140
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every scope has rules that determine when an object goes out of scope. When an object goes out of scope, the EL resolver won't find it anymore and will feel obliged to create a new instance if a later EL expression needs one. That new instance will not carry any state from the older instance, which it knows nothing about, so the only initialized values in the new instance will have been set by the object's constructor, ManagedProperty and post-construct services.

An ideal scope will drop the object as soon as you no longer need it. saving system resources (since it will presumanbly get garbage-collected). A scope whose lifespan is too long will be less efficient because redundant instances will be hanging around idle, but a scope whose lifespan is too short will suffer "data droputs" because the instance will have been deleted in the middle of a workflow and replaced with one whose state values are not reflecting any changes done in earlier stages of the workflow.

Session Scope eliminates the "lost state" problem, since it's the longest scope that is available on a per-user basis. So once you have everything working in session scope, you can tune the object's scope to whatever is most suitable for the workflows that will be using the object.
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once again thanks a million. This issue is definitely resolved and I have much stronger understanding of the scopes.
 
Do not set lab on fire. Or this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic