-->
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.  [ 2 posts ] 
Author Message
 Post subject: Querying an uni-directional one-to-many association
PostPosted: Fri Oct 14, 2005 9:01 am 
Newbie

Joined: Fri Oct 14, 2005 8:37 am
Posts: 10
Location: University of Edinburgh
Hello,

I am using Hibernate in an e-learning system to make learner models
persistent (extracts of the class definitions and mapping to a
relational model are included below). Development have been a bit
painful --since I am not and expert on databases nor the Hibernate
manual is particularly clear at points-- but have been moving for a
while: models can be stored and retrieved, as well as individual
beliefs. But now I am stuck with a particular query:

Code:
select {belief.*} from belief, bdesc
where belief.lmodel_id = 'JorgeNegrete'
  and belief.bdesc_id = bdesc.id
  and bdesc.competencyId = 'think'

(Not syntatically correct but gives you the idea)

I think it could be solved with HQL, but requires adding private fiels
to belief for lmodel_id and bdesc_id, yet I don't quite like this
approach. I tried SQL, but then I have problems understanding what the
arguments of addJoin are. Criteria search? Can't see how. Filters?
Haven't found examples with Map (instead of Set or List) I can adjust
to my case.

Please let me know if you have any suggestions.

Regards,

Rafael


Extracts of class definitions

Code:
public class LearnerModel {
    String learnerId;
    Map beliefs;
    // ...
}

public class Belief {
    private List evidence;
    private int news;
    MassDistribution massDist;
    // ...
}

public class BeliefDescriptor {

    private String metacogId;
    private String motivationId;
    private String affectId;
    private String competencyId;
    private String capeId;
    private String domainId;
    // ...
}


LearnerModel mapping

Code:
<hibernate-mapping package="org.activemath.xlm.model">
  <class name="LearnerModel" table="LModel">
    <id name="learnerId" column="id">
      <generator class="assigned"/>
    </id>
    <map name="beliefs" cascade="all,delete-orphan">
      <key column="LModel_id" not-null="true" />
      <map-key-many-to-many class="BeliefDescriptor" column="BDesc_id" />
      <one-to-many class="Belief"/>
    </map>
  </class>
</hibernate-mapping>


Belief mapping:

Code:
<hibernate-mapping default-access="field"
  package="org.activemath.xlm.model">
  <class name="Belief" table="Belief" >
    <id type="long">
      <generator class="native" />
    </id>
    <component class="MassDistribution" name="massDist">
      <primitive-array name="massDist" table="MassDist">
   <key column="Belief_id"/>
   <list-index column="lSetOrd"/>
   <element column="bpa" type="double">
   </element>
      </primitive-array>
    </component>
    <list name="evidence" cascade="all,delete-orphan">
      <key column="Belief_id" not-null="true"/>
      <list-index column="n" />
      <one-to-many class="Evidence"/>
    </list>
  </class>
</hibernate-mapping>


BeliefDescriptor mapping

Code:
<hibernate-mapping>
  <class name="org.activemath.xlm.model.BeliefDescriptor" lazy="false"
    table="BDesc">
    <id type="long" >
      <generator class="native" />
    </id>

    <properties name="coordinates" unique="true">
      <property name="metacogId" access="field" not-null="true"/>
      <property name="motivationId" access="field" not-null="true"/>
      <property name="affectId" access="field" not-null="true"/>
      <property name="competencyId" access="field" not-null="true"/>
      <property name="capeId" access="field" not-null="true"/>
      <property name="domainId" access="field" not-null="true"/>     
    </properties>
  </class>
</hibernate-mapping>


Evidence mapping

Code:
<hibernate-mapping package="org.activemath.xlm.model">
  <class name="Evidence" table="Evidence" >
    <id type="long">
      <generator class="native"/>
    </id>
    <property name="eventId" />
    <component class="MassDistribution" name="mass" >
      <primitive-array name="massDist" access="field"
   table="EventInterp">
   <key not-null="true" column="Evidence_id"/>
   <list-index column="lSetOrd"/>
   <element column="bpa" type="double" />
      </primitive-array>
    </component>
  </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Solution found
PostPosted: Sat Oct 15, 2005 7:05 pm 
Newbie

Joined: Fri Oct 14, 2005 8:37 am
Posts: 10
Location: University of Edinburgh
Hello,

For the sake of completeness, this is what I have found useful: I included references in beliefs to the learner models they belong to and the descriptors they correspond to. This makes possible (to me, though it could be possible otherwise) to define bidirectional associations in the
database that can be exploited in HQL. The original (pseudo SQL) query

Code:
select {belief.*} from belief, bdesc
where belief.lmodel_id = 'JorgeNegrete'
  and belief.bdesc_id = bdesc.id
  and bdesc.competencyId = 'think'


becomes, in HQL,

Code:
select belief
from Belief as belief,
     LearnerModel as model,
     BeliefDescriptor as bdesc
where belief.lModel = model
  and belief.descriptor = bdesc
  and model.learnerId = 'JorgeNegrete'
  and bdesc.competencyId = 'think'


Regards,

Rafael


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