views:

214

answers:

1

Ok. What I wanted was: With Hibernate, to load once a list of MyEntity objects and have that list as the internal data of a model of a jtable. I also have a jTextField and when a key is typed, i call a method of my model that takes the text of the jtextfield as argument and filters the internal list of MyEntitys and fires the tablemodel change event. I want to do that filtering without hitting the database everytime.

What I did was: I kept 2 lists inside the model: The one named initialList, that was only once loaded with createCriteria(...).list(), and a second list named filteredList, that whenever the refresh method was called it was populated like this:

public void refresh(String filterText)
{
   filteredList = session.createFilter(initialList, "where property= ?")
  .setParameter( 0, filterText)
 .list();
}

Then I used this filtered list to give the jtable the row,column value. Well, It didnt work because Hibernate says that the filter API stuff can be used only in persistent collections. So I have the following 3 questions:

  1. Essentialy If i create a dummy parent entity(along with the dummy parent table in the database) that will contain all of the MyEntity rows as child collection and load this parent dummy entity, this will work? If yes, it sounds silly to do this, just to make hibernate consider the collection persistent and let me use the filter stuff. What am I missing?
  2. Suppose I do have a persistent collection and use the filter API, does this guarantee that I will never hit the database except from the initial loading? I dont care being updated, just load once and then filter QUICKLY, because I need to filter every time the user presses a key.
  3. Is there any other way to get this functionality. I suppose I could manually use the Java Collections API to sort filter index and stuff, but any other ready made way?

Thanks in advance.

A: 

Not sure about #1 but it might work...

I would recommend looking maybe at commons-collection (CollectionsUtils) and do the filtering in memory as you need

Also depending on the queries you do, the second level cache might actually prevent the access to the database if configured correctly.

webclimber