The Data Access Objects for JVending are located in the provisioning-dao package. For an overview of Data Access Objects see: Wikipedia
The DAOs within 2.0.0-alpha2 are designed to be used at the provisioning framework level.
You may, however, have custom DAOs to which you need access from the application layer. To access the
HibernateDAORegistry
, implement an HttpServlet
and get the attribute
org.jvending.registry.hibernate.HibernateDAORegistry
from the ServletContext
.
Then invoke the find
method for the DAO that you are looking for.
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { HibernateDAORegistry hibernateRegistry = (HibernateDAORegistry) this.getServletContext() .getAttribute("org.jvending.registry.hibernate.HibernateDAORegistry"); ContentAccessorDAO contentDAO = (ContentAccessorDAO) hibernateRegistry.find("dao:content-accessor"); }
Before going into how to create a new DAO, let's cover the configuration. Within the registry-config.xml file, you will see the following entry
<repository> <repository-name>connections</repository-name> <repository-class> org.jvending.registry.hibernate.impl.Hibernate3RepositoryImpl </repository-class> <repository-config>/WEB-INF/jvending-config/connections-hsqldb.xml</repository-config> <init-param> <param-name>dao:bundle-descriptor</param-name> <param-value>org.jvending.provisioning.dao.impl.BundleDescriptorDAOImpl</param-value> </init-param> <init-param> <param-name>dao:client-bundle</param-name> <param-value>org.jvending.provisioning.dao.impl.ClientBundleDAOImpl</param-value> </init-param> <init-param> <param-name>dao:fulfillment-task</param-name> <param-value>org.jvending.provisioning.dao.impl.FulfillmentTaskDAOImpl</param-value> </init-param> </repository>
The database connection and configuration is already set up for you. To add a new dao, put an
additional init-param within the repository tag. The param-name must
begin with dao:
followed by the name of the dao. The param-value gives the class name
of your DAO instance.
<repository> <repository-name>connections</repository-name> <repository-class> org.jvending.registry.hibernate.impl.Hibernate3RepositoryImpl </repository-class> <repository-config>/WEB-INF/jvending-config/connections-hsqldb.xml</repository-config> <init-param> <param-name>dao:bundle-descriptor</param-name> <param-value>org.jvending.provisioning.dao.impl.BundleDescriptorDAOImpl</param-value> </init-param> <init-param> <param-name>dao:client-bundle</param-name> <param-value>org.jvending.provisioning.dao.impl.ClientBundleDAOImpl</param-value> </init-param> <init-param> <param-name>dao:fulfillment-task</param-name> <param-value>org.jvending.provisioning.dao.impl.FulfillmentTaskDAOImpl</param-value> </init-param> <init-param> <param-name>dao:my-fancy-dao</param-name> <param-value>org.domainname.app.FancyDAO</param-value> </init-param> </repository>
That's it for configuration. You can now access it as shown in the "Working with DAOs"
section. Make sure that you cast the class to FancyDAO
before using it.
For more information on configuration, see
Registry-J2SE - How to Use
To create a DAO, you will need to extend the org.jvending.provisioning.dao.BaseProvisioningDAO
abstract class. You will need a constructor that consists of a className
and id
.
When JVending initializes the application, it will pass in the className=org.domainname.app.FancyDAO
and id=dao:my-fancy-dao. So your first stab at a valid FancyDAO class, would look something like:
public class FancyDAO extends BaseProvisioningDAO { public FancyDAO(String className, String id) { super(className, id); } }
This class doesn't do anything, so now let's add some functionality by putting in the following method which will save our fancy objects:
public void store(FancyObject myFancyObject) throws IOException { Transaction transaction = null; Session session = null; try { session = getSessionFactoryByName("hibernate3").openSession(); transaction = session.beginTransaction(); session.save(myFancyObject); transaction.commit(); } catch (Exception e) { try { if (transaction != null) transaction.rollback(); } catch (HibernateException he) { logger.log(Level.INFO, "Failed to roll back transaction", he); } throw new IOException(); } finally { try { if (session != null) session.close(); } catch (HibernateException e) { logger.log(Level.INFO, "Failed to close session", e); } } }
Notice the BaseProvisioningDAO.getSessionByName
method. This class will locate and
open a session for the hibernate3
datasource, which is the preconfigured one for JVending
and is the datasource given in the
connections-hsqldb.xml
configuration file. Now you can access your DAO with all the rest of
the JVending DAOs.
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String fancyObjectId = request.getParameter("fancy-id"); HibernateDAORegistry hibernateRegistry = (HibernateDAORegistry) this.getServletContext().getAttribute("org.jvending.registry.hibernate.HibernateDAORegistry"); FancyDAO fancyDAO = (FancyDAO) hibernateRegistry.find("dao:my-fancy-dao"); FancyObject fancyObject = new FancyObject(); fancyObject.setFancyId(fancyObjectId); fancyDAO.store(fancyObject); }
You can also set up your own database and database connections (separate from the one JVending uses)
and reference them through the HibernateDAORegistry
. On how to do this, see
Registry-J2SE.