Hi All
I have just started learning about Hibernate so forgive me if I my questions seam stupid in anyway.
I have started by creating a hibernate config file and a mapping file for a domain object Subscription.
hibernate.cfg.xml:
<?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">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.password">test</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/Test</property>
<property name="hibernate.connection.username">test</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping resource="nl/test/virtualcommunity/domain/Subscription.hbm.xml" />
</session-factory>
</hibernate-configuration>
Subscription.hbm.xml:
<class name="SubscriptionType" table="Subscription_Type">
<id name="id" type="int" column="SUBSCRIPTION_TYPE_ID">
<generator class="native" />
</id>
<property name="name" column="NAME" type="string" />
<property name="costs" column="COSTS" type="double" />
<property name="timeToLive" column="TIME_TO_LIVE" type="int" />
</class>
I want to create a DAO to load/save/update information about a subscription. I have created this code:
SubscriptionManagerDAO:
public Subscription findSubscription(int id) throws ResourceNotFoundException {
// Start a hibernate session
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction transaction = null;
Reseller result = null;
try {
transaction = session.beginTransaction();
Query query = session.createQuery("from Subscription s where s.id = :id");
query.setInteger("id", id);
List list = query.list();
if (list != null)
result = (Subscription) list.get(0);
session.getTransaction().commit();
} catch (HibernateException he) {
logger.error(he);
if (!Validator.isEmpty(transaction))
transaction.rollback();
}
if (result == null)
throw new ResourceNotFoundException("Subscription with id '" + id + "' not found");
return result;
}
As you can see I throw a custom ResourceNotFoundException if result is empty but this happens after the try catch. I would like to throw the exception in the try section after the empty check of the query list, like this:
if (list != null)
result = (Subscription) list.get(0);
else
throw new ResourceNotFoundException("Subscription with id '" + id + "' not found");
But then the transaction.commit() wont be called and if I understand Hibernate good enough transaction.commit() must be called to free the jdbc connection.
Has anybody got some tips on how this could be solved or have you got any comments on how the function in the dao is constructed.
Thanks in advance,
Martyn
|