0

I have 2 tables 'Stock' and 'stockdaily' Need to fetch all the

"stock_template" from stock where stock_status=true

and stock.stock_template is the FK to Stockdaily.stock_template.

Right now I am doing this

I wrote in Stockdaily.hbm.xml

<many-to-one class="StockDTO" column="stock_template" name="templates"/>

In StockdailyDTO.java

private StockDTO templates;

In StockDTO

private Boolean stocktemplate;

In Stock.hbm.xml

<property name="stockStatus" type="java.lang.Boolean">
        <column name="stock_Status"  />
    </property>

but whenever I try to fetch data on the basis of restriction stock_statua=true

criteria = session.createCriteria(stockDailyDTO.class).add( Restrictions.eq("templates.stockStatus", true) );   

System gives an exception that

org.hibernate.QueryException: could not resolve property: templates.stockStatus of: StockdailyDTO

Can anyone tell me what else should I do to resolve this issue?

Just_another_developer
  • 5,737
  • 12
  • 50
  • 83
  • The way you're querying your entities you're goint to obtain "StockDaily" entities not "Stock" objects, is this correct? in your first parragraph is seems that you want to obtain "Stock" objects. – kothvandir Sep 06 '12 at 06:36
  • these two tables are related stock.stocktemplate is a FK to stockDaily.stocktemplate, I just want to query all the templates that have stock.stock_status=true . To relate these two tables I defined relationship in stockDaily.hbm.xml as mentioned above. I am using these stocktemplates on my Stockdaily's screens. – Just_another_developer Sep 06 '12 at 06:43
  • If you want to navigate from StockDaily to Stock entities to filter by state attribute, your relation should be mapped as one-to-many in the StockDaily Class. The way you defined it, you're able to navigate only from Stock to StockDaily. – kothvandir Sep 06 '12 at 06:52
  • It was wroking fine before ..It was fetching all the templates from Stock but now I just have to add Status="true" condition and when I am adding, it is throwing exception. – Just_another_developer Sep 06 '12 at 06:57
  • More over .. the relationship is like ... "Many Stock_daily can have 1 Stock.template" so it should be "many-to-one" relation ship. – Just_another_developer Sep 06 '12 at 06:59
  • In SQL you define just one relation, a 1-n relation based on a FK. But in hibernate, the same schema relation can be mapped one-to-many or many-to-one depending on how you want to navigate your relation: http://stackoverflow.com/questions/4601703/difference-between-one-to-many-and-many-to-one-relationship. Of course you can also define both. – kothvandir Sep 06 '12 at 08:11

0 Answers0