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.  [ 11 posts ] 
Author Message
 Post subject: Join Problem
PostPosted: Wed Sep 28, 2011 4:20 am 
Newbie

Joined: Tue Sep 27, 2011 4:11 am
Posts: 14
hey all,

i have these two mapping XML`s for Inventory and ProductInformation tables respectively:

Code:

    <?xml version="1.0"?> 
    <!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
       
    <hibernate-mapping> 
    <class name="tpos.Inventory" table="inventory"> 

       <id name="allocationId" type="int" column="AllocationId" > 
       <generator class="native"/> 
      </id> 
       
       
     <property name="allocatedLocation"> 
      <column name="AllocatedLocation"/> 
      </property> 
   
       
      <property name="sellable_Notsellable"> 
       <column name="Sellable_NotSellable"/> 
      </property> 
       
      <property name="sellablefromDate"> 
      <column  name="SellableFromDate" />   
      </property> 
       
      <property name="sellableTill"> 
      <column name="SellableTill" /> 
      </property> 
     
     
      <many-to-one name="productID" class="tpos.ProductInformation" column="productid" not-null="false" cascade="all" unique="true"  /> 

</class>
</hibernate-mapping>



Code:

    <?xml version="1.0"?> 
    <!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
       
    <hibernate-mapping> 
     <class name="tpos.ProductInformation" table="ProductInformation" > 
       
       
       <id name="productId" type="int" column="ProductId" > 
       <generator class="native"/> 
      </id> 
       
      <property name="productCode"> 
      <column name="ProductCode"/> 
      </property> 
 
      <property name="productName"> 
       <column name="ProductName" /> 
      </property> 
       
      <property name="createdDate"> 
       <column name="CreatedDate"/> 
      </property> 
       
      <property name="expiryDate"> 
      <column  name="ExpiryDate" />   
      </property> 
       
      <property name="statuS"> 
      <column name="Status" /> 
      </property> 
       
       
      <property name="sellablE"> 
      <column name="Sellable" /> 
      </property> 
     
       
      <property name="purchaseDate"> 
      <column name="PurchaseDate" /> 
      </property> 
       
      <property name="barcodeNo"> 
      <column name="BarcodeNo" /> 
      </property> 
       
      <property name="updationDate"> 
      <column name="UpdationDate" /> 
      </property> 
       
      <property name="createdBy"> 
      <column name="CreatedBy" /> 
      </property> 
       
      <property name="updatedBy"> 
      <column name="UpdatedBy" /> 
      </property> 
       
      <property name="productCompType"> 
      <column name="ProductCompType" /> 
      </property> 
     </class> 
    </hibernate-mapping> 





Now, i want to call a join statement on both of them using HQL..... so i defined a relationship in inventory XML and called this query

select pi.productCode , pi.productName , pi.barcodeNo , inv.saleunitprice , inv.quantity from Inventory inv join inv.ProductInformation pi


but it always fails with this error:

could not resolve property:ProductInformation of: tpos.Inventory


Can you guys please help me this?


Top
 Profile  
 
 Post subject: Re: Join Problem
PostPosted: Wed Sep 28, 2011 4:55 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You have defined the relation as

Code:
<many-to-one name="productID" ...



which means that your join should be something like:

Code:
from Inventory inv join inv.productID pi


Top
 Profile  
 
 Post subject: Re: Join Problem
PostPosted: Wed Sep 28, 2011 6:25 am 
Newbie

Joined: Tue Sep 27, 2011 4:11 am
Posts: 14
hey thanks again man, it solved that problem but now it says this:

could not resolve property: saleunitprice of: tpos.Inventory

saleunitprice is a field in table Inventory.


Now what should i do?
:( please nordborg man you helped me before...... Can you please solve this problem for me... i have been stuck in it since 3 days???


Top
 Profile  
 
 Post subject: Re: Join Problem
PostPosted: Wed Sep 28, 2011 6:32 am 
Newbie

Joined: Tue Sep 27, 2011 4:11 am
Posts: 14
Phew got it working.......... man nordborg your the master dude........ Thanks


Top
 Profile  
 
 Post subject: Re: Join Problem
PostPosted: Wed Sep 28, 2011 6:52 am 
Newbie

Joined: Tue Sep 27, 2011 4:11 am
Posts: 14
Nop the query works i.e i can see it on the console but it gives me a classcastexception:

[Ljava.lang.Object; cannot be cast to tpos.Inventory

now what should i do?


Top
 Profile  
 
 Post subject: Re: Join Problem
PostPosted: Wed Sep 28, 2011 7:00 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
When you specify each property in the SELECT list Hibernate will return each row as an Object[], so row[0] is pi.productCode, row[1] is pi.productName and so on. If you want your query to return entity objects you should only SELECT that entity. For example:

Code:
select inv fromInventory inv


If you want to fetch product information with the same query, use a JOIN FETCH:

Code:
select inv fromInventory inv join fetch inv.productID


Top
 Profile  
 
 Post subject: Re: Join Problem
PostPosted: Wed Sep 28, 2011 7:15 am 
Newbie

Joined: Tue Sep 27, 2011 4:11 am
Posts: 14
i dont understand nordborg........ basically want i want is to get the resultant data and show it in jtable.... so what type of query should i use???


Top
 Profile  
 
 Post subject: Re: Join Problem
PostPosted: Wed Sep 28, 2011 7:17 am 
Newbie

Joined: Tue Sep 27, 2011 4:11 am
Posts: 14
i need to get data from both the tables and show it in a jtable........ so that`s why i am using join


Top
 Profile  
 
 Post subject: Re: Join Problem
PostPosted: Wed Sep 28, 2011 7:45 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I don't know anything about jtable.


Top
 Profile  
 
 Post subject: Re: Join Problem
PostPosted: Wed Sep 28, 2011 7:58 am 
Newbie

Joined: Tue Sep 27, 2011 4:11 am
Posts: 14
its ok......

Here`s what i did:

i modified the query to this :

select inv from Inventory inv join fetch inv.productID

but this happened:

IllegalArgumentException in class: tpos.Inventory, setter method of property: productID
expected type: java.lang.Integer, actual value: tpos.ProductInformation
org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of tpos.Inventory.productID


Top
 Profile  
 
 Post subject: Re: Join Problem
PostPosted: Wed Sep 28, 2011 8:25 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You set/get methods that are related with the <many-to-one> association should work with objects, not id values.

Eg.

Code:
private ProductInformation pi;
public void setProductID(ProductInformation pi) {
  this.pi = pi;
}
public ProductInformation getProductID() {
   return pi;
}


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