I have been trying to get a sample working and its almost there except the data is not getting saved to the database (retrieval is ok).
I am using SQL Server and the Microsoft jdbc driver. There is no error from Hibernate on the console:
Hibernate: insert into item (name, description) values (?, ?)
Hibernate: select @@identity
Hibernate: select item0_.id as id, item0_.name as name, item0_.description as de
script3_ from item item0_ order by item0_.name
After the AddItem() method is called, a call to getItemList() shows the Item that I was trying to save. However the data is not in the database and if I select * from Item, SQL Server hangs when trying to view the data in the Item table, until I shutdown Tomcat. So there must be a lock on the Item table?
Pls help..
Jeff.
----------------------------------------------------------------------------------
/**
* 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);
}
}
}
}
----------------------------------------------------------------------------------
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);
}
}
}
----------------------------------------------------------------------------------
|