[Dev] New support for Hibernate XML mapping files
Edmund Grossenbacher
egrossenbacher at alterpoint.com
Tue May 6 21:08:28 CDT 2008
I added support for Hibernate XML mapping to org.ziptie.zap.hibernate. The plugin extension point now supports zero or more "resource" elements in addition to the usual "class" elements. For example:
<extension point="org.ziptie.zap.hibernate.PersistenceUnit">
<persistence-unit name="ziptie-ds">
<class name="org.ziptie.foo.Bar"/>
<resource name="/META-INF/sql/queries.xml"/>
</persistence-unit>
</extension>
Needless to say, we have been and should continue to use EJB3 style annotated Java objects instead of the flyblown Hibernate XML stuff for mapping Java objects to relational tables. But the XML mapping files ALSO support named queries, both of the HQL and SQL variety, which are very convenient:
1) queries are validated at startup which further reduces the possibility of a runtime HQL/SQL syntax error;
2) queries are taken out of Java code and rendered more readable by DBA / SQL folks who eschew Java... :/
3) you avoid the risk of multiple preparation of the same query on a session.
Per the extension above, I have a file in my bundle at META-INF/sql/queries.xml that looks like this:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<sql-query name="getComplicatedSQLBars">
<return alias="node" class="org.ziptie.foo.Bar"/>
<![CDATA[
select
*
from
BAR_TABLE
where [complicated SQL things happen]
]]>
</sql-query>
<query name"getAllBars">
<![CDATA[
select
var
from
Bar
]]>
</query>
</hibernate-mapping>
Now in code I can score my session and grab the named query:
Session session = sessionFactory.getCurrentSession();
Query query = session.getNamedQuery("getAllBars");
List<Bar> bars = (List<Bar>) query.list();
etc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ziptie.org/pipermail/dev/attachments/20080506/ca58b7f7/attachment.html
More information about the Dev
mailing list