Tuesday, June 5, 2012

Trying again on windows: Hibernate, TomCat, Struts- With NetBeans

Okay, so after last night's show stopper bugs on my nix system, lets give it a shot on Windows. Again I have NetBeans 7.1.2 and for some reason Tomcat is not coming with it... Even though it said it did? Anyway, got the 32 bit Windows version from the website- 6.0.35 so lets give this a shot.

Went to add server-> tomcat -> navigated to where I unpacked Tomcat 6. Let Netbeans create a default user/pass for the manager script (root/test). Okay great. So I'm NOT going to include the super outdated version of Hibernate that comes with Netbeans (3.2.5)  as they are on 4.1 now. And I'm not including Struts 1.3.10 either (no thanks it isn't 1999).

So right away I'm running the build to see if I can get the TomCat "Hello World" up. Great @ http://localhost:8080/srvrTest/ I can see a nice big "Hello World". Sweet. So lets go ahead and get maybe Hibernate 3.5.X going because the documentation looks to be pretty decent. Yeah I'm talking about this tutorial right here. Wait nope, everyone is using '3.6.1 FINAL' so I better use that. I doubt they are too different. They also have almost the exact same tutorial for 3.6... cool.

Alright so the tutorial is telling to use Maven. Idk what this is so that is pretty fucking awesome. Well okay I'll just go to apache.maven.org and see if that helps. Maven in 5 minutes... So I'm getting Maven 3.0.4- hope that is good. Uh actually that looks like a pain in the ass setting that up. I'll hold off. I think Netbeans already uses ant anyway, but I really have no Idea how this works...

So I'm starting up my MySQL server and getting that running because I need something for Hibernate to mess with. Alright so I forgot I disabled my MySQL service because I thought it was making Battlefield run slower. So went to 'services' to fix that. Oh god I forgot I don't have MySQL workbench installed on this computer. There goes another 5 minutes.

So after some delay here is my DB:

usr: root

pass: test

DB name: messaging

table: messages
    messageID  (integer(10), PK, AI)
    message  (varchar(255))
    ip (varchar(20))
    stamp (DATETIME) 
  
So that is done now. So the hibernate tutorial mentions nothing about actually importing the Hibernate Jars into the project. I guess they just want to leave that to the imagination. But skipping the maven section, I'm supposed to make a class that mirrors the table I'm to interact with. Here goes. I made a new package called 'dbclasses' and a class called 'Messages':


package dbclasses;
import java.util.Date;

public class Messages {
    private long messageID;
    private String message;
    private String ip;
    private Date stamp;
    
    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public long getMessageID() {
        return messageID;
    }

    public void setMessageID(long messageID) {
        this.messageID = messageID;
    }

    public Date getStamp() {
        return stamp;
    }

    public void setStamp(Date stamp) {
        this.stamp = stamp;
    }
}
Alright, so now I suppose I should try to import Hibernate and set up their config file. First though, I'm going to the 'Services' tab in Netbeans and connecting to MySQL. So that looks good.

So wow the official Hibernate tutorial is worthless and complicated. This is from netbeans. And this is even better as well. So I went in to 'Create Library' and I added all the required Jars and the JavaDocs to a library called "Hibernate3.6". Then I set up the mappings and config files as follows:


Mappings file: under a the classpath: srvrTest/src/java/hibernatemappings/hibernate.hbm.xml


<hibernate-mapping>
    
    <class name="srvrTest.src.java.dbclasses.Messages" table="Messages">
        <id column="messageID" name="messageID">
            <generator class="native">
        </generator></id>
        <property name="message" />
        <property name="ip" />
        <property column="stamp" name="stamp" type="timestamp" />
    </class>
</hibernate-mapping>


And the config file under: srvrTest/src/java/hibernatemappings/hibernate.cfg.xml


<hibernate-configuration>
  <session-factory>
      
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/messaging</property>
        <property name="connection.username">root</property>
        <property name="connection.password">test</property>

        
        <property name="connection.pool_size">1</property>

        
        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>

        
        <property name="current_session_context_class">thread</property>

        
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        
        <property name="show_sql">true</property>

        
        
        <mapping resource="srvrTest/src/java/hibernatemapping/hibernate.hbm.xml">
  </mapping></session-factory>
</hibernate-configuration>
And for tonight.. I'm done.

No comments:

Post a Comment