-->
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.  [ 6 posts ] 
Author Message
 Post subject: Cannot get correct row from table
PostPosted: Wed Feb 11, 2009 12:25 pm 
Newbie

Joined: Wed Feb 11, 2009 12:10 pm
Posts: 3
hibernate 3.

I use many-to-many relationship, I have Companies and Administrators for Companies. Each Admin can have many companies and companies can have many admins:
Admins:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="data">
  <class name="data.USER" table="ADMINUSER" optimistic-lock="none">
    <id name="id" type="integer" column="U_ID">
      <generator class="hilo"/>
    </id>
    <property name="name" type="string" column="U_NAME"/>
    <property name="username" type="string" column="U_USERNAME"/>
     ...
    <set name="companies" table="USERJOINCOMPANY">
    <key column="U_ID"/>
    <many-to-many column="C_ID" class="data.COMPANY"/>
    </set>
  </class>
</hibernate-mapping>


Companies:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="data">
  <class name="data.COMPANY" table="COMPANY">
    <id name="id" type="integer" column="C_ID">
      <generator class="hilo"/>
    </id>
    <property name="name" type="string" column="C_NAME" length="50"/>
....
    <set name="users" table="USERJOINCOMPANY" cascade="all">
    <key column="C_ID"/>
    <many-to-many column="U_ID" class="data.USER"/>
    </set>
  </class>
</hibernate-mapping>


in my backbean I'am getting company list which belogs to admin:
Code:
    public List getCom() {   
        Transaction tx = null;         
        Session session = HibernateUtil.currentSession();       
        try {                   
        tx = session.beginTransaction();
        USER auser = (USER) session.load(USER.class, 1);
        AllComList = new ArrayList(auser.getCompanies());   
            tx.commit();            }
        catch (HibernateException e)
        {                       
            e.printStackTrace();                   
            if (tx != null && tx.isActive())                               
                tx.rollback();
            }
        finally{
            HibernateUtil.closeSession();
        }
        return AllComList;
    }


links com to ADF table.
So company list is populated, but when I select needed company, another company from list is selected. Sometimes correct one is selected, sometimes no.
Ahother hibernate queries works fine (also with ADF table).

What could be a problem?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2009 2:47 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Your mappings and code seems to be ok, but I don't understand what you mean with:

Quote:
So company list is populated, but when I select needed company, another company from list is selected. Sometimes correct one is selected, sometimes no.

Can you explain what you mean with "select needed company"?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2009 4:49 pm 
Newbie

Joined: Wed Feb 11, 2009 12:10 pm
Posts: 3
nordborg wrote:
Your mappings and code seems to be ok, but I don't understand what you mean with:

Quote:
So company list is populated, but when I select needed company, another company from list is selected. Sometimes correct one is selected, sometimes no.

Can you explain what you mean with "select needed company"?


I mean that I'am using Company List as value for my table, there I can click on corresponding Company name and get this company data. Problem is that Iterator returns incorrect row (another company data is shown).

I tried to use same code, but with simple query:
Code:
"select c from COMPANY as c"

and in that case all works fine.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2009 5:14 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
I mean that I'am using Company List as value for my table, there I can click on corresponding Company name and get this company data. Problem is that Iterator returns incorrect row (another company data is shown).


But you have not shown the code that does this. Seems like the problem should be with that code.

What do you mean with 'Iterator returns incorrect row'? An iterator returns the items that are the list. Is the list incorrect to begin with? Eg. if you do 'System.out.println(AllComList)' in your getCom() method does the list contain the companies that you expect to be there?

Or... are you calling the getCom() method more than once and the returned lists doesn't contain the companies in the same order? That is expected since you are populating the list from a set and a set has no specific order by default. If you want the list to be sorted in a specific way you can sort it yourself or specify either the 'sort' or 'order-by' attribute for the <set> mapping. For example:

Code:
<set
  name="companies"
  table="USERJOINCOMPANY"
  order-by="name">
...
</set>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2009 4:05 am 
Newbie

Joined: Wed Feb 11, 2009 12:10 pm
Posts: 3
nordborg wrote:
Quote:
I mean that I'am using Company List as value for my table, there I can click on corresponding Company name and get this company data. Problem is that Iterator returns incorrect row (another company data is shown).


But you have not shown the code that does this. Seems like the problem should be with that code.

What do you mean with 'Iterator returns incorrect row'? An iterator returns the items that are the list. Is the list incorrect to begin with? Eg. if you do 'System.out.println(AllComList)' in your getCom() method does the list contain the companies that you expect to be there?

Or... are you calling the getCom() method more than once and the returned lists doesn't contain the companies in the same order? That is expected since you are populating the list from a set and a set has no specific order by default. If you want the list to be sorted in a specific way you can sort it yourself or specify either the 'sort' or 'order-by' attribute for the <set> mapping. For example:

Code:
<set
  name="companies"
  table="USERJOINCOMPANY"
  order-by="name">
...
</set>


Great, it works now!

So it means that I should always use order by, for Set?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2009 4:22 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
So it means that I should always use order by, for Set?


If your code depends on the order. I have never had any need for it myself.


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