I have been trying to add a persistence layer on a JSF web app with little luck. I am using Eclipse with Exadel Studio Pro.
For the purpose of learning hibernate, I have created a very simple sample project.
This project has one java bean (ProjectManager) that controls a class (Project) with four properties; id, name, client, location.
The first page (index2.jsp) has a button which creates a new instance of the Project class, which redirects to another page (index3.jsp) with inputText boxes to enter the values. Clicking the set button on this page redirects to another page (index4.jsp) which uses outputText to show the values of the attributes of the Project class. There is a button there that is to persist this instance by calling a method in ProjectManager which saves the Project object.
Below is the relevent code for the files:
the JSP pages are self explanatory, they accomplish everything I said above with inputText, outputText and commandButton tags.
Project.java contains:
Code:
Integer id; // Primary key
String name, client, location;
as well as the proper getters/setters.
ProjectManager.java:Code:
package beans;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class ProjectManager {
private Project projObj;
public void setProjObj(Project _projObj) {
this.projObj = _projObj;
}
public Project getProjObj() {
return projObj;
}
public ProjectManager() {}
public String newProject() {
projObj = new Project();
return "success";
}
public String saveProject() throws Exception {
// Create SessionFactory object
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
// Open Session
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
// Save Project and close Session
session.saveOrUpdate(projObj);
session.close();
return "success";
}
}
hibernate.cfg.xml:Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate10</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="beans/Project.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Project.hbm.xml:Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping schema="hibernate10" package="beans">
<class name="Project" table="project" optimistic-lock="none">
<id name="id" type="integer" unsaved-value="null" column="id">
<generator class="increment"/>
</id>
<property name="name" type="string" column="name"/>
<property name="client" type="string" column="client"/>
<property name="location" type="string" column="location"/>
</class>
</hibernate-mapping>
I made the project table with the following SQL statement:
CREATE TABLE project ( id INT(255) NOT NULL AUTO_INCREMENT , name varchar(255), client varchar(255), location varchar(255), primary key (id) )
I also tried:
CREATE TABLE project ( id INT(255) NOT NULL DEFAULT '0' , name varchar(255), client varchar(255), location varchar(255), primary key (id) )
I'm hoping there's an obvious little error in all of this, if someone can point it out. Perhaps my SQL statements are making tables that aren't appropriate to my task? Also, I'm not sure if my configuration files are missing a tag or have a property improperly defined. What frustrates me most is that there are no exceptions being thrown, and the save method executes to completion, but data is still not being written to the database.
Thank you all for your time, any help would be greatly appreciated!