Announcement Announcement Module
Collapse
No announcement yet.
hibernate criteria api and <composite-id> Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • hibernate criteria api and <composite-id>

    Hello

    I have a little problem, I try to do select with a hibernate criteria api.

    All the time my program print wrong, if i try to do select table where i have been used <composite-id> -tag. Other words it just print first row on database twice (i have a two rows on table where i try to do select).

    If i left that table out of select then my program works fine, i just don't get what is wrong. I have been used on sefFetchMode.JOIN on criteria. My map file looks like this:

    Code:
    <composite-id name="id" class="ApplId">
    <key-property name="applJobToApplicationId" type="java.lang.Integer">
                    <column name="APPL_ID" />
                </key-property>
                <key-property name="applJobToJobId" type="java.lang.Integer">
                    <column name="JOB_ID" />
                </key-property>
            </composite-id>
    <many-to-one name="applJobToAppl" class="Appl" update="false" insert="false">
                <column name="APPL_ID" not-null="true" />
            </many-to-one>
            <many-to-one name="Status" class="Status">
                <column name="STATUS_ID" not-null="true" />
            </many-to-one>
            <many-to-one name="Potential" class="Potential">
                <column name="POTENTIAL_ID" not-null="true" />
            </many-to-one>
            <many-to-one name="Job" class="Job" update="false" insert="false" fetch="select">
                <column name="JOB_ID" not-null="true" />
            </many-to-one>
    Can somebody help me on this?
    Last edited by kokkonen; Sep 17th, 2007, 02:20 AM. Reason: more information

  • #2
    And how does your criteria look? Your mapping looks fine.

    Comment


    • #3
      My criteria looks like this:

      Code:
          public List readCriteria(int start,int page,){
         sessio=this.getHibernateTemplate().getSessionFactory().openSession(); 
      detachedCriteria =DetachedCriteria.forClass(Class.forName(dataObjectClass));
                  Criteria criteria = detachedCriteria.getExecutableCriteria(this.sessio);
              
              
              //set fetch modes
              criteria.setFetchMode("Job", FetchMode.JOIN);
              criteria.setFetchMode("Customer", FetchMode.JOIN);
              criteria.setFetchMode("WorkPlace", FetchMode.JOIN);
              
              //sort by this property
              criteria.createAlias(Job, "orderWith");
              
              //sort asc or desc
              if(sort.equals("asc")){
                  criteria.addOrder(Order.asc("orderWith."+column));   
              } if (sort.equals("desc")){
                  criteria.addOrder(Order.desc("orderWith."+column)); 
              }
              
              //do search and return list    
              return getHibernateTemplate().findByCriteria(detachedCriteria,start, page);  
          }
      If i take that first setFectMode away from criteria then it works fine.

      Comment


      • #4
        Why are you creating a DetachedCriteria, set a bunch of stuff on the Criteria and in the end use the DetachedCriteria again ?!

        It also appears as if you are keeping state inside your class DON'T do that, multiple threads will overwrite the state in your class.

        Code:
        public List readCriteria(int start,int page, String sort) {
            DetachedCriteria criteria =DetachedCriteria.forClass(Class.forName(dataObjectClass));
            
            //set fetch modes
            criteria.setFetchMode("Job", FetchMode.JOIN);
            criteria.setFetchMode("Customer", FetchMode.JOIN);
            criteria.setFetchMode("WorkPlace", FetchMode.JOIN);
            
            //sort by this property
            criteria.createAlias(Job, "orderWith");
            
            //sort asc or desc
            if(sort.equals("asc")){
                criteria.addOrder(Order.asc("orderWith."+column));   
            } if (sort.equals("desc")){
                criteria.addOrder(Order.desc("orderWith."+column)); 
            }
            
            //do search and return list    
            return getHibernateTemplate().findByCriteria(detachedCriteria,start, page);  
        }
        Furthermore I think it is because of your mapping, you map a column twice (which is well justified in your cause) it might be that the query returns 2 rows due to the way the query is being build.

        Comment

        Working...
        X