tags:

views:

75

answers:

2

I want to add a (where) condition in hibernate mapping file when fetching, how could i do that for example fetch="select" where id != 1 ,am not using annotations also, so please specify within the mapping (hbm) files.

A: 

You could perhaps define a <filter> in the mapping file, and attach it to the class mapping definition. See the docs for examples.

skaffman
it needs a lot of configuration, and also to be enabled from code, is there another way to do that?
Amr Faisal
A: 

According to the Hibernate documentation, you can specify a where clause when mapping a collection:

6.2. Collection mappings

(...) The <map> element is representative:

<map
    name="propertyName"                                         (1)
    table="table_name"                                          (2)
    schema="schema_name"                                        (3)
    lazy="true|extra|false"                                     (4)
    inverse="true|false"                                        (5)
    cascade="all|none|save-update|delete|all-delete-orphan|delet(6)e-orphan"
    sort="unsorted|natural|comparatorClass"                     (7)
    order-by="column_name asc|desc"                             (8)
    where="arbitrary sql where condition"                       (9)
    fetch="join|select|subselect"                               (10)
    batch-size="N"                                              (11)
    access="field|property|ClassName"                           (12)
    optimistic-lock="true|false"                                (13)
    mutable="true|false"                                        (14)
    node="element-name|."
    embed-xml="true|false" 
>

    <key .... />
    <map-key .... />
    <element .... />
</map>

(...)

(9) where (optional): specifies an arbitrary SQL WHERE condition that is used when retrieving or removing the collection. This is useful if the collection needs to contain only a subset of the available data.

This is also supported at the <class> level.

Pascal Thivent
Could you please give me a simple example using it, i tried to search for clear example but i can't find, am mapping a set and want to run clause WHERE ID != 1, regards
Amr Faisal
@Amr Well, what do you get when using `where="id != 1"`, what query gets generated?
Pascal Thivent
ok all what i gonna say is you helped me for the second time in row, your answer is just perfect and it was my fault applying it, many thanks for your support
Amr Faisal