-->
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.  [ 6 posts ] 
Author Message
 Post subject: Help with Hibernate's Criteria query
PostPosted: Thu May 17, 2007 10:06 am 
Newbie

Joined: Thu May 17, 2007 10:02 am
Posts: 2
I have to perform left outer join on 2 tables, omit nulls & specify additional restrictions during join condition.
SQL looks like this:
select * from dd
left outer join nwd
on dd.Id = nwd.Id and nwd.div=17
where and dd.year=2007 and nwd.Id is null

The highlighted condition has to be specified in the join ON clause ONLY to get the required result.

Code snippet:
Criteria ddc = Session().createCriteria(DD.class);
dateDimCriteria.add(Restrictions.eq("year", 2007));

Criteria n = ddc.createCriteria("nwd", Criteria.LEFT_JOIN);
n.add(Restrictions.eq("div", 17));
n.add(Restrictions.isNull(id));
return ddc.list();

Hibernate generated query:
select this_.id as date1_0_1_, this_.year as year6_0_1_ from DD
this_ left outer join NWD nw1_
on this_.id = nw1_.id
where this_.year=? and nw1_.div=? and nw1_.id is null

The restrictions are appended to the where clause, hence it does not retrieve the required result. How do we supply additional restrictions to join condition? Is there anything wrong with my code? Please help. Thanks,


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 21, 2007 7:37 am 
Newbie

Joined: Thu Nov 16, 2006 9:24 pm
Posts: 4
Shouldn't it be
Code:
n.list()
instead?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 21, 2007 8:43 am 
Newbie

Joined: Mon May 21, 2007 3:36 am
Posts: 16
Am I right?
You want to join dd and nwd on equal ids, but at the same time you want to have results with null ids only? What are the primary key columns of your table?
Please append mapping information!


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 21, 2007 9:52 am 
Newbie

Joined: Thu May 17, 2007 10:02 am
Posts: 2
Here is the mapping info.
Code:
@Entity
@Table(name = "DATE_DIM")
public class DateDim implements Serializable {
   private int dateDimId;

   private List<NonWorkingDate> nwd;
                private int year;
   
   @Id
    @Column(name="date_dim_id", insertable = false, updatable = false, nullable = false)
   public int getDateDimId() {
      return dateDimId;
   }

   public void setDateDimId(int dateDimId) {
      this.dateDimId = dateDimId;
   }

   @Column(name = "year")
   public int getYear() {
      return year;
   }

   public void setYear(int year) {
      this.year = year;
   }

   @OneToMany( fetch = FetchType.EAGER,  targetEntity = NonWorkingDate.class )
   @JoinColumn(name="date_dim_id")
   public List<NonWorkingDate> getNwd() {
      return nwd;
   }

   public void setNwd(List<NonWorkingDate> nwd) {
      this.nwd = nwd;
   }
}


@Entity
@Table(name = "NON_WORKING_DATE")
public class NonWorkingDate implements Serializable   {

   private int dateDimId;
   private DateDim dateDim;
                private int div;

   @Id
   @Column(name = "date_dim_id")
   public int getDateDimId() {
      return dateDimId;
   }

   public void setDateDimId(int dateDimId) {
      this.dateDimId = dateDimId;
   }

   @ManyToOne( fetch = FetchType.EAGER,  targetEntity = DateDim.class )
    @JoinColumn(name="date_dim_id", insertable = false, updatable = false, nullable = false)
   public DateDim getDateDim() {
      return dateDim;
   }

   public void setDateDim(DateDim dateDim) {
      this.dateDim = dateDim;
   }

   @Column(name = "div")
   public int getDiv() {
      return div;
   }

   public void setDiv(int div) {
      this.div = div;
   }

}


In the NonWorkingDate entity(or table) the primary key date_dim_id is also the foreignkey in DateDim table.
In DateDim table date_dim_id is the primary key.
Here is the Hibernate criteria:
Code:
Criteria ddc = Session().createCriteria(DD.class);
dateDimCriteria.add(Restrictions.eq("year", 2007));

Criteria n = ddc.createCriteria("nwd", Criteria.LEFT_JOIN);
n.add(Restrictions.eq("div", 17));
n.add(Restrictions.isNull(id));
return ddc.list();


I have to get rows in DateDim that are not in NonWorkingDate. The sql using join will be
select * from dd
left outer join nwd
on dd.Id = nwd.Id and nwd.div=17
where and dd.year=2007 and nwd.Id is null

projyal,
Code:
n.list();
will perform outerjoin on nonWorkingDate instead of DateDim. It will translate to select * from nwd left outer join nwd on dd. This is not what I want.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 21, 2007 11:29 am 
Newbie

Joined: Mon May 21, 2007 3:36 am
Posts: 16
If I get that right, you are searching for all DateDim objects that do not contain any NonWorkingDate objects having div=17 (but they may contain any other NonWorkingDate objects).

So in HQL the query should look like the following:

Code:
select dd from DateDim as dd
where empty(select n from dd.nwd as n where n.div=17)


Of course this will not result in the SQL statement you've mentioned (instead you will get an SQL statement having one correlated subselect), but I think that shouldn't matter as long as you get the correct result set


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 22, 2007 7:24 am 
Newbie

Joined: Thu Nov 16, 2006 9:24 pm
Posts: 4
Can you please post the SQL you want to be generated?

cheers
Projyal


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