-->
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.  [ 47 posts ]  Go to page Previous  1, 2, 3, 4  Next
Author Message
 Post subject:
PostPosted: Wed Apr 12, 2006 6:38 am 
Beginner
Beginner

Joined: Wed Jan 25, 2006 7:32 am
Posts: 34
Hi,

I have tried to defined the following:

<hibernate-mapping>
<class name="mlisting.entity.DatedMessage" mutable="false">
<subselect>
SELECT a.Id AS id,
a.date_sent AS date,
a.message AS message,
FROM table1 AS a
UNION ALL
SELECT s.Id AS id,
s.date_received AS date,
s.message AS message,
FROM table2 AS s
</subselect>
<synchronize table="table1"/>
<synchronize table="table2"/>

<id name="id" type="int"/>
<property name="message" type="string" />
<property name="date" type="string" />
</class>
<sql-query name="SMSInbox">
<return alias="sms" class="mlisting.entity.SMSBox"/>
SELECT a.Id AS {sms.id},
a.date_sent AS {sms.date},
a.message AS {sms.message},
from table1 a
union all
select s.Id AS {sms.id},
s.date_received AS {sms.date},
s.message AS {sms.message},
from table2 s
</sql-query>
</hibernate-mapping>


I tried to query the SQL from DAO using

List l = session.getNamedQuery("SMSInbox")
.setString("subscriber_id", subscriber_id)
.list();

But still I didn't work. It returned the same result which am not expecting to.

Please guide.Thank you!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 6:49 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
No, you don't need the query with that mapping.

Also, I suppose you only need DatedMessage class, no need for SMSBox class.

subscriber_id ????????

Something like: ???

Code:
String hql = "from mlisting.entity.DatedMessage where id = :subscriber_id";
List<DatedMessage> l = session.createQuery(hql)
   .setString("subscriber_id", subscriber_id)
   .list();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 7:00 am 
Beginner
Beginner

Joined: Wed Jan 25, 2006 7:32 am
Posts: 34
Hi,

I tried this before using createQuery() but it didn't work for UNION.


Sorry I provide the wrong info. here is the correct info:
<hibernate-mapping>
<class name="mlisting.entity.DatedMessage" mutable="false">
<subselect>
SELECT a.Id AS id,
a.date_sent AS date,
a.message AS message,
FROM table1 AS a
UNION ALL
SELECT s.Id AS id,
s.date_received AS date,
s.message AS message,
FROM table2 AS s
</subselect>
<synchronize table="table1"/>
<synchronize table="table2"/>

<id name="id" type="int"/>
<property name="message" type="string" />
<property name="date" type="string" />
</class>
<sql-query name="SMSInbox">
<return alias="sms" class="mlisting.entity.DatedMessage"/>
SELECT a.Id AS {sms.id},
a.date_sent AS {sms.date},
a.message AS {sms.message},
from table1 a
union all
select s.Id AS {sms.id},
s.date_received AS {sms.date},
s.message AS {sms.message},
from table2 s
</sql-query>
</hibernate-mapping>


I tried to query the SQL from DAO using

List l = session.getNamedQuery("SMSInbox")
.list();

Anyway to resolve it by not upgrading MySQL 4.1 to 5.0?

:(


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 7:05 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
Code:
<hibernate-mapping>
   <class name="mlisting.entity.DatedMessage" mutable="false">
      <subselect>
      SELECT a.Id AS id,
      a.date_sent AS date,
      a.message AS message,
      FROM table1 AS a
      UNION ALL
      SELECT s.Id AS id,
      s.date_received AS date,
      s.message AS message,
      FROM table2 AS s
      </subselect>
      
      <synchronize table="table1"/>
      <synchronize table="table2"/>

      <id name="id" type="int"/>
      <property name="message" type="string" />
      <property name="date" type="string" />
   </class>
</hibernate-mapping>


Code:
String hql = "from mlisting.entity.DatedMessage";
List<DatedMessage> datedMessages = session.createQuery(hql).list();


Do you get what i mean?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 7:24 am 
Beginner
Beginner

Joined: Wed Jan 25, 2006 7:32 am
Posts: 34
Hi Peplnm,

I got what you mean.Thanks!

To make it more constructive, if I need to include a WHERE statement
as below,

<hibernate-mapping>
<class name="mlisting.entity.DatedMessage" mutable="false">
<subselect>
SELECT a.Id AS id,
a.date_sent AS date,
a.message AS message,
FROM table1 AS a
where a.username_id = :username_id
UNION ALL
SELECT s.Id AS id,
s.date_received AS date,
s.message AS message,
FROM table2 AS s
where s.username_id = :username_id
</subselect>

<synchronize table="table1"/>
<synchronize table="table2"/>

<id name="id" type="int"/>
<property name="message" type="string" />
<property name="date" type="string" />
</class>
</hibernate-mapping>

I tried to call the query in such way by passing the username_id parameter:

String hql = "from mlisting.entity.DatedMessage";
List datedMessages = session.createQuery(hql).
.setString("username_id", username_id);
list();

But it didn't work.

Also even if I don't pass in the username_id parameter, the result returned with the defined <subselect>is still as below

id message date
1 XXXX 12/06/2005
2 XXXX 13/09/1998
3 XXXX 14/09/1999
1 XXXX 12/06/2005
2 XXXX 13/09/1998

Instead of

id message date
1 XXXX 12/06/2005
2 XXXX 13/09/1998
3 XXXX 14/09/1999
1 ABC 14/08/1999
2 ABC 13/07/1980

Am afraid that I need to upgrade to MySQL5.0 :(

Please guide.Thank you!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 7:47 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
You can't parameterized <subselect>.

I have modified the query to include username_id and to have a true id/primary key.
Code:
<hibernate-mapping>
   <class name="mlisting.entity.DatedMessage" mutable="false">
      <subselect>
         SELECT
            s.Id AS id,
            'S' AS type,
            s.date_sent AS date,
            s.message AS message,
            s.username_id AS username_id
         FROM
            table1 AS s
         UNION ALL
         SELECT
            r.Id AS id,
            'R' AS type,
            r.date_received AS date,
            r.message AS message,
            r.username_id AS username_id
         FROM
            table2 AS r
      </subselect>

      <synchronize table="table1"/>
      <synchronize table="table2"/>

      <composite-id>
         <key-property name="id" type="int"/>
         <key-property name="type" type="string"/>
      </composite-id>
      <property name="message" type="string"/>
      <property name="date" type="string"/>
      <property name="username_id" type="string"/>
   </class>
</hibernate-mapping>



Code:
String hql = "from mlisting.entity.DatedMessage where username_id = :username_id";
List somebodyDatedMessages = session.createQuery(hql).
   .setString("username_id", username_id);
   list();


Don't forget to add type and username_id properties to DatedMessage class.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 7:54 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
Your DatedMessage class must override equals() and hashCode() to implement composite identifier equality. It
must also implements Serializable.

Maybe the problem with tenwit SQL query approach was the lack of a true id/primary key.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 8:00 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
A possible implementation:

Code:
public boolean equals(Object obj) {
   if(obj.getClass().equals(this.getClass())) {
      DatedMessage dm = (DatedMessage) obj;
      return
         dm.getId() == getId()
         &&
         dm.getType().compareTo(getType()) == 0;
   } else {
      return false;
   }
}
public int hashCode() {
   return getType().compareTo("S") ? super.hashCode() : -super.hashCode();
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 8:26 am 
Beginner
Beginner

Joined: Wed Jan 25, 2006 7:32 am
Posts: 34
Hi,

Thanks for your possible solutions.

Am abit confused. Hope you don't mind.

Where do I define equals() and hashCode() to implement composite identifier equality. Is in the DatedMessage class or somewhere else?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 8:28 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
you got it right!
In the DatedMessage class.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 8:41 am 
Beginner
Beginner

Joined: Wed Jan 25, 2006 7:32 am
Posts: 34
Hmmmm.....

The hashCode() which you have defined is supposed to return int but it returned a boolean.....


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 8:47 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
Sorry.
Corrected code.
Code:
public int hashCode() {
   return getType().compareTo("S") == 0 ? super.hashCode() : -super.hashCode();
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 9:08 am 
Beginner
Beginner

Joined: Wed Jan 25, 2006 7:32 am
Posts: 34
Hi Peplmn,

The exact query which I suppose to execute is as below:

======================================
<!-- <sql-query name="DatedMessage">
<return alias="dtdmsg" class="mlisting.entity.DatedMessage"/>
SELECT a.Id AS {dtdmsg.id},
a.date_sent AS {dtdmsg.date},
a.message AS {dtdmsg.message},
a.sender AS {dtdmsg.sender},
a.recipient AS {dtdmsg.recipient}
from table1 a where a.recipient = :subscriber_id
union all
select s.Id AS {dtdmsg.id},
s.date_received AS {dtdmsg.date},
s.message AS {dtdmsg.message},
s.sender AS {dtdmsg.sender},
l.subscriber_id AS {dtdmsg.recipient}
from table2 s, Listing l
where l.subscriber_id = :subscriber_id and l.id = s.listing_id
</sql-query>-->
======================================

In table table2 I have a condition
where l.subscriber_id = :subscriber_id and l.id = s.listing_id

I wonder how can I define in the <subselect> based on your sample but don't think is correct :(


I tried this:

<hibernate-mapping>
<class name="mlisting.entity.DatedMessage" mutable="false">
<subselect>
SELECT a.Id AS id,
'S' AS type,
a.date_sent AS date,
a.message AS message,
a.sender AS sender,
a.recipient AS recipient,
a.recipient AS username_id
from table1 a
union all
select s.Id AS id,
'R' AS type,
s.date_received AS date,
s.message AS message,
s.sender AS sender,
l.subscriber_id AS recipient,
l.subscriber_id AS username_id
from table2 s, Listing l
where l.id = s.listing_id
</subselect>

<synchronize table="table1"/>
<synchronize table="table2"/>

<composite-id>
<key-property name="id" type="int"/>
<key-property name="type" type="string"/>
</composite-id>

<property name="message" type="string" />
<property name="sender" type="string" />
<property name="recipient" type="string" />
<property name="date" type="string" />
<property name="username_id" type="string" />
</class>
</hibernate-mapping>


I have defined getter and setter method for type and username_id in DatedMessage class.

I have also defined the hashCode() and equals() in DatedMessage class but seems like I can't even start the server after compiling it. The server returned having a problem of HbmBinder.bindCompositeId(Element, RootClass, Mappings, Map) line: 417

Please guide.Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 10:22 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
post DatedMessage class.

but between [code] [/code] tags please


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 8:35 pm 
Beginner
Beginner

Joined: Wed Jan 25, 2006 7:32 am
Posts: 34
Here it goes:


Code:
package mlisting.entity;

import mlisting.entity.Subscriber;

public class DatedMessage {
   private int id;
   
   private String date = "";
   private String message = "";
   private String sender = "";
   private String recipient = "";
   private String username_id="";
   private String type="";
   private String errorMessage="";
   
   public String getUsername_id() {
      return username_id;
   }

   public void setUsername_id(String username_id) {
      this.username_id = username_id;
   }
   public String getType() {
      return type;
   }

   public void setType(String type) {
      this.type = type;
   }
   
   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   public String getDate() {
      return date;
   }
   
   public void setDate(String date) {
      this.date = date;
   }

   public String getSender() {
      return sender;
   }

   public void setSender(String sender) {
      this.sender = sender;
   }
   
   public String getRecipient() {
      return recipient;
   }

   public void setRecipient(String recipient) {
      this.recipient = recipient;
   }
   
   public String getMessage() {
      return message;
   }

   public void setMessage(String message) {
      this.message = message;
   }
   
   public String getErrorMessage() {
      return errorMessage;
   }

   public void setErrorMessage(String errorMessage) {
      this.errorMessage = errorMessage;
   }
   
   public boolean equals(Object obj) {
         if(obj.getClass().equals(this.getClass())) {
            DatedMessage dm = (DatedMessage) obj;
            return
               dm.getId() == getId()
               &&
               dm.getType().compareTo(getType()) == 0;
         } else {
            return false;
         }
      }
   public int hashCode() {
         return getType().compareTo("A") == 0 ? super.hashCode() : -super.hashCode();
      }
   
}



Thank you!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 47 posts ]  Go to page Previous  1, 2, 3, 4  Next

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.