-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Did not save to database (caused table lock I think)
PostPosted: Sat Jan 24, 2004 11:10 am 
Newbie

Joined: Tue Jan 06, 2004 12:57 am
Posts: 14
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);
}
}

}


----------------------------------------------------------------------------------


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 24, 2004 11:16 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Please wrap your code with hibernate transactions.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 25, 2004 5:24 pm 
Regular
Regular

Joined: Fri Sep 05, 2003 12:01 am
Posts: 80
Location: Bogot
this has been already discussed.

Take a look at :

http://forums.hibernate.org/viewtopic.php?t=925354&highlight=&sid=856a924738ee4ee30a4130b3bd4feff1

_________________
Mauricio Hern


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.