-->
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.  [ 1 post ] 
Author Message
 Post subject: Criteria Qurey for Composite key table
PostPosted: Mon Dec 29, 2008 8:29 am 
Newbie

Joined: Tue Nov 11, 2008 8:19 am
Posts: 11
Dear all.
I am using hibenate 3.0. I am getting an exception when i tried to fetch a data from a table with composite key. I can able to retrive the data without adding any restrictions in the criteria. How to apply the restrictions for the composite keys?

1. My hibernate mapping file
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

   <class name="nulamda.Bean.CompanyBankAccount" table="CompanyBankAccount" lazy="false" >
      <composite-id class="nulamda.Bean.CompanyBankAccount_PK"
         name="companyBankAccount_PK" access="property">
         <key-property name="CoBankAcctId" type="integer"></key-property>
         <key-property name="Company" type="integer"></key-property>
      
      </composite-id>

      <property name="CoBankAcctNo" type="string"></property>
      <property name="CoBankAcctName" type="string"></property>
      
      
      <set name="suspenses" cascade="all" inverse="true" fetch="join"
            outer-join="false" lazy="false">
      <key>
      <column name="CoBankAcctId"></column>
      <column name="Company"></column>
      </key>
      <one-to-many class="nulamda.Bean.SUSPENS" />
      </set>
      
   </class>
</hibernate-mapping>


CompanyBankAccount.java
Code:
package nulamda.Bean;

import java.util.HashSet;
import java.util.Set;

public class CompanyBankAccount {

   private String CoBankAcctNo;
   private String CoBankAcctName;
   private Set<SUSPENS> suspenses = new HashSet<SUSPENS>();
   
   //Variable for composite key class
   private CompanyBankAccount_PK companyBankAccount_PK;
   
   public String getCoBankAcctNo() {
      return CoBankAcctNo;
   }
   public void setCoBankAcctNo(String coBankAcctNo) {
      CoBankAcctNo = coBankAcctNo;
   }
   public String getCoBankAcctName() {
      return CoBankAcctName;
   }
   public void setCoBankAcctName(String coBankAcctName) {
      CoBankAcctName = coBankAcctName;
   }
   public CompanyBankAccount_PK getCompanyBankAccount_PK() {
      return companyBankAccount_PK;
   }
   public void setCompanyBankAccount_PK(CompanyBankAccount_PK companyBankAccount_PK) {
      this.companyBankAccount_PK = companyBankAccount_PK;
   }
   public Set<SUSPENS> getSuspenses() {
      return suspenses;
   }
   public void setSuspenses(Set<SUSPENS> suspenses) {
      this.suspenses = suspenses;
   }


CompanyBankAccount_PK.java
Code:
package nulamda.Bean;

import java.io.Serializable;

public class CompanyBankAccount_PK implements Serializable{

   
   private static final long serialVersionUID = 1L;
   
   private int CoBankAcctId;
   private int Company;
   
   public CompanyBankAccount_PK() {
   // TODO Auto-generated constructor stub
   }   
   
   public int getCoBankAcctId() {
      return CoBankAcctId;
   }
   public void setCoBankAcctId(int coBankAcctId) {
      CoBankAcctId = coBankAcctId;
   }
   public int getCompany() {
      return Company;
   }
   public void setCompany(int company) {
      Company = company;
   }
   
   /*
    * The identifier class must override equals() and hashCode() and implement.
      Serializable. The disadvantage of this approach is quite obviouscode
      duplication.
    * */
   
   @Override
   public boolean equals(Object other) {
      if (this == other)return true;
      if ( !(other instanceof CompanyBankAccount_PK))return false;
      final CompanyBankAccount_PK companyBankAccount_PK = (CompanyBankAccount_PK) other;
      if (!( companyBankAccount_PK.getCoBankAcctId() == getCoBankAcctId()) ) return false;
      if (!( companyBankAccount_PK.getCompany() == getCompany()) ) return false;
      return true;
   }
   
   @Override
   public int hashCode() {
      int result = 0;
      return result;
   }
}


The following code is used to get the result

Code:
import java.util.Iterator;
import java.util.List;

import nulamda.Bean.CompanyBankAccount;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

public class CompanyBabkAccountSupport {

   public void GetCompanyBankAccountDetail(int coBankAcID, int company)
   {
      
      Session session = HibernateSessionFactory.currentSession();
      Transaction transaction = session.beginTransaction();
      Criteria companyBankCriteria = session.createCriteria(CompanyBankAccount.class);
      companyBankCriteria.add(Restrictions.like("CoBankAcctId", coBankAcID));
      companyBankCriteria.add(Restrictions.like("Company", company));
      List<CompanyBankAccount> companyBankAccountList = companyBankCriteria.list();
      System.out.println(" %%%%%%%%%%%%%%%%%%%% List size is  : "+ companyBankAccountList.size());

   }
}


i am getting the following exception

Code:
[b]org.hibernate.QueryException[/b]: [i]could not resolve property[/i]: CoBankAcctId of: nulamda.Bean.CompanyBankAccount
   at org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapping.java:43)
   at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:37)
   at org.hibernate.persister.entity.BasicEntityPersister.getSubclassPropertyTableNumber(BasicEntityPersister.java:1124)
   at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:33)
   at org.hibernate.persister.entity.BasicEntityPersister.toColumns(BasicEntityPersister.java:1104)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:402)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumnsUsingProjection(CriteriaQueryTranslator.java:368)
   at org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:42)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:313)
   at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:92)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1228)
   at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:299)
   at nulamda.Support.CompanyBabkAccountSupport.GetCompanyBankAccountDetail(CompanyBabkAccountSupport.java:23)
   at nulamda.Action.SuspensSelectingAction.SuspensActionMethod(SuspensSelectingAction.java:33)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at com.sun.faces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:126)
   at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:72)
   at org.apache.struts.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:137)
   at javax.faces.component.UICommand.broadcast(UICommand.java:312)
   at javax.faces.component.UIData.broadcast(UIData.java:657)
   at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:267)
   at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:381)
   at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:75)
   at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
   at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:90)
   at javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
   at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
   at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
   at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
   at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
   at java.lang.Thread.run(Thread.java:595)

_________________
Vinoth


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.