-->
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.  [ 5 posts ] 
Author Message
 Post subject: Help with complex HQL query
PostPosted: Tue Aug 15, 2006 5:49 pm 
Beginner
Beginner

Joined: Thu Jun 09, 2005 2:33 pm
Posts: 30
I'm have a fairly complex(to me at least) query that I need to translate to HQL. It uses some MySQL date functions. Anyway here's the sql and all relevant code:

Code:
select count(id),DATE_FORMAT(MAIL_DATE,'%U %X') as week from dmm_request_log where year(MAIL_DATE)='2004' group by week ;


The pojo:
Code:
package com.package.model;

import java.util.Date;

/**
* @hibernate.class table="dmm_request_log"
*/
public class GuidebookRequest {
   
   Long id;
   String adId;
   String email;
   String name1;
   String name2;
   String city;
   String state;
   String zip;
   String country;
   String source;
   Date mailDate;
   Date fileDate;
   
   public GuidebookRequest() {
      
   }
   /**
    * @return the id
    * @hibernate.id generator-class="native" column="id"
    */
   public Long getId() {
      return id;
   }
   /**
    * @param id the id to set
    */
   public void setId(Long id) {
      this.id = id;
   }
   /**
    * @return the adId
    * @hibernate.property column="ad_id"
    */
   public String getAdId() {
      return adId;
   }
   /**
    * @param adId the adId to set
    */
   public void setAdId(String adId) {
      this.adId = adId;
   }
   /**
    * @return the city
    * @hibernate.property column="city"
    */
   public String getCity() {
      return city;
   }
   /**
    * @param city the city to set
    */
   public void setCity(String city) {
      this.city = city;
   }
   /**
    * @return the country
    * @hibernate.property column="country"
    */
   public String getCountry() {
      return country;
   }
   /**
    * @param country the country to set
    */
   public void setCountry(String country) {
      this.country = country;
   }
   /**
    * @return the email
    * @hibernate.property column="email"
    */
   public String getEmail() {
      return email;
   }
   /**
    * @param email the email to set
    */
   public void setEmail(String email) {
      this.email = email;
   }
   /**
    * @return the fileDate
    * @hibernate.property column="file_date"
    */
   public Date getFileDate() {
      return fileDate;
   }
   /**
    * @param fileDate the fileDate to set
    */
   public void setFileDate(Date fileDate) {
      this.fileDate = fileDate;
   }
   /**
    * @return the mailDate
    * @hibernate.property column="mail_date"
    */
   public Date getMailDate() {
      return mailDate;
   }
   /**
    * @param mailDate the mailDate to set
    */
   public void setMailDate(Date mailDate) {
      this.mailDate = mailDate;
   }
   /**
    * @return the name1
    * @hibernate.property column="name1"
    */
   public String getName1() {
      return name1;
   }
   /**
    * @param name1 the name1 to set
    */
   public void setName1(String name1) {
      this.name1 = name1;
   }
   /**
    * @return the name2
    * @hibernate.property column="name2"
    */
   public String getName2() {
      return name2;
   }
   /**
    * @param name2 the name2 to set
    */
   public void setName2(String name2) {
      this.name2 = name2;
   }
   /**
    * @return the source
    * @hibernate.property column="source"
    */
   public String getSource() {
      return source;
   }
   /**
    * @param source the source to set
    */
   public void setSource(String source) {
      this.source = source;
   }
   /**
    * @return the state
    * @hibernate.property column="state"
    */
   public String getState() {
      return state;
   }
   /**
    * @param state the state to set
    */
   public void setState(String state) {
      this.state = state;
   }
   /**
    * @return the zip
    * @hibernate.property column="zip4"
    */
   public String getZip() {
      return zip;
   }
   /**
    * @param zip the zip to set
    */
   public void setZip(String zip) {
      this.zip = zip;
   }
   
   
}



And the mapping:
Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
>
    <class
        name="com.package.model.GuidebookRequest"
        table="dmm_request_log"
    >

        <id
            name="id"
            column="id"
            type="java.lang.Long"
        >
            <generator class="native">
              <!-- 
                  To add non XDoclet generator parameters, create a file named
                  hibernate-generator-params-GuidebookRequest.xml
                  containing the additional parameters and place it in your merge dir.
              -->
            </generator>
        </id>

        <property
            name="adId"
            type="java.lang.String"
            update="true"
            insert="true"
            column="ad_id"
        />

        <property
            name="city"
            type="java.lang.String"
            update="true"
            insert="true"
            column="city"
        />

        <property
            name="country"
            type="java.lang.String"
            update="true"
            insert="true"
            column="country"
        />

        <property
            name="email"
            type="java.lang.String"
            update="true"
            insert="true"
            column="email"
        />

        <property
            name="fileDate"
            type="java.util.Date"
            update="true"
            insert="true"
            column="file_date"
        />

        <property
            name="mailDate"
            type="java.util.Date"
            update="true"
            insert="true"
            column="mail_date"
        />

        <property
            name="name1"
            type="java.lang.String"
            update="true"
            insert="true"
            column="name1"
        />

        <property
            name="name2"
            type="java.lang.String"
            update="true"
            insert="true"
            column="name2"
        />

        <property
            name="source"
            type="java.lang.String"
            update="true"
            insert="true"
            column="source"
        />

        <property
            name="state"
            type="java.lang.String"
            update="true"
            insert="true"
            column="state"
        />

        <property
            name="zip"
            type="java.lang.String"
            update="true"
            insert="true"
            column="zip4"
        />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-GuidebookRequest.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>




Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 15, 2006 6:17 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Don't convert that to HQL. HQL is best suited to loading entities: you're loading two scalars. Just create that as an SQL query (Session.createSQLQuery).

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 15, 2006 6:27 pm 
Beginner
Beginner

Joined: Thu Jun 09, 2005 2:33 pm
Posts: 30
Thanks I think that's a good approach. Any other thoughts?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 15, 2006 10:32 pm 
Beginner
Beginner

Joined: Thu Jun 09, 2005 2:33 pm
Posts: 30
Ok, I constructed a native SQL query as follows. First off, does this look correct? and second, what will it return? an array?

Code:
String sql = "select count(id) as count,DATE_FORMAT(MAIL_DATE,'%U') as week from dmm_request_log where YEAR(MAIL_DATE)='2004' group by week";
      Session session = this.getSession();
      SQLQuery obj = session.createSQLQuery(sql).addScalar("count", Hibernate.LONG).addScalar("week", Hibernate.OBJECT);


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 15, 2006 10:56 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Looks fine. obj.list() will return a List<Object[2]>, and the two scalars will contain the count and the week.

_________________
Code tags are your friend. Know them and use them.


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