-->
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.  [ 10 posts ] 
Author Message
 Post subject: "read all records" and session.find on this collec
PostPosted: Sat Feb 21, 2004 3:40 pm 
Regular
Regular

Joined: Thu Dec 11, 2003 4:14 pm
Posts: 86
Location: Hibernate 3 + annotations, Oracle 9i, PostgreSQL 8.0, Java 1.5.0
Hello!

Maybe i am blind, but i just cant find something which helps me.

Is it possible to read all records in an table into a map where the key is other than the pk-key of the table?
Much like the <map ..> in an hbm.xml but i do not have a parent-child association.
The table is simply the "root"-Table.


Since i cant find how to add such a <map ...> into my hbm.xml without a parent-child association, i have tried something else. But this wont work too:

Table:
id <= pk-key
path <= another uniq-index field
last-mod-date
file-checked


And would like to set the file-checked to false on startup,
then check every file and set the last-mod-date and true to file-checked. Sometimes a new record has to be inserted.

Now i thought about this:

Code:
      List files = dbSession.createCriteria(db.File.class).list();
      Iterator iterFiles = songs.iterator();
      while (iterFiles.hasNext())
      {
         db.File file = (db.File) iterFiles.next();
         file.setFileChecked(false);
         dbSession.update(file);
      }

      iterate-through-files-dummy
      {
            Collection files = dbSession.filter(files, "where path = ?", path, Hibernate.STRING);
            if (files.isEmpty())
            {
                  db.File file = new db.File(next-id, path)
                  file.setLastModDate(filedate);
                  file.setFileChecked(true);
                  dbSession.save(file);
            }
            else
            {
                  file.setLastModDate(filedate);
                  file.setFileChecked(true);
                  dbSession.update(file);
            }
      }



But dbSession.filter wont work. I know why (files is not a persistent collection), but can i fetch all records to suit these needs?

Thanks,
Mario


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 21, 2004 7:30 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I don't get it. Why don't you just do session.find("from db.File f where f.path=?") instead of the filter?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 22, 2004 4:04 am 
Regular
Regular

Joined: Thu Dec 11, 2003 4:14 pm
Posts: 86
Location: Hibernate 3 + annotations, Oracle 9i, PostgreSQL 8.0, Java 1.5.0
I would like to read all records of the table but need another (also uniq) key to access the elements.
Just like the <map sort="xxxx" ...> element for collections in hbm.xml

The idea is to load the object once. If i use session.find hibernate execute one select * from file for the criteria and many
select * from file where path = ? for every find. This cost some performance.

I know i could use session.iterate and build my own collection, but i thought hibernate just can do this with <map sort="">, i simply havent managed to use this for a root table.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 22, 2004 4:08 am 
Regular
Regular

Joined: Thu Dec 11, 2003 4:14 pm
Posts: 86
Location: Hibernate 3 + annotations, Oracle 9i, PostgreSQL 8.0, Java 1.5.0
In other words:

I have to access a collection two times:
1) first the ordering is not important i simply have to update a flag
2) direct get using the "path" as key

Now i am searching a way to create this collections once, filled with all records of the table.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 22, 2004 10:21 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You could also do "select f.path, f from Files f" which will give you an List containing Object[2]'s . You could then put them into a map yourself if you want.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 22, 2004 11:46 am 
Regular
Regular

Joined: Thu Dec 11, 2003 4:14 pm
Posts: 86
Location: Hibernate 3 + annotations, Oracle 9i, PostgreSQL 8.0, Java 1.5.0
I hoped i could use the internal hibernate cache.

If i use my own collection, i have the problem to keep it in sync with the database.
Do you suggest to use interceptors for this?
However, this is a very complicated thing to do. Things hibernate might have included already, or am i wrong here (<map ...>)?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 22, 2004 1:27 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You do not intend to keep that stuff longer than one session don't you? Then you have just the same situation than you'd have when you got a hibernate collection. This is all in one transaction scope, so your database should handle isolation.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 22, 2004 2:11 pm 
Regular
Regular

Joined: Thu Dec 11, 2003 4:14 pm
Posts: 86
Location: Hibernate 3 + annotations, Oracle 9i, PostgreSQL 8.0, Java 1.5.0
After startup updates to this collection are very rare, and if needet, i thought i could evict the object.
But if i build my own collection from some hibernate resulst, hibernate cant invalidate this local cache. This might be dangerous.

In our own, self written persistence layer, every collection can listen to changes in the database, and in the simplest case, all elements within will be invalidated (on next access, the collection is regenerated)

This is something i have to find out, how to do with hibernate.

I could reimplement suche local application cache again, but i have the feeling hibernate can do this much more elegant.

The only thing needet is to get a set or map from all records. Something like list(), find(), ... , but with return type "map".
For sure, i have to call e.g. list() every time, but if there are no other changes, hibernate gets the result from the cache automatically.

Or am i wrong here?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 22, 2004 4:12 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Well if you use a cache anyways, you can just enable the cache for the appropriate classes and the query cache. Then you won't have database hits and won't have to worry about doing some additional querys (they are cached anyways). So you can do additional finds without problems.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 22, 2004 4:28 pm 
Regular
Regular

Joined: Thu Dec 11, 2003 4:14 pm
Posts: 86
Location: Hibernate 3 + annotations, Oracle 9i, PostgreSQL 8.0, Java 1.5.0
Say i have 1500 records in the table, i have to send 1500 find requests. Correct, these are cached then, but this 1500 find requests have to be done.

If i load all these 1500 records in one select, this is much faster.

This is something i have pretty often in an real live application.

I would like to query using the criteria api and pass in an comparator which should be used for the returning map.
Is this something which would be implementd if i try to extends the criteria api this way?


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