-->
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.  [ 3 posts ] 
Author Message
 Post subject: Criteria alias changes Outer join to inner join
PostPosted: Tue Jun 29, 2010 3:17 pm 
Newbie

Joined: Tue Jun 29, 2010 1:35 pm
Posts: 3
Hi,

Iam using Hibernate 3.0.5 and JDk 1.4. What iam tryin gto achieve is to create an alias for unidirectional Many-to-one association. I want it to perform an outer join which it does as long as i dont use createAlias in my Criteria query. Can you please help me out with this.

My Mapping:-
Code:
<class name="com.domain.businessobjects.RecentSlotTransaction"
      table="SLOTTRANSACTIONS" lazy="false" optimistic-lock="none">
      <id name="transactionKey" column="TRANSACTIONKEY" type="big_decimal">
         <generator class="assigned" />
      </id>

      <many-to-one name="attendant"
         class="com.domain.businessobjects.Attendant" lazy="false"
         outer-join="true" update="false" cascade="none" not-null="false"
         insert="false" not-found="ignore" fetch="join" optimistic-lock="false">
         <column name="CARDNO" />
      </many-to-one>
      </class>

<class name="com.domain.businessobjects.Attendant"
      table="SLOT_ATTENDANTS" lazy="false" where=" TIMEIN !=0 and DATEIN !=0 "
      optimistic-lock="none">
      <id name="cardNo" column="CARDNO"
         type="com.domain.persistence.types.StringAsFixedCharType">
         <generator class="assigned" />
      </id>
      <property name="attendantName" column="ATTENDANTNAME"
         not-null="true"
         type="com.domain.persistence.types.StringAsFixedCharType" />
   </class>


My Business objects:-

Code:

public class RecentTransaction extends MutableBusinessObject implements
      Serializable {

   private Attendant attendant;
   
      public SlotAttendant getAttendant() {
         return attendant;
      }
   
      public void setAttendant(SlotAttendant attendant) {
         this.attendant = attendant;
}

public class Attendant implements ImmutableBusinessObject{
   
   /* (non-Javadoc)
    * @see java.lang.Object#hashCode()
    */
   public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result
            + ((attendantName == null) ? 0 : attendantName.hashCode());
      result = prime * result + ((cardNo == null) ? 0 : cardNo.hashCode());
      return result;
   }

   /* (non-Javadoc)
    * @see java.lang.Object#equals(java.lang.Object)
    */
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (!(obj instanceof Attendant))
         return false;
      Attendant other = (Attendant) obj;
      if (attendantName == null) {
         if (other.attendantName != null)
            return false;
      } else if (!attendantName.equals(other.attendantName))
         return false;
      if (cardNo == null) {
         if (other.cardNo != null)
            return false;
      } else if (!cardNo.equals(other.cardNo))
         return false;
      return true;
   }

   /**
    *
    */
   private static final long serialVersionUID = -38107996517669240L;

   private String cardNo;
   
   private String attendantName;

   public void setCardNo(String cardNo) {
      this.cardNo = cardNo;
   }

   public String getCardNo() {
      return cardNo;
   }

   public void setAttendantName(String attendantName) {
      this.attendantName = attendantName;
   }

   public String getAttendantName() {
      return attendantName;
   }

}



My DAO code :-

Code:

Criteria aCriteria = getHibernateDAO().getSession().createCriteria(
            RecentTransaction.class);
         
transactions = getHibernateDAO().executeCriteriaSearch(aCriteria);


The above when executed generates a SQl query as follows
Select * from Transactions A left outer join Attendants B ........

But i want to create a alias as follows

Code:

Criteria aCriteria = getHibernateDAO().getSession().createCriteria(
            RecentTransaction.class);

   aCriteria.createAlias("attendant", "tr").setFetchMode("attendant", FetchMode.JOIN);
         
transactions = getHibernateDAO().executeCriteriaSearch(aCriteria);


It generates an sql query with inner join. which roughly looks like the below.

Select * from Transactions A inner join Attendants B ........

The question is how do i still get the outer join to be performed even after creating an alias. I also tried to set FetchMode to EAGER and DEFAULT. there is no difference it still generates an inner join. Any help will be appreciated.

Thank you


Top
 Profile  
 
 Post subject: Re: Criteria alias changes Outer join to inner join
PostPosted: Thu Jul 01, 2010 3:22 am 
Newbie

Joined: Thu Jul 01, 2010 2:28 am
Posts: 3
Hello,

there is another createAlias() method, providing a third parameter that changes the JOIN type:

Code:
aCriteria.createAlias("attendant", "tr", Criteria.LEFT_JOIN)


I did not try it out but maybe it helps.

Regards, Christian


Top
 Profile  
 
 Post subject: Re: Criteria alias changes Outer join to inner join
PostPosted: Thu Jul 01, 2010 9:14 am 
Newbie

Joined: Tue Jun 29, 2010 1:35 pm
Posts: 3
Thanks a lot for the reply mate..... unfortunately the version(3.0.5) that iam using does not have the method you have suggested.


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