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: Views with HQL
PostPosted: Thu Mar 23, 2006 10:08 am 
Newbie

Joined: Tue Mar 21, 2006 4:25 am
Posts: 13
Hello,

I am working on a hibernate system, and I am trying to generate some views of the data to display to the user (some what like reports).

Now being a newby I have some problems in knowing what is the best aproach to generate a view of the data I have in the database.

When I say view I mean that you do not retrieve all the data from the a table, but a portion. For example following is the SQL of a view I would like to generate:

Code:
SELECT     dbo.toydef.name, COUNT(0) AS [childreen]
FROM         dbo.toy INNER JOIN
                      dbo.toydef ON dbo.toy.definition = dbo.toydef.id
GROUP BY dbo.toydef.name


As you can see in the above view I am joing the data from three two different tables. I am using the toydef table to retrieve the names of each toy, and I am using the toy table to see how many childreen are playing with a particular toy.

The output of this query in Microsoft Sql Server is something as follows:

Code:
  name      |  childreen 
----------------------------
  car       |  5
  doll      |  3
----------------------------


Now I tought whether I will have to create a pojo for each single view I have. This pojo woudl have two methods, one with toyname and the other one with ammount. Then the dao would return a list of these pojos.

Is this they way I have to go, or there would be a more simpler way. The reason I am asking this is because this would mean having to create a dao and pjo definition for each report I have to create.

thanks and regards,
kcorp


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 23, 2006 11: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 have two main options:
  1. Define a data/pojo/dto(data transfer object) class for the views
  2. Not define it.


Option 1 I think is the better. You don't need to map it. You have to use the "select new" syntax.
Think that if the structure of the view is repeated through the program you can reuse the same class.

Ex:
If you have (add public getters and setters)
Code:
class simpleViewDTO {
   String name;
   Integer count;
   public simpleViewDTO(String name, Integer count) {
      this.name = name;
      this.count = count;
   }
}

you think you could ask in HQL something like
Code:
select
   new simpleViewDTO(toy.name, count(*))
from
   Toy as toy
   join toy.definitions as def
group by
   toy.name


Option 2 is faster to implement but more difficult to maintain and debug. You receive the data from the view in an array.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 23, 2006 11:33 am 
Newbie

Joined: Tue Mar 21, 2006 4:25 am
Posts: 13
Hi,

First of all thank you very much for answering :) I do not know how many refreshes I have pressed.

thanks for your sugestion, I will implement it in that way. I prefer to keep my layer structers. My only so called problem is that I will have several classes which will be there just for nothing (just because a specidifc layer needs that class).

however, thanks a lot again :)
kcorp

ps: dto (Data Transmition Object) is the pojo class right! .. just to make sure... we have different naming conventions here :D


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 23, 2006 11:45 am 
Newbie

Joined: Tue Mar 21, 2006 4:25 am
Posts: 13
Also ... is it possible to set the set methods as private? would hibernate still find them and initialize the object accordingly. What I am aiming for is that from the presentation layer the developer would not have access to setName(something) since this can never be persisted back, so it would provide a false impresion to the developer.

reg,
kcorp


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 23, 2006 12:38 pm 
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
Hibernate only needs the constructor.

Getters and setters are for the programmer so you can drop the setters.

Also, I am putting these classes in an inner package inside my model package. So I need the full name of the DTO class in the HQL query.


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.