Thursday, February 2, 2012

Clicking on a display table row

I have been working on displaying data from the database with the action triggered from a displaytag row,  and now it works. I use the following steps, the code fragments might give brief info
  1. get the row id in the jsp page that contains displaytage result 
  <display:column title="View"  property="dvdid" sortable="true"  href="detailaction.do" paramId="id" paramProperty="dvdid" ><s:hidden name="list[%{#attr.row_rowNum - 1}].dvdid" value="%{#attr.row.dvdid}"/></display:column>
      
  1. get access from the struts action 
 Query result = session.createQuery("from Dvdstbl where dvdid="+request.getParameter("id"));
  1. display the result wherever you intend to.

Saturday, January 28, 2012

Solution to inserting record into the database

I tried to do it based on the Insert into ... Select standard query of HQL. This is only applicable to copy data from one object and add the content into another object. I could not proceed, just because i was trying to insert new records from a jsp page. The easier way to do it was ....
  • from the jsp page populate the data to the corresponding struts form class
  • do the persistence layer on to the entity generated by the hibernate 
  • using the struts corresponding action class save the form data in to the entity 
  • save the entity and execute the transaction after getting the session factory
look the struts action fragment code

data.setUsex(formObj.getUsex());

        try {
            SessionFactory _factory = (SessionFactory) context.getAttribute(HibernatePlugIn.SESSION_FACTORY_KEY);
            /*Open Hibernate Session */
            sess = _factory.openSession();
            sess.beginTransaction();
            sess.save(data);
            sess.getTransaction().commit();
        } catch (Exception e) {
            sess.getTransaction().rollback();
        }finally{
           
            sess.flush();
            sess.close();

Friday, January 27, 2012

Two major problems ...

Solving the following two problems gave me a hard time .... and not yet solved
  1. Inserting new records into the database table
  2. Manipulating the rows of the table displayed using displaytag 

Hibernate struts problems encountered in the development?

  • Inserting new record to a database ... HQL (INSERT INTO .....SELECT ....)

javax.servlet.ServletException: org.hibernate.QueryException: query must begin with SELECT or FROM: 
INSERT [INSERT INTO Usertbl (uemail,upword,upwordQue,upwordAns,ufirstName,ulastName,udob,uaddress,uname,usex)
SELECT abeshaz@gmail.com','123','what','why','zelalem','Mihret',1680-06-25,'addis','zolla','male from UserForm ]
 org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:520)
 org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:427)
 org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
 org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
 org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

    Tuesday, January 3, 2012

    Hibernate ..

    Today, I manage to integrate and use Hibernate with my project. It took me about one hour and half to read and internalize the how of hibernate browsing the internet with the help of the lecture note.