-->
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.  [ 4 posts ] 
Author Message
 Post subject: Mapping with where condition and parameter
PostPosted: Wed Apr 08, 2009 3:57 am 
Newbie

Joined: Mon Mar 09, 2009 11:21 am
Posts: 11
Hi,
i want to add a "where" condition to a mapping... I have an environment Table with a m:n realtion to a Server Table(bidirectional) and a 1:n relation to a History table (uni) wich also have a 1:n relation to the server table.(bidirectional) (See mapping files below)

If i load environment objects I want that my server objects are always loading only the history data which are related to my environment.
A normal subselect would look like this:

Code:
select *    from
        ecm.ecm_history
    where   environment_id = ?
    and  and ecmhistori0_.server_id=?


so i wanted to apply a where condition to the bag mapping:
Code:
<bag  where="environment_id = ?"
      name="ecmHistories" lazy="false" inverse="true">                       
            <key>
                <column name="server_id" not-null="true" />
            </key>
           
            <one-to-many class="com.basfits.ecm.core.history.EcmHistory" />
        </bag>


Of course it dont work because Hibernate don't know wich parameter it should assign. Can you help me or do you have another solution which works?

Hibernate version:
3.3.2.GA
Mapping documents:
Environment:
Quote:
<hibernate-mapping>
<class name="com.basfits.ecm.core.environmentData.EcmEnvironmentData" table="ecm_environment_data" catalog="ecm">
<id name="persistentId" type="java.lang.Long">
<column name="environment_id" />
<generator class="identity" />
</id>
<many-to-one cascade="all" not-null="false" lazy="false" name="ecmRuntimeEnvironment" class="com.basfits.ecm.core.environmentRuntime.EcmRuntimeEnvironment" fetch="select">
<column name="runtime_environment_Id" />
</many-to-one>
<property name="name" type="java.lang.String">
<column name="name" length="50" />
</property>
<property name="context" type="java.lang.String">
<column name="context" length="250" />
</property>
<property name="version" type="java.lang.Float">
<column name="version" precision="12" scale="0" />
</property>
<bag lazy="false" name="ecmServers" table="ecm_server_environment_data" cascade="all">
<key column="environment_data_id" />
<many-to-many class="com.basfits.ecm.core.server.EcmServer" column="server_id" />
</bag>
<bag name="ecmClusters" cascade="all-delete-orphan" lazy="false">
<key>
<column name="environment_data_id" />
</key>
<one-to-many class="com.basfits.ecm.core.cluster.EcmCluster" />
</bag>
</class>
</hibernate-mapping>


Server

Quote:
<hibernate-mapping>
<class name="com.basfits.ecm.core.server.EcmServer" table="ecm_server" catalog="ecm">
<id name="persistentId" type="java.lang.Long">
<column name="server_id" />
<generator class="identity" />
</id>
<many-to-one name="ecmCluster" update="true" lazy="false" class="com.basfits.ecm.core.cluster.EcmCluster" fetch="select">
<column name="cluster_id" />
</many-to-one>
<property name="name" type="java.lang.String">
<column name="name" length="50" not-null="true" />
</property>
<property name="description" type="java.lang.String">
<column name="description" length="65535" />
</property>
<property name="url" type="java.lang.String">
<column name="url" length="250" not-null="true" />
</property>
<property name="port" type="java.lang.Integer">
<column name="port" not-null="true" />
</property>
<bag name="ecmPortals" table="ecm_portal_server" cascade="all" lazy="false">
<key column="server_id" not-null="true" />
<many-to-many class="com.basfits.ecm.core.portal.EcmPortal" column="portal_id"/>
</bag>
<bag name="ecmEnvironmentDatas" lazy="false" inverse="true" table="ecm_server_environment_data">
<key column="server_id" />
<many-to-many class="com.basfits.ecm.core.environmentData.EcmEnvironmentData" column="environment_data_id"/>
</bag>
<bag where="environment_id = ?"
name="ecmHistories" lazy="false" inverse="true">
<key>
<column name="server_id" not-null="true" />
</key>

<one-to-many class="com.basfits.ecm.core.history.EcmHistory" />
</bag>


</class>
</hibernate-mapping>


History

Quote:
<hibernate-mapping>
<class name="com.basfits.ecm.core.history.EcmHistory" table="ecm_history" catalog="ecm">
<id name="persistentId" type="java.lang.Long">
<column name="history_id" />
<generator class="identity" />
</id>
<many-to-one not-null="false" name="ecmEnvironmentData" class="com.basfits.ecm.core.environmentData.EcmEnvironmentData" fetch="select">
<column name="environment_id" />
</many-to-one>
<many-to-one name="ecmServer" class="com.basfits.ecm.core.server.EcmServer" fetch="select">
<column name="server_id" not-null="true" />
</many-to-one>
<property name="heap" type="java.lang.Float">
<column name="heap" precision="12" scale="0" />
</property>
<property name="memory" type="java.lang.Float">
<column name="memory" precision="12" scale="0" />
</property>
<property name="sessions" type="java.lang.Integer">
<column name="sessions" />
</property>
<property name="sessionsIdle" type="java.lang.Integer">
<column name="sessionsIdle" />
</property>
<property name="time" type="java.util.Date">
<column name="time" length="19"/>
</property>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 08, 2009 6:45 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You can use a <filter> as described in the Hibernate documentation http://www.hibernate.org/hib_docs/v3/re ... te-filters

For example:
Code:
<filter-def name="envFilter">
    <filter-param name="env" type="long"/>
</filter-def>

...

<bag 
      name="ecmHistories" lazy="false" inverse="true">       
        <filter name="envFilter" condition="environment_id = :env" />
            <key>
                <column name="server_id" not-null="true" />
            </key>
           
            <one-to-many class="com.basfits.ecm.core.history.EcmHistory" />
        </bag>


And then when you want to use the filter:

Code:
session.enableFilter("envFilter").setParameter("env", envId);


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 08, 2009 7:05 am 
Newbie

Joined: Mon Mar 09, 2009 11:21 am
Posts: 11
Thanks nordborg, but i already tried to implement a filter solution. I only could enable them in my environment DAO, but it dont works. Here is the generated SQL Statement:
Code:
select
        ecmenviron0_.server_id as server1_1_,
        ecmenviron0_.environment_data_id as environm2_1_,
        ecmenviron1_.environment_id as environm1_6_0_,
        ecmenviron1_.runtime_environment_Id as runtime2_6_0_,
        ecmenviron1_.name as name6_0_,
        ecmenviron1_.context as context6_0_,
        ecmenviron1_.version as version6_0_
    from
        ecm_server_environment_data ecmenviron0_
    left outer join
        ecm.ecm_environment_data ecmenviron1_
            on ecmenviron0_.environment_data_id=ecmenviron1_.environment_id
    where
        ecmenviron0_.server_id=?
Hibernate:
    select
        ecmenviron0_.server_id as server1_1_,
        ecmenviron0_.environment_data_id as environm2_1_,
        ecmenviron1_.environment_id as environm1_6_0_,
        ecmenviron1_.runtime_environment_Id as runtime2_6_0_,
        ecmenviron1_.name as name6_0_,
        ecmenviron1_.context as context6_0_,
        ecmenviron1_.version as version6_0_
    from
        ecm_server_environment_data ecmenviron0_
    left outer join
        ecm.ecm_environment_data ecmenviron1_
            on ecmenviron0_.environment_data_id=ecmenviron1_.environment_id
    where
        ecmenviron0_.server_id=?


...

    select
        ecmhistori0_.server_id as server3_1_,
        ecmhistori0_.history_id as history1_1_,
        ecmhistori0_.history_id as history1_8_0_,
        ecmhistori0_.environment_id as environm2_8_0_,
        ecmhistori0_.server_id as server3_8_0_,
        ecmhistori0_.heap as heap8_0_,
        ecmhistori0_.memory as memory8_0_,
        ecmhistori0_.sessions as sessions8_0_,
        ecmhistori0_.sessionsIdle as sessions7_8_0_,
        ecmhistori0_.time as time8_0_
    from
        ecm.ecm_history ecmhistori0_
    where
        ecmhistori0_.server_id=?
    order by
        time DESC


Here the new mapping:
Code:
<hibernate-mapping>
    <class name="com.basfits.ecm.core.server.EcmServer" table="ecm_server" catalog="ecm">

...


<bag order-by="time DESC"
      name="ecmHistories" lazy="false" inverse="true">                            
            <key>
                <column name="server_id" not-null="true" />
            </key>             
            <one-to-many class="com.basfits.ecm.core.history.EcmHistory" />
            <filter name="envFilter" condition="environment_id = :env" />
        </bag>       
    </class>
    <filter-def name="envFilter">
       <filter-param name="env" type="long"/>
   </filter-def>
</hibernate-mapping>


And here is how I call the find method:
Code:
public EcmEnvironmentData findById(Long id){
      getSession().enableFilter("envFilter").setParameter("env", id);
      return (EcmEnvironmentData) getSession().get(EcmEnvironmentData.class, id);
   }


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 08, 2009 10:32 am 
Newbie

Joined: Mon Mar 09, 2009 11:21 am
Posts: 11
I tried to implement a custom load, but this doesn't work to because of the m:n relation between Environment & Server.

So I solved the problem with deleting the mapping between Server & History and load the data in a second step... quick 'n dirty solution...


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