I am writing a cleanup function for a job scheduler. It involves 3 tables: TB_ALGO_JOB_EXECUTION, TB_ALGO_SCHEDULED_JOB and TB_ALGO_SCHEDULED_JOB_AUD. The first table contains some sort of logging of the jobs that have been executed. The second table contains the scheduled jobs and the third keeps track of jobs that were cancelled. As you can see in the query below: I need a query that takes a union of the joins of table 1 and 2 and table 1 and 3.
Is it possible to define a mapping for an object without binding it to a table? If I don't define the object in the mapping definition, it returns a mapping exception "Unknown entity"... I tried to like you can see in the example below...
Hibernate version: 3
Mapping documents:
Code:
<?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>
<class name="be.kbc.algo.hibernate.cleanup.JobExecution">
<property name="executionId" type="String" />
<property name="executionOutputDirectory" type="String" />
<property name="scheduleOutputDirectory" type="String" />
<property name="outputArchivable" type="short" />
</class>
<sql-query name="JobExecution" callable="true">
<return alias="je" class="be.kbc.algo.hibernate.cleanup.JobExecution" />
<![CDATA[
select
un.EXECUTION_ID as {je.executionId},
un.OUTPUT_DIR as {je.executionOutputDirectory},
un.OUTPUT_ROOT_DIR as {je.scheduleOutputDirectory},
un.IS_OUTPUT_ARCHIVABLE as {je.outputArchivable}
FROM
(select
JE.EXECUTION_ID,
JE.OUTPUT_DIR,
SJ.OUTPUT_ROOT_DIR OUTPUT_ROOT_DIR,
SJ.IS_OUTPUT_ARCHIVABLE IS_OUTPUT_ARCHIVABLE
from
AL.TB_ALGO_JOB_EXECUTION JE,
AL.TB_ALGO_SCHEDULED_JOB SJ
where
JE.SCHEDULE_ID = SJ.SCHEDULE_ID and
JE.END_DATETIME <= :firstEndDateTime
union
select
JE.EXECUTION_ID,
JE.OUTPUT_DIR,
SJA.OUTPUT_ROOT_DIR OUTPUT_ROOT_DIR,
SJA.IS_OUTPUT_ARCHIVABLE IS_OUTPUT_ARCHIVABLE
from
AL.TB_ALGO_JOB_EXECUTION JE,
AL.TB_ALGO_SCHEDULED_JOB_AUD SJA
where
JE.SCHEDULE_ID = SJA.SCHEDULE_ID and
JE.END_DATETIME <= :secondEndDateTime ) un;]]>
</sql-query>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Query query = session.getNamedQuery("JobExecution");
Timestamp now = new Timestamp(new GregorianCalendar().getTimeInMillis());
query.setParameter("firstEndDateTime", now);
query.setParameter("secondEndDateTime", now);
List result = query.list();
if (result != null) {
System.out.println(result.size());
}