-->
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: How to get a list when using composite-id and a list of id's
PostPosted: Thu Jun 15, 2006 8:59 am 
Newbie

Joined: Thu Jun 15, 2006 8:32 am
Posts: 6
I am using Hibernate version:3.1.3 with Mysql 4.1.11 and my test classes are using: hsqldb 1.8.0.1

I try to get a list of Data (abstract superclass of BinaryData and XMLData) using the following code:

Code:
   public List<Data> get(List<DataIdentifier> dataIdentifiers) throws DaoException {
      List <Data>dataList = new ArrayList<Data>();
      try {                  
         Criteria criteria = getSession().createCriteria(Data.class);
         criteria.add( Expression.in("dataIdentifier", dataIdentifiers) );
         dataList = criteria.list();
         
      } catch (Exception e) {
         log.error(e,e);
         throw new DaoException("Get failed ", e);
      }
      return dataList;
   }


With mysql I don't get any results. Here is the query generated by Hibernate:

With hsqldb I got an exception. If needed I can post the exception. The query is quite long so I left it out.

Here are my mapping files:

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 package="nl.tesis">
  <class name="Data" table="data">

      <composite-id name="dataIdentifier" class="DataIdentifier">
        <key-property name="userId" length="200"/>
        <key-property name="eaGroupId"/>
        <key-property name="dossierId"/>
        <key-property name="sectionId"/>                        
     </composite-id>
     
     <property name="created"/>
     <property name="createdByUserId"/>
     <property name="lastModified"/>
     <property name="lastModifiedByUserId"/>
          
  </class>
</hibernate-mapping>


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 package="nl.tesis">
  <joined-subclass name="BinaryData" table="binarydata" extends="Data">
   <key>
      <column name="userId" />
      <column name="eaGroupId" />
      <column name="dossierId" />
      <column name="sectionId" />            
   </key>


     <property name="mimeType" />
     <property name="originalFilename" />
   <property name="data" length="10485760"/> <!-- 1024 * 1024 * 10 = 10Mb-->
 
  </joined-subclass>
</hibernate-mapping>


The mapping file for XMLData is almost the same as BinrayData. There are 2 different properties.

The generated SQL (show_sql=true):


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 19, 2006 4:10 am 
Newbie

Joined: Thu Jun 15, 2006 8:32 am
Posts: 6
This is my fix for now. Does anyone know a better solution?

Code:
   /* (non-Javadoc)
    * @see nl.tesis.dao.DataDao#get(java.util.List)
    */
   public List<Data> get(List<DataIdentifier> dataIdentifiers) throws DaoException {
      
      List <Data>dataList = new ArrayList<Data>();
      if (dataIdentifiers.size() > 0 ) {
         try {
         
            Criteria criteria = getSession().createCriteria(Data.class);
            if (dataIdentifiers.size()==1) {
               criteria.add( Expression.eq("dataIdentifier", dataIdentifiers.get(0)) );
            } else if (dataIdentifiers.size()==2) {
               criteria.add( Expression.or(Expression.eq("dataIdentifier", dataIdentifiers.get(0)), Expression.eq("dataIdentifier", dataIdentifiers.get(1))) );            
            } else {
               LogicalExpression logicalExpression = Expression.or(Expression.eq("dataIdentifier", dataIdentifiers.get(0)), Expression.eq("dataIdentifier", dataIdentifiers.get(1)));
               
               for (int i = 2; i < dataIdentifiers.size(); i++) {
                  logicalExpression = Expression.or(Expression.eq("dataIdentifier", dataIdentifiers.get(i)), logicalExpression);
               }
               
               criteria.add( logicalExpression );   
               
            }
            dataList = criteria.list();
            
   
         } catch (Exception e) {
            log.error(e,e);
            throw new DaoException("Get failed ", e);
         }
      }
      return dataList;
   }


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.