From: Craig G. <cg...@ma...> - 2002-09-25 13:45:00
|
Hi Gavin: The demands of the project I'm working on are such that the persistence layer will provide support services for (1) a web site that communicates with an ejb server via stateless session beans wrapping a DAO and (2) a stand-alone application that directly accesses the same DAO without the session wrapper. Databases will be separate, but the data model needs to be available in both places. Because of this dual identity, I need to be able to configure Hibernate differently depending on environment. The way I understand it, in my DAO, I'd be better off using a SessionFactory that has been preconfigured and bound to JNDI via hibernate.cfg.xml. No problem there as you've adequately allowed for this via named session factories. In my stand-alone application, I'd like to be able to do the same, but there's no JNDI around so I wonder if my only option is to use datastore and properties files for configuration? Looking at the code it seems like named session factories ought to be possible (and useful) without JNDI. If JDNI names were instead configured using a property (connection.jndiname?) then it seems that one could specify named factories in the config, keep all config info in the xml file and obtain the correct factory either by querying SessionFactoryObjectFactory or going against JNDI. What I was hoping was that I'd be able to do something like this: hibernate.cfg.xml ===================== <hibernate-configuration> <session-factory name="localfactory"> <property name="connection.driver_class">org.hsqldb.jdbcDriver</property> <property name="connection.url">jdbc:hsqldb:c:/data/hsql/test</property> <property name="connection.username">sa</property> <property name="dialect">cirrus.hibernate.sql.HSQLDialect</property> <!-- mapping files --> <mapping resource="example.hbm.xml"/> </session-factory> <session-factory name="jndifactory"> <property name="jndiname">/env/daofactory</property> <property name="jta.UserTransaction"> java:comp/UserTransaction/ </property> <property name="connection.datasource">/ejbds/</property> <property name="dialect">cirrus.hibernate.sql.HSQLDialect</property> <!-- mapping files --> <mapping resource="example.hbm.xml"/> </session-factory> </hibernate-configuration> This would allow me to set up my DAO more or less as follows. // Abstract inherited by DAO subclasses public BaseHibernateDAO(String config) throws DAOException { protected SessionFactory factory; try { new Configuration(config).configuration.configure(); } catch (HibernateException e) { throw new DAOException(e); } } // Java beans would acquire factory via SessionFactoryObjectFactory public HibernateDAO(String config) extends BaseHibernateDAO throws DAOException { super(config); factory = SessionFactoryObjectFactory.getInstance("localfactory"); } // EJB session beans would acquire factory via JNDI public EJBHibernateDAO(String config) extends BaseDAO throws DAOException { super(config); try { factory = (SessionFactory) new InitialContext().lookup("/env/daofactory"); } catch (NamingException e) { throw new DAOException(e); } } Oh yeah, it'd also be cool if there were a way to share mapping resources with multiple session factories more or less the way you do with properties. I don't think the DTD allows this right now, so in the example I gave above I would have to duplicate all mapping resource declarations despite the fact that both factories use the same set of resources. Forgive me if I've got anything wrong here -- I'm still quite new to Hibernate and this is coming off the top of my head. Maybe there are ways to do this already and I'm simply not seeing them. |
From: Gavin K. <ga...@ap...> - 2002-09-25 14:35:46
|
Why don't you just grab Sun's FSContext JNDI provider, as other people have been using. Its very easy to use. Alternatively, you could write yourself an InMemoryContext JNDI provider (i've done it before its quite trivial). The advantage of this approach is your code would be portable to a J2EE environment if/when necessary. > Oh yeah, it'd also be cool if there were a way to share mapping resources > with multiple session factories more or less the way you do with properties. If you wanna hack away at cirrus.hibernate.cfg.Configuration and implement this I'm happy to accept this change into the codebase. P.S.To make the cfg XML format a bit more typesafe, I have toyed with allowing properties to be configured like: <connection> <url>jdbc:db2:test</url> <driver-class>db2.Driver</driver-class> <username>foo</username> <password>bar</password> </connection> <jndi> <class>sun.FSInitialContextProvider</class> <url>C:\jndi</url> </jndi> etc, instead of <property name="hibernate.connection.url">jdbc:db2:test</property> ..... If anyone wants to take this on, I'd appreciate it, since I have some more immediate issues ATM. You should be able to implement this in a clever way, so that Configuration doesn't need to know exactly what properties there are; just how to map from an element path to a property name. ----- Original Message ----- From: "Craig Goss" <cg...@ma...> To: "Hibernate Mailing List" <hib...@li...> Sent: Wednesday, September 25, 2002 11:44 PM Subject: [Hibernate] Named SessionFactory in cfg.xml > Hi Gavin: > > The demands of the project I'm working on are such that the persistence > layer will provide support services for (1) a web site that communicates > with an ejb server via stateless session beans wrapping a DAO and (2) a > stand-alone application that directly accesses the same DAO without the > session wrapper. Databases will be separate, but the data model needs to be > available in both places. Because of this dual identity, I need to be able > to configure Hibernate differently depending on environment. > > The way I understand it, in my DAO, I'd be better off using a SessionFactory > that has been preconfigured and bound to JNDI via hibernate.cfg.xml. No > problem there as you've adequately allowed for this via named session > factories. In my stand-alone application, I'd like to be able to do the > same, but there's no JNDI around so I wonder if my only option is to use > datastore and properties files for configuration? > > Looking at the code it seems like named session factories ought to be > possible (and useful) without JNDI. If JDNI names were instead configured > using a property (connection.jndiname?) then it seems that one could specify > named factories in the config, keep all config info in the xml file and > obtain the correct factory either by querying SessionFactoryObjectFactory or > going against JNDI. > > What I was hoping was that I'd be able to do something like this: > > hibernate.cfg.xml ===================== > <hibernate-configuration> > <session-factory name="localfactory"> > <property > name="connection.driver_class">org.hsqldb.jdbcDriver</property> > <property > name="connection.url">jdbc:hsqldb:c:/data/hsql/test</property> > <property name="connection.username">sa</property> > <property name="dialect">cirrus.hibernate.sql.HSQLDialect</property> > <!-- mapping files --> > <mapping resource="example.hbm.xml"/> > </session-factory> > > <session-factory name="jndifactory"> > <property name="jndiname">/env/daofactory</property> > <property name="jta.UserTransaction"> > java:comp/UserTransaction/ > </property> > <property name="connection.datasource">/ejbds/</property> > <property name="dialect">cirrus.hibernate.sql.HSQLDialect</property> > <!-- mapping files --> > <mapping resource="example.hbm.xml"/> > </session-factory> > </hibernate-configuration> > > This would allow me to set up my DAO more or less as follows. > > // Abstract inherited by DAO subclasses > public BaseHibernateDAO(String config) throws DAOException { > protected SessionFactory factory; > try { > new Configuration(config).configuration.configure(); > } catch (HibernateException e) { > throw new DAOException(e); > } > } > > // Java beans would acquire factory via SessionFactoryObjectFactory > public HibernateDAO(String config) extends BaseHibernateDAO throws > DAOException { > super(config); > factory = SessionFactoryObjectFactory.getInstance("localfactory"); > } > > // EJB session beans would acquire factory via JNDI > public EJBHibernateDAO(String config) extends BaseDAO throws DAOException { > super(config); > try { > factory = (SessionFactory) new > InitialContext().lookup("/env/daofactory"); > } catch (NamingException e) { > throw new DAOException(e); > } > } > > Oh yeah, it'd also be cool if there were a way to share mapping resources > with multiple session factories more or less the way you do with properties. > I don't think the DTD allows this right now, so in the example I gave above > I would have to duplicate all mapping resource declarations despite the fact > that both factories use the same set of resources. > > Forgive me if I've got anything wrong here -- I'm still quite new to > Hibernate and this is coming off the top of my head. Maybe there are ways > to do this already and I'm simply not seeing them. > > > > > > > > ------------------------------------------------------- > This sf.net email is sponsored by:ThinkGeek > Welcome to geek heaven. > http://thinkgeek.com/sf > _______________________________________________ > hibernate-devel mailing list > hib...@li... > https://lists.sourceforge.net/lists/listinfo/hibernate-devel |