[Dev] New support for Hibernate XML mapping files
Brett Wooldridge
bwooldridge at alterpoint.com
Wed May 7 03:20:42 CDT 2008
Cool extension, and I don't mind the addition, but ZipTie developers take note that I prefer named query annotations on the class. They do the same thing and are associated with the object/table on which they operate. We don't have any pure DBA/SQL folks who eschew Java. :-)
@NamedQuery(
name="getAllOrder",
queryString="SELECT OBJECT(o) FROM CustomerOrder as o WHERE
o.customerId = :customerId")
And subsequently:
Query allOrders = em.createNamedQuery("getAllOrder");
allOrders.setParameter("customerId",customerId);
Collection <CustomerOrder> results = allOrders.getResultList();
return results;
Ed, I was planning to switch our org.ziptie.zap.hibernate to a pure JPA approach in the next two releases? Are these named queries in XML resource files Hibernate specific? If so, when I do the refactor, I'll be sure to encapsulate it in a new bundle (org.ziptie.zap.jpa) so that there is a graceful fork/non-conflicting branch.
-Brett
On 5/7/08 11:08 AM, "Edmund Grossenbacher" <egrossenbacher at alterpoint.com> wrote:
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/20080507/d92c1cc1/attachment.html
More information about the Dev
mailing list