-->
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.  [ 15 posts ] 
Author Message
 Post subject: Is this a synchronization problem that im facing?
PostPosted: Thu Dec 02, 2004 5:53 am 
Newbie

Joined: Fri Nov 19, 2004 6:35 am
Posts: 19
I have a function inside my DAO that goes like this:

Code:
public static List getNamedQueryObjectById( String namedQuery, String id ){
   List list = null;

   try{
      Query q = CcSession.currentSession().getNamedQuery( namedQuery );
      q.setString( "id", id );
         
      list = q.list();

   }catch( HibernateException he ){
      logger.error( "HibernateException: " + he.getLocalizedMessage());
      he.printStackTrace();
   }
      
   return list;
}


And a separate class calls the above function like this:

Code:
     List preqList = DAOObject.getNamedQueryObjectById( namedQueryStr, clientId );
                    
     if( preqList != null ){
        for( Iterator i=preqList.iterator(); i.hasNext();){        
           BalanceHistory b = (BalanceHistory)i.next();
           out.print( "[" );
           out.print( "\"" + Utility.showString( b.getProductName()) + "\", " );
           out.print( "\"" + Utility.showString( b.getBrandName()) + "\", " );
           out.print( "\"" + Utility.showString( b.getQuantity()) + "\", " );
           out.print( "\"" + Utility.showString( b.getCommonType()) + "\", " );
           out.print( "\"" + Utility.showString( b.getAppraisedValue())  + "\", " );
           out.print( "\"" + Utility.showString( b.getBuyValue())  + "\", " );
           out.print( "\"" + Utility.showString( b.getAppraisedAmount())  + "\", " );
           out.print( "\"" + Utility.showString( b.getProfitValue())  + "\", " );          
           out.print( i.hasNext() ? "," : "" );          
        }
     }


My problem is:
When it is displayed, the data that was shown is only the first item which is printed list.size() times!!.

please help. im suspecting synchronization problem...

thanks in advance.


Top
 Profile  
 
 Post subject: View
PostPosted: Fri Dec 03, 2004 3:02 am 
Newbie

Joined: Fri Nov 19, 2004 6:35 am
Posts: 19
seems like everyone is puzzled about this in as much as I do!!!..

well, just wondering if this would make a difference but...

the BalanceHistory object is being mapped with a VIEW NOT A TABLE.

here's the XML mapping:
Code:
    <class name="com.test.hibernate.BalanceHistory" table="vw_BalanceHistory">
        <cache usage="read-only"/>
       <id name="clientId" column="clientId">
          <generator class="assigned" />
       </id>
        <property name="appraisedAmount" column="amount"/>
        <property name="appraisedValue" column="avalue"/>
        <property name="brandName" column="brandName"/>
        <property name="buyValue" column="buyPrice"/>
        <property name="commonType" column="type"/>
        <property name="currentPlacement" column="placement"/>
        <property name="productName" column="productName"/>
        <property name="profitValue" column="gain"/>
        <property name="quantity" column="quantity"/>
    </class>

Please help, im stucked in here!. :((

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 3:26 am 
Newbie

Joined: Fri Nov 19, 2004 6:35 am
Posts: 19
Paging hibernate creators. Is this a bug or what?...

since seems like its so puzzling to analyze what happened, i decided to do a JDBC approach to retrieve the data in the view like this...

Code:
try{
   Session s = DemoSession.currentSession();

   Query q = s.getNamedQuery( "com.test.hibernate.BalanceHistory.getById" );
   q.setString( "id", "101104" );
         
   List l = Collections.synchronizedList( q.list());
           
   System.out.println( "---- Using Hibernate Approach ------");           
   for( Iterator i=l.iterator();i.hasNext();){
      BalanceHistory bh = (BalanceHistory)i.next();
               
       System.out.println( "Client ID --> " + bh.getClientId());
       System.out.println( "Name --> " + bh.getBrandName());
       System.out.println( "Value --> " + bh.getBuyValue());
        System.out.println( "-------------------------------" );
   }
           
   System.out.println( "---- Using JDBC Approach ------");           
   Connection c = s.connection();
   Statement st = c.createStatement();
   ResultSet r = st.executeQuery( "select * from vw_BalanceHistory where clientId = 101104" );
           
   while( r.next()){
   System.out.println( "Client ID --> " + r.getString( "clientId" ));
   System.out.println( "Name --> " + r.getString( "brandName" ));
   System.out.println( "Value --> " + r.getString( "buyPrice" ));
   System.out.println( "-------------------------------" );               
   }
           
   r.close();
   st.close();
   c.close();


It turns out that the JDBC ResultSet returns the correct contents!.
I really dont understand why Hibernate approach wont work.

Given this reality, I might temporarily revert back using JDBC approach to access views.
But, when I obtain Connection, Statement, ResultSet objects from the Session, do I have to execute close() on each of this object?.

Will Session become prematurely close if I do so?,
coz, Ive got a ServletFilter to properly close the Session.....

Warmest Regards!...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 3:44 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you didn't show the query so I have no way to see what could be wrong about it...

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 3:46 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
and did you check what sql hibernate actually executes ? maybe that can hint the cause of a mapping error..

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 4:58 am 
Newbie

Joined: Fri Nov 19, 2004 6:35 am
Posts: 19
max wrote:
and did you check what sql hibernate actually executes ? maybe that can hint the cause of a mapping error..


(Please refer to previous code)
here's the query which is inside the hibernate's class mapping:
Code:
   <query name="com.test.hibernate.BalanceHistory.getById" >
      <![CDATA[
         from com.test.hibernate.BalanceHistory as p
         where p.clientId = :id
      ]]>
   </query>
   <query name="com.test.hibernate.BalanceHistory.getAll" >
      <![CDATA[
         from com.test.hibernate.BalanceHistory
      ]]>
   </query>


Im using Websphere IDE and the console printed the statement as follows:

Hibernate: select [views fields are here] from vw_BalanceHistory balancehis0_ where (balancehis0_.clientId=? )

Seems everything's perfect... :(

thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 5:03 am 
Newbie

Joined: Fri Nov 19, 2004 6:35 am
Posts: 19
and by the way, seems like the result is consistent in all my views.

all hibernate mapped to views return dataset just like what I described in my first posting.

cheers,
atn473


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 5:12 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
this is the most basic hibernate example i simply don't belive is has anything to do with hibernate ;)

What does "out" point to ? is it something that could print this stuff twice ?

Is clientId unique in the database ? If not hibernate would think the query returns the same object multiple times.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 5:14 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
looking more into it ...

Your table name has History in it - suggestion to me that you have the id duplicated multiple times - you need to provide hibernate with the column(s) that IDENTIFIES each row - client id does not (I assume!)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 6:44 am 
Newbie

Joined: Fri Nov 19, 2004 6:35 am
Posts: 19
hi Max!.
I agree with you!. This hibernate mapping that I defined is too simple.
Every hibernate beginners even know what it is doing. (I presume).
That is why as if Im trying to break a steel wall.

Anyways, regarding to your question, yes, clientId is defined in the original table as Unique and is a primary key.
the variable out is JSP's predefined out variable.

I'd also like to point out that, the SQL statement generated by hibernate, once I execute it directly into SQLWorksheet, works perfectly fine!!.
No redundancies whatsoever.

I know I missed something here... whatever it is I have no idea.

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 6:47 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Can you show me the output for the following SQL (not HQL)

"select * from vw_BalanceHistory where clientId = 101104"

that should ONLY contain ONE row if you mapping is correct!

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 11:36 am 
Newbie

Joined: Fri Nov 19, 2004 6:35 am
Posts: 19
max wrote:
Can you show me the output for the following SQL (not HQL)

"select * from vw_BalanceHistory where clientId = 101104"

that should ONLY contain ONE row if you mapping is correct!


bear in mind that we're trying to get the data in the VIEW.
and this VIEW contains two or more joined tables.
upon executing that command, it will return the client's transaction history which is more than ONE row.

but the weird thing is, Hibernate return, say a list whose objects' properties are completely the same,
while the generated SQL (by Hibernate), upon executing it directly to SQLWorksheet will return same number of rows but with different, but correct data.

interesting.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 11:46 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
please re-read what i have written ;)

If hibernate reads two rows like:

id: 42, amount: 1.99$
id: 42, amount: 4.99$

and you have told Hibernate that the unique identifier for objects from this table (or view) is those in the id column then you have inconsistent data!

The point is that unique identifier is NOT the id column - it is something else!

Maybe it is (id, amount) or you maybe have an (id, seqno) or something that makes each row unique!

The reason why you see hibernate show the same object is that it uses the id (which you told it to) to load the object from it's current session before looking at the resultset - thus the first row "overrules" the second.

If what you want is "just" a collection of values associated with a customer - you could look into having a collection of element or composite-element's.

Read about that in the docs or even better Hibernate In Action.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 9:36 pm 
Newbie

Joined: Fri Nov 19, 2004 6:35 am
Posts: 19
max wrote:
please re-read what i have written ;)

If hibernate reads two rows like:

id: 42, amount: 1.99$
id: 42, amount: 4.99$


oh, this is clearer to me now.
yes, i got rows similar to what you presented above.

so, how do i define multiple IDs in the XML mapping, like this?.

Code:
<id name="id1" column="id1">
    <generator class="assigned" />
</id>

<id name="id2" column="id2">
    <generator class="assigned" />
</id>

... ....

<id name="idn" column="idn">
    <generator class="assigned" />
</id>


or is there a better way?..

Thanks in advance and really I appreciate your help!.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 04, 2004 5:19 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
read the docs and dtd ;)

you can have multiple column or even components within and <id>.
Also look at composite-id if that can help you.

_________________
Max
Don't forget to rate


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