Wednesday, June 6, 2012

Wait I don't know JSP yet. And getting Hibernate working.

So here is a thought: I have actually never used JSP. At all. Turns out it is just like PHP:
you open up tags like so: <% Java code goes here %>

Import statements are special:
<%@page import="java.util.Date"%>

And they go up at the top. And you can just use out.println() to render stuff to the page.

Anyway, lets make a Hibernate session factory util: I'm just using the Netbeans defualt Hibernate util by going to new->other->hibernate->HibernateUtil.java which gives me this:


package hibernatemapping;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class NewHibernateUtil {

    private static final SessionFactory sessionFactory;
    
    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
    
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

I quickly went into my index.jsp page and tried it out:


            <%
            String derp = "Derp derp";
            MyMessages m = new MyMessages();
            // set message to a string
            m.setMessage(derp);
            // set ip to the request object for it
            m.setIp(request.getRemoteAddr());
            
            //pass some new date:
            Date d = new Date();
            m.setStamp(d);
            
            String mymsg = "message:" + m.getMessage() + " IP:" + m.getIp() + " Date:" + m.getStamp().toString();
            out.println(mymsg);
            
            /*
            *  NOW LETS GIVE HIBERNATE A TRY:
            */
            // this gives null pointer exception
               Session sesh = NewHibernateUtil.getSessionFactory().getCurrentSession();
               sesh.beginTransaction();
               sesh.save(m);
               sesh.getTransaction().commit();
            %>

After I went into MySQL workbench I checked my previously virgin 'Messages' table and saw that it had one entry in it after reloading index.jsp. So far so good. Now, one more thing before bed: I want to pull out a message:

               Session sesh2 = NewHibernateUtil.getSessionFactory().getCurrentSession();
               sesh2.beginTransaction();
               long id = 1; // yeah it has to be a long apparently
               // You typecase the result as an object you want it to be basically:
               msg = (MyMessages) sesh2.get(MyMessages.class, id);
               
               out.println("Here is a row from the db");
               out.println("Message: " + msg.getMessage() + "Time:" + msg.getStamp().toString());
              

So tomorrow I suppose I will try to pull out rows and iterate over them.

No comments:

Post a Comment