facebook

openSession() method // update functionality

  1. MyEclipse IDE
  2.  > 
  3. Off Topic
Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #220352 Reply

    Kevin_L
    Member

    Hi,
    I am using the the latest 3.8.2 MyEclipse with Eclipse 3.0, JVM 1.4.2.
    OS: WinXP, Tomcat 5.0, MyEclipse 4.1

    First of all, I appreciate your support and help. With tutorials from your site and helps from other java programmers from open source community, I am learning a lot about J2EE development….well I am stuck in implementing update functionality with hibernate and I don’t know whoelse I should turn to..Hopefully someone would help me and direct me into the correct path…

    I am mainly using MyEclipse with Hibernate funcationality to create contact managment application. Because I am new to the hibernate, I am stuck in update functionality.

    Well, I would like to know why I cannot do this. Is it because of the way MyEclipse is configured for hibernate… I should be able to do this..

    Contact contact = new Contact();
    //….update the contact from ContactForm data
    Session session = SessionFactory.openSession();
    session.update(contact);

    But MyEclipse is only allowing me to choose currentsession() method… Am I missing something..???

    this is what I have now..I know I should not use currentSession()….my application doesn’t spit out the hibernate exception errors but it goes back to listContact page with no update effect…

    By the way, I have updateContact.java that extend the Structs Action and ActionForward… it first get the contactId from parameter and store it as String..If localContactId is not null, I create new Contact object and store all the data from ContactForm.. and then pass the object to ContactService.java which has updateContact method. Inside that method , I have this codes…

    session = SessionFactory.currentSession();
    /*
    * Update the state of the item using the Hibernate session’s update method.
    *
    * Call the flush method to ensure that the object in saved.
    *
    */

    Transaction transaction = session.beginTransaction();
    session.update(data);
    transaction.commit();
    session.close();
    session.flush();

    THis is what I have for ActionForward

    public ActionForward execute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response) {
    ContactForm UpdateContactForm = (ContactForm) form;

    //Get the ContactId first
    String localContactId = request.getParameter(“contactId”);

    //If ContactId is not null, update the data
    if (localContactId != null)
    {
    Contact contact = new Contact();
    contact.setContactNameSalutation(UpdateContactForm.getContactNameSalutation());
    contact.setContactNameFirst(UpdateContactForm.getContactNameFirst());
    contact.setContactMi(UpdateContactForm.getContactMi());
    contact.setContactNameLast(UpdateContactForm.getContactNameLast());
    contact.setContactAddress1(UpdateContactForm.getContactAddress1());
    contact.setContactAddress2(UpdateContactForm.getContactAddress2());
    contact.setContactAddress3(UpdateContactForm.getContactAddress3());
    contact.setContactCity(UpdateContactForm.getContactCity());
    contact.setContactStateProvince(UpdateContactForm.getContactStateProvince());
    contact.setContactPostalCode(UpdateContactForm.getContactPostalCode());
    contact.setContactCountry(UpdateContactForm.getContactCountry());
    contact.setContactEmail(UpdateContactForm.getContactEmail());
    contact.setContactPhone(UpdateContactForm.getContactPhone());
    contact.setContactFax(UpdateContactForm.getContactFax());
    contact.setContactRole(UpdateContactForm.getContactRole());
    contact.setContactOrganizationType(UpdateContactForm.getContactOrganizationType());

    ContactService.getInstance().updateContact(contact);
    UpdateContactForm.clear();
    }
    /*
    * Always return to the “success” forward, currently
    * configured in struts-config.xml to return the user
    * to the AddItem.jsp.
    */
    return mapping.findForward(“success”);

    #220356 Reply

    Riyad Kalla
    Member

    Moving to OT > Soft Dev, this is a hib question.

    Additionally, I don’t see you setting your PK value on the Contact object anywhere… how is Hibernate going to know which record to update?

    #220359 Reply

    Kevin_L
    Member

    As I mentioned earlier, I am new to hibernate. Well that is good question.. I though hibernate will automatically detect whether the object is already existed or not…

    This following quote from introduction to hibernate.
    http://www.systemmobile.com/articles/IntroductionToHibernate.html

    It is a good article but it doesn’t paint the picture completely. Will someone tell me how I can code so that hibernate know which record to update…
    currently, I am using structs to pass the contactId value to go to updateContact.do page which display the corresponding data to that specific contactId.

    Well, this is what I learned so far..

    The saveOrUpdate(Object) call will save the object if the id property is null, issuing a SQL INSERT to the database. This refers to the unsaved-value attribute that we defined in the Player mapping document. If the id is not null, the saveOrUpdate(Object) call would issue an update, and a SQL UPDATE would be issued to the database. (Please refer to the sidebar Unsaved-Value Strategies for more information on this topic.)

    #220360 Reply

    Kevin_L
    Member

    contactID is PK and it is passing as a parameter from listContact.do to updateContact.do…

    Anyhow, this might look silly and easy for other people.. I am totally new to this hibernate thing… I was developing web application by writing tedious JDBC codes and java beans… this is totally different way from what I knew… In any case, hibernate is cool!

    #220361 Reply

    Riyad Kalla
    Member

    As I mentioned earlier, I am new to hibernate.

    I would highly suggest Hibernate In Action from Manning… its written by the authors of Hibernate and takes a very smooth transition from “What is hibernate” to end with “How do I build an enterprise application with it?”. It fills in a lot of holes.

    Will someone tell me how I can code so that hibernate know which record to update…

    Every table you map will have some form of primary key for the rows, in our mapping file you declare an <id> field or <composition-id> to uniquely identify records. When you want to create an object and have Hibernate update it, what you need to do is make sure you set the ID value of your object so when Hibernate does its JDBC it knows which record to update. Think of Hibernate as an abstration to JDBC code… if I gave you the above you had above in your code, and said “ok update the record”, you’d have no idea which record you needed to update… neither does Hibernate.

    This is the gist, but of course things are never as easy as they seem. I found with hibernate there is this sort of startup/confusion phase that once you get through you start kicking ass with it… but until you get past the confusion, its this jumble of confusing what is required and what magic hibernate performs… again one of the reasons I suggest that book. (ebook is like $22.50)

    #220362 Reply

    Riyad Kalla
    Member

    contactID is PK and it is passing as a parameter from listContact.do to updateContact.do…

    Right, what you need to do is add a line right after you create the Contact object that does something like:

    
    contact.setContactID(contactID);
    

    now when you go to update() that object, Hibernate will know which one it is in the database.

    #220383 Reply

    Kevin_L
    Member

    Thanks again Riyad….I think I understand now… I will get my hand on that book that you suggested… so far I am going by its official documentation and tutorial…I do like Manning books…I am using Struts in Action and it sure does help me a lot…

    Well..one more question…

    I can’t do
    contact.setContactId(localContactId);
    because setContactId metho wants Integer and localContactId is int.. any idea how to change int to Integer..
    so far I have this code to change string to int… i

    int localContactId = Integer.parseInt(request.getParameter(“contactId”));

    Let’s say…we want to save localContactId as Integer so that contact.setContactId(localContactId) will work…. I did
    Integer localContactId = Integer.parseInt(request.getParameter(“contactId”));

    but then again..this is mistype…
    so I did this return Integer , instead of parsing into int type
    Integer localContactId = Integer.valeuof(request.getParameter(“contactId”));

    Well..MyEclipse took it with no errors..

    but when I deploy the application to tomcat and try to update it,
    it spits out the
    java.lang.NumberFormatException: null
    java.lang.Integer.parseInt(Integer.java:436)
    java.lang.Integer.parseInt(Integer.java:518)

    So I am back to square one…
    I know I am type casting and parsing type the correct way…but do yo have any idea what I am doing wrong…Obviously, something is wrong with the codes…but they all seem right…MyEclipse/Eclipse took it without errors..

    #220393 Reply

    Riyad Kalla
    Member

    Note the exception, that is the key. Its the parseInt method telling you that it can’t parse a “null” value, so for some reason the “request.getParameter(“contactId”)” piece of code is returning null, so when you call parseInt or valueOf on it, the method is dying with a NumberFormatException. Do some more line-by-line debugging to see what is going on (stick in print outs, break points, etc.), make sure you typed the parameter name correctly in the address bar of your browser or that your test URL is correct.

    You are on the right track, if you have an int and need an Integer, no problem:
    contact.setContactID(new Integer(contactID));

    just wrap it up. In JDK 5.0 this is done for you via ‘autoboxing/unboxing’ but it is simply abstracting out the code that you need to write for it anyway (it performs it for you, no special optimizations).

    #220396 Reply

    Kevin_L
    Member

    Hi Riyad,

    There is not a word to describe how much I am grateful for your help. FYI, updateContact is working now… :):):)
    Your explaination and interpretion of error message has made me realized that I should just use contactId from original bean objects instead of using parameter values, obivously, as you mentioned, updateContact.java is getting null value… I should have just used parameter values to get the updateContact.do from listContact.do…
    From updateContact.do to do real update, parameter values that was received from listContact.do is useless… I didn’t realize that until now… thanks to you… 🙂

    well cut to the short, I decided to get the original contactId value and change it to Interger as you mentioned.
    It wouldn’t work if you didn’t tell me how I can change the int to Integer.
    You are the man….

    Thank you very much!
    K~

    #220397 Reply

    Riyad Kalla
    Member

    Kevin I’m really glad to hear everything is working now, I know the feeling when you click the link for the 1000s time, except this time, IT WORKED!

Viewing 10 posts - 1 through 10 (of 10 total)
Reply To: openSession() method // update functionality

You must be logged in to post in the forum log in