-->
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.  [ 4 posts ] 
Author Message
 Post subject: Requete utilisant un Criteria
PostPosted: Tue Oct 18, 2005 1:23 am 
Newbie

Joined: Wed Oct 12, 2005 9:20 pm
Posts: 16
Location: Sydney
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.1

Mapping documents:
ExternalHdd.hbm.xml
Code:
<hibernate-mapping package="com.siliconmemory.hibernate.model">
   <class name="ExternalHdd" table="external_Hdd">
      <id column="id" name="id" type="long">
         <generator class="increment" />
      </id>

      <!--  Item fields -->
      ...
      <!-- External Hdd Fields -->
      <many-to-one name="hdd" column="hdd_id" class="Hdd" lazy="false"/>      
   </class>
</hibernate-mapping>


Hdd.hbm.xml
Code:
<hibernate-mapping package="com.siliconmemory.hibernate.model">
   <class name="Hdd" table="hdd">
      <id column="id" name="id" type="long">
         <generator class="increment" />
      </id>

      <!--  Item fields -->
      <property column="description" name="description" type="string"/>
      <property column="freight" name="freight" type="float"/>
      <property column="markup1" name="markup1" type="float"/>
      <property column="markup2" name="markup2" type="float"/>
      <property column="markup3" name="markup3" type="float"/>
      <property column="markup4" name="markup4" type="float"/>
      <property column="distributor_markup" name="distributorMarkup" type="float"/>
      <property column="dealer_markup" name="dealerMarkup" type="float"/>
      <property column="rrp_markup" name="rrpMarkup" type="float"/>
      <property column="smt_part_number" name="smtPartNumber" type="string"/>
      <property column="warranty_smt" name="warrantySmt" type="int"/>
      <many-to-one column="status_id" name="status" class="Status"/>
      <set name="accessories" table="ln_hdd_accessory">
         <key column="id_hdd"/>
         <many-to-many class="Accessory" column="accessory_id"/>
      </set>
      
      <!-- Product Fields -->
      <property column="aus_dollar" name="ausDollar" type="boolean" />
      <many-to-one name="manufacturer" column="manufacturer_id" class="Manufacturer"/>
      <property column="manufacturer_part_number" name="manufacturerPartNumber" type="string"/>
      <property column="model_number" name="modelNumber" type="string"/>
      <property column="unit_cost" name="unitCost" type="float"/>
      <property column="warranty_manu" name="warrantyManufacturer" type="int" />
      
      <!-- Hdd Fields-->
      <many-to-one name="size" column="size_id" class="Size"/>
      <property column="series" name="series" type="string" />
      <property column="capacity" name="capacity" type="float" />
      <property column="enclosure" name="enclosure" type="boolean" />
      <many-to-one name="buffer" column="buffer_id" class="Buffer"/>
      <many-to-one name="interfaceInstance" column="interface_id" class="InterfaceInstance"/>
      <many-to-one name="rpm" column="rpm_id" class="Rpm"/>
   </class>
</hibernate-mapping>




Name and version of the database you are using: MySQL 4.024

The generated SQL (show_sql=true): Hibernate: select this_.id as id16_0_, <...> this_.hdd_id as hdd15_16_0_ from external_Hdd this_ where this_.hdd_id=?


Bonjour,

Je suis en train de transformer mes requetes HQL en Criteria que je trouve plus lisible. Malheureusement, j'ai un comportement un peu bizarre pour l'un de mes criteres :
Voici le code pour le HQL:
Code:
String query = "from ExternalHdd as obj where obj.hdd.id=?";
String value = hdd.getId()+""; // getId() retourne un Long
Query SQLquery = session.createQuery(query).setParameter(0, value).list();


La requete HQL me retourne 1 ExternalHdd dans la liste.

Si maintenant je passe aux Criteria:
Code:
Criteria crit = session.createCriteria(ExternalHdd.class);
crit.add(Restrictions.eq("hdd", hdd)); // hdd est une instance de Hdd
crit.list();


Me genere :
Code:
Hibernate: select this_.id as id16_0_, <...> this_.hdd_id as hdd15_16_0_ from external_Hdd this_ where this_.hdd_id=?


Ce qui me semble correct, je n'ai pas d'erreurs de la part d'Hibernate mais 0 objets dans ma liste.

Quelqu'un a une idee ?


Merci d'avance.

Ben


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 1:55 am 
Newbie

Joined: Wed Oct 12, 2005 9:20 pm
Posts: 16
Location: Sydney
Bon, j'ai trouve l'erreur, mais je la comprends pas:

Le code exact pour le Criteria etait :
Code:
Criteria crit = session.createCriteria(ExternalHdd.class);
crit.add(Restrictions.eq("hdd", hdd));
List list = new ArrayList();
try{
   crit.list();
}catch(HibernateException e){
   e.printStackTrace();
   throw new DAOException(e);
}
return list;


Je voulais en effet etre capable de recupere les exceptions Hibernate generees (si elles exitent) et de les transformer en Exception qui sont traitees de maniere automatique par l'application.

En executant ce code, la liste est vide.

Si j'execute :
Code:
Criteria crit = session.createCriteria(ExternalHdd.class);
crit.add(Restrictions.eq("hdd", hdd));
return crit.list();


La liste contient 1 objet.

Je voudrais que l'on m'explique.....

Pourquoi le fait de rajouter un Try{}catch empeche les Criteria de fonctionner ????


Merci


Ben


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 3:32 am 
Expert
Expert

Joined: Thu Sep 04, 2003 8:23 am
Posts: 368
Ne manque t'il pas juste un
Code:
list = crit.list();

dans ton try {} catch

_________________
Seb
(Please don't forget to give credits if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 4:26 am 
Newbie

Joined: Wed Oct 12, 2005 9:20 pm
Posts: 16
Location: Sydney
Tohhh !!!

{partir discrètement...}


Merci de m'avoir pointé cette grosse boulette de ma part !


Ben


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.