Olá pessoal,
Estou fazendo o exemplo do site: http://homepage.mac.com/edahand/projects/java/example1.html de struts com hibernate.
Por alguma razao que nao sei explicar, o hibernate nao salva e nem dá erro. Fui debugando tudo com o netbeans e ele passa por tudo bonitinho, indica sucesso, mas simplesmente nao grava no banco… não sei explicar.
Coloco abaixo algumas partes do codigo que julgo importante pra resolver o problema:
AddItemAction:
package com.edhand.example1;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
* Processes user's request to add an <code>Item</code> to the database.
*
* Copyright 2003 Edward Hand
*
* @author Edward Hand
*
*/
public class AddItemAction extends Action
{
/**
* execute() processes a user's request to add an <code>Item</code> to the database,
* given the data in the AddItemForm. Forwards the user back to the input page.
*
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
{
/*
* Cast generic form to the AddItemForm.
*/
AddItemForm addItemForm = (AddItemForm) form;
/*
* If the user has typed a name into the form,
*
* Create an item with the name and description contained
* in the form.
*
* Using the ItemService, add the item to the persistence
* layer.
*
* Clear the form so that the two fields will be empty
* upon the next load.
*/
if (addItemForm.getName() != null)
{
Item item = new Item();
item.setDescription(addItemForm.getDescription());
item.setName(addItemForm.getName());
item.setId(addItemForm.getId());
ItemService.getInstance().addItem(item);
addItemForm.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");
}
}
ItemService:
/*
* Created on Nov 26, 2003
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.edhand.example1;
import java.util.List;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.ObjectNotFoundException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
/**
* Provides an interface used to access <code>Item</code> objects from the
* database using Hibernate.
*
* Copyright 2003 Edward Hand
*
* @author Edward Hand
*
*/
/*
* The ItemService class uses the Singleton design pattern to provide an interface
* to the Struts application allowing it to work with Item objects through
* the persistence layer.
*
* No Hibernate-related code exists above this layer. This would allow us, in the
* future, to switch to some other method of object persistence without making changes
* to the Struts-related code in the application.
*
*/
public class ItemService
{
private static ItemService instance = null;
private ItemService()
{
}
/**
* getInstance() returns the instance of the <code>ItemService</code> singleton.
*
* @return <code>ItemService</code> singleton.
*/
public static synchronized ItemService getInstance()
{
/*
* Creates the Singleton instance, if needed.
*
*/
if (instance == null)
{
instance = new ItemService();
}
return instance;
}
/**
* getItem() returns <code>Item</code> object from database with given id. If
* the user is not found, will return null.
*
* @param id The <code>Long</code> id of desired <code>Item</code>
* @return <code>Item</code> with given id, if it exists. Otherwise, returns null.
*/
public Item getItem(Long id)
{
/*
* Use the ConnectionFactory to retrieve an open
* Hibernate Session.
*
*/
Session session = ConnectionFactory.getInstance().getSession();
try
{
/*
* Calls the load method on the Hibernate session object
* to retrieve the Item with the provided id.
*/
return (Item) session.load(Item.class, id);
}
/*
* If the object is not found, i.e., no Item exists with the
* requested id, we want the method to return null rather
* than throwing an Exception.
*
*/
catch (ObjectNotFoundException onfe)
{
return null;
}
catch (HibernateException e)
{
/*
* All Hibernate Exceptions are transformed into an unchecked
* RuntimeException. This will have the effect of causing the
* user's request to return an error.
*
*/
System.err.println("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}
/*
* Regardless of whether the above processing resulted in an Exception
* or proceeded normally, we want to close the Hibernate session. When
* closing the session, we must allow for the possibility of a Hibernate
* Exception.
*
*/
finally
{
if (session != null)
{
try
{
session.close();
}
catch (HibernateException e)
{
System.err.println("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}
}
}
}
/**
* updateItem() updates specfied <code>Item</code> through Hibernate.
*
* @param item An <code>Item</code> to be updated
*/
public void updateItem(Item item)
{
/*
* Use the ConnectionFactory to retrieve an open
* Hibernate Session.
*
*/
Session session = ConnectionFactory.getInstance().getSession();
try
{
/*
* Update the state of the item using the Hibernate session's update method.
*
* Call the flush method to ensure that the object in saved.
*
*/
session.update(item);
session.flush();
}
catch (HibernateException e)
{
System.err.println("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}
/*
* Regardless of whether the above processing resulted in an Exception
* or proceeded normally, we want to close the Hibernate session. When
* closing the session, we must allow for the possibility of a Hibernate
* Exception.
*
*/
finally
{
if (session != null)
{
try
{
session.close();
}
catch (HibernateException e)
{
System.err.println("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}
}
}
}
/**
* getItemList() returns list of all <code>Item</code> objects stored in the database.
*
* @return <code>List</code> of <code>Item</code> objects.
*/
public List getItemList()
{
/*
* Use the ConnectionFactory to retrieve an open
* Hibernate Session.
*
*/
Session session = ConnectionFactory.getInstance().getSession();
try
{
/*
* Build HQL (Hibernate Query Language) query to retrieve a list
* of all the items currently stored by Hibernate.
*/
Query query =
session.createQuery(
"select item from com.edhand.example1.Item item order by item.name");
return query.list();
}
catch (HibernateException e)
{
System.err.println("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}
/*
* Regardless of whether the above processing resulted in an Exception
* or proceeded normally, we want to close the Hibernate session. When
* closing the session, we must allow for the possibility of a Hibernate
* Exception.
*
*/
finally
{
if (session != null)
{
try
{
session.close();
}
catch (HibernateException e)
{
System.err.println("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}
}
}
}
/**
* addItem() inserts new <code>Item</code> into the database through Hibernate.
*
* @param item A new <code>Item</code> to be added.
*/
public void addItem(Item item)
{
Session session = ConnectionFactory.getInstance().getSession();
try
{
session.save(item);
session.flush();
}
catch (HibernateException e)
{
System.err.println("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}
/*
* Regardless of whether the above processing resulted in an Exception
* or proceeded normally, we want to close the Hibernate session. When
* closing the session, we must allow for the possibility of a Hibernate
* Exception.
*
*/
finally
{
if (session != null)
{
try
{
session.close();
}
catch (HibernateException e)
{
System.err.println("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}
}
}
}
}
ConnectionFactory:
/*
* Created on Nov 25, 2003
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.edhand.example1;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.MappingException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
/**
* Singleton class that creates and returns an open Hibernate <code>Session</code> to the user.
*
* Copyright 2003 Edward Hand
*
* @author Edward Hand
*
*/
/*
* This class was created to encapsulate the Hibernate SessionFactory and assist
* the service layer, in this example consisting of ItemService.
*
*/
public class ConnectionFactory
{
private static ConnectionFactory instance = null;
private SessionFactory sessionFactory = null;
private ConnectionFactory()
{
// Establish SessionFactory for Hibernate
try
{
/*
* The Hibernate Configuration will contain all the classes to be
* persisted by Hibernate. For each class persisted, Hibernate will
* expect to find a ClassName.hbm.xml file in the same location as the
* class file. This XML file will define the mapping between the Java
* object and the database.
*
* To add additional classes to the configuration, you may cascade the
* method calls thusly:
*
* Configuration cfg = new Configuration().
* addClass(Foo.class).
* addClass(Bar.class);
*
*/
Configuration cfg = new Configuration().addClass(Item.class);
sessionFactory = cfg.buildSessionFactory();
}
catch (MappingException e)
{
/*
* Upon encountering a Hibernate generated Exception, we are throwing
* an unchecked RuntimeExcpetion that will cause the user's
* request to fail.
*
*/
System.err.println("Mapping Exception" + e.getMessage());
throw new RuntimeException(e);
}
catch (HibernateException e)
{
/*
* Upon encountering a Hibernate generated Exception, we are throwing
* an unchecked RuntimeExcpetion that will cause the user's request to fail.
*
*/
System.err.println("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}
}
/**
* getInstance() returns the instance of the ConnectionFactory singleton.
*
* Example call to retrieve session:
*
* <pre>
* Session session = ConnectionFactory.getInstance().getSession();
* </pre>
*
* @return Instance of the <code>ConnectionFactory</code> singleton.
*/
public static synchronized ConnectionFactory getInstance()
{
/*
* If the instance of the Singleton has not been created, create and
* return.
*/
if (instance == null)
{
instance = new ConnectionFactory();
}
return instance;
}
/**
* getSession() returns a Hibernate <code>Session</code>
*
* @return <code>Session</code> retrieved from Hibernate <Code>SessionFactory</code>
*/
public Session getSession()
{
try
{
/*
* Use the Hibernate Session Factory to return an open session to the caller.
*/
Session s = sessionFactory.openSession();
return s;
}
catch (HibernateException e)
{
/*
* Upon encountering a Hibernate generated Exception, we are throwing
* an unchecked RuntimeExcpetion that will cause the user's request to fail.
*
*/
System.err.println("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}
}
}
Item.hbm.xml:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.edhand.example1.Item" table="item" >
<id name="id" column="id" type="java.lang.Long">
<generator class="assigned"/>
</id>
<property name="name" column="name" type="java.lang.String" />
<property name="description" column="description" type="java.lang.String" />
</class>
</hibernate-mapping>
O que fiz de diferente do exemplo foi apenas mudar o banco pro Firebird e mudar o id pra assigned, pra jogar o valor dele também.
Alguem já passou por problema semelhante e poderia me ajudar?