In my Java program with Hibernate I have XML file with two Named Query:
<hibernate-mapping>
<sql-query name="BIG_SQL_QUERY_WITH_LOT_OF_WHERE">
<![CDATA[
select * from Foo
where ...
and ...
and ...
...
order by ...
]]>
</sql-query>
<sql-query name="BIG_SQL_QUERY_WITH_LOT_OF_WHERE_WITH_ONE_MORE_CONDITION">
<![CDATA[
select * from Foo
where ...
and ...
and ...
...
and one_more_condition = ...
order by ...
]]>
</hibernate-mapping>
First query ("BIG_SQL_QUERY_WITH_LOT_OF_WHERE") has a lot of join and conditions inside. Second query ("BIG_SQL_QUERY_WITH_LOT_OF_WHERE_WITH_ONE_MORE_CONDITION") is the same query but it has one more where condition.
Is it any way to make one query with common part of this query and use in them (or include) only this one part of second query (only this one more condition)?
Pure XML solution:
you could use a XML entity
, that is an abbreviated entry you can place anywhere in your XML document:
<?xml version="1.0" standalone="no" ?>
<!DOCTYPE hibernate-mapping [
<!ENTITY includedStuff SYSTEM "common.xml">
]>
<hibernate-mapping>
<sql-query name="BIG_SQL_QUERY_WITH_LOT_OF_WHERE">
&includedStuff;
</sql-query>
</hibernate-mapping>