-->
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: Joining and Projections
PostPosted: Fri May 25, 2007 4:59 am 
Newbie

Joined: Fri May 25, 2007 4:37 am
Posts: 13
Location: Germany
Hi there!

I have a Many-To-One Relation between the entities AdLog and Page. In the AdLog entity there should be inserted an entry each time a Page gets displayed. And a Page should only be displayed when its adMaxTime is not overstepped. So, I came to the conclusion that I have to join the entities, sum up AdLog.duration and compare the sum with Page.adMaxTime. Finally I should get a List of Pages.

I tried several things with createCriteria, setProjections, createAlias and setFetchMode. Nothing worked for me. Maybe s.o. can help me with this as I do not find any helpful documentation/example of how to handle this issue.

Thanks in advance!

Mapping documents:
Code:
@Entity()
@Table(name = "page")
public class Page implements Serializable {
   
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "pag_id", nullable = false)
   private int id;
   
   @OneToMany(mappedBy="page")
   private List<AdAllocation> adAllocations = new ArrayList<AdAllocation>();

   @OneToMany(mappedBy="page")
   private List<AdLog> adLogs = new ArrayList<AdLog>();
   
   @Column(name = "pag_admaxtime", nullable = true)
   private Integer adMaxTime = null;

   @Column(name = "pag_title", nullable = false, length = 128)
   private String title;


Code:
@Entity()
@Table(name = "adlog")
public class AdLog implements Serializable {

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "adl_id", nullable = false)
   private int id;
   
   @ManyToOne(optional = false)
   @JoinColumn(name = "pag_id", nullable = false)
   private Page page;
   
   @Column(name = "adl_duration", nullable = false)
   private int duration = 30;
   
   @Column(name = "adl_datetime", nullable = false)
   private Date datetime = new Date(0);


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 25, 2007 7:49 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

Send me sql query which you want to convert to hibernate Criterian


Amila


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 16, 2007 4:57 am 
Newbie

Joined: Fri May 25, 2007 4:37 am
Posts: 13
Location: Germany
I need something like this:

Code:
SELECT P.*, SUM(A.duration) AS maxDuration FROM Page P
LEFT JOIN AdLog A
ON P.id = A.pag_id
GROUP BY A.pag_id
HAVING maxDuration < P.adMaxTime



This is my SQL-Query, that works perfectly:
Code:
SELECT P . * , SUM( A.adl_duration ) AS maxDuration
FROM page P
LEFT JOIN adlog A ON P.pag_id = A.pag_id
GROUP BY A.pag_id
HAVING maxDuration < P.pag_adMaxTime
OR P.pag_adMaxTime IS NULL


Can anybody translate this into Criteria Query? I just don't get it!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 16, 2007 7:08 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

HAVING Doesn't support with criteria API
see for patch
http://opensource.atlassian.com/projects/hibernate/browse/HHH-1700

use that patch

Amila

(Don't forget to rate if helps)


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.