These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Object with data from multiple tables...
PostPosted: Tue Jan 23, 2007 9:36 am 
Newbie

Joined: Mon Feb 20, 2006 6:31 am
Posts: 17
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());
      }


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 24, 2007 9:59 am 
Newbie

Joined: Mon Feb 20, 2006 6:31 am
Posts: 17
Just in case anyone should ever need a similar construction: I solved it by splitting the object into an object containing a reference to an abstract class. The 2 concrete implementations of that abstract class don't add any functionality, but they offer the oportunity to define a "table-per-subclass". I defined the mapping using abstract=true for the abstract class without assigning a table to it and <union-subclass table=""> for each concrete class.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.