[hibernate-commits] Hibernate SVN: r12758 - in shards/trunk/src: test/org/hibernate/shards/integration/model and 1 other directory.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Thu Jul 12 01:37:04 EDT 2007


Author: max.ross
Date: 2007-07-12 01:37:04 -0400 (Thu, 12 Jul 2007)
New Revision: 12758

Modified:
   shards/trunk/src/java/org/hibernate/shards/session/ShardedSessionImpl.java
   shards/trunk/src/test/org/hibernate/shards/integration/model/ModelQueryPermutedIntegrationTest.java
Log:
HSHARDS-30
Implement ShardedSessionImpl.createFilter()

Modified: shards/trunk/src/java/org/hibernate/shards/session/ShardedSessionImpl.java
===================================================================
--- shards/trunk/src/java/org/hibernate/shards/session/ShardedSessionImpl.java	2007-07-12 04:26:31 UTC (rev 12757)
+++ shards/trunk/src/java/org/hibernate/shards/session/ShardedSessionImpl.java	2007-07-12 05:37:04 UTC (rev 12758)
@@ -37,6 +37,7 @@
 import org.hibernate.UnresolvableObjectException;
 import org.hibernate.classic.Session;
 import org.hibernate.engine.SessionFactoryImplementor;
+import org.hibernate.engine.SessionImplementor;
 import org.hibernate.id.IdentifierGenerator;
 import org.hibernate.metadata.ClassMetadata;
 import org.hibernate.proxy.HibernateProxy;
@@ -1150,11 +1151,29 @@
   }
 
   /**
-   * Unsupported.  This is a scope decision, not a technical decision.
+   * The {@link org.hibernate.impl.SessionImpl#createFilter(Object, String)} implementation
+   * requires that the collection that is passed in is a persistent collection.
+   * Since we don't support cross-shard relationships, if we receive a persistent
+   * collection that collection is guaranteed to be associated with a single
+   * shard.  If we can figure out which shard the collection is associated with
+   * we can just delegate this operation to that shard.
    */
   public Query createFilter(Object collection, String queryString)
       throws HibernateException {
-    throw new UnsupportedOperationException();
+    Shard shard = getShardForCollection(collection, shards);
+    Session session;
+    if(shard == null) {
+      // collection not associated with any of our shards, so just delegate to
+      // a random shard.  We'll end up failing, but we'll fail with the
+      // error that users typically get.
+      session = getSomeSession();
+      if (session == null) {
+        session = shards.get(0).establishSession();
+      }
+    } else {
+      session = shard.establishSession();
+    }
+    return session.createFilter(collection, queryString);
   }
 
   public Query getNamedQuery(String queryName) throws HibernateException {
@@ -1511,6 +1530,18 @@
     return getShardIdForObject(obj, shards);
   }
 
+  private Shard getShardForCollection(Object coll, List<Shard> shardsToConsider) {
+    for(Shard shard : shardsToConsider) {
+      if(shard.getSession() != null) {
+        SessionImplementor si = ((SessionImplementor)shard.getSession());
+        if(si.getPersistenceContext().getCollectionEntryOrNull(coll) != null) {
+          return shard;
+        }
+      }
+    }
+    return null;
+  }
+
   List<ShardId> selectShardIdsFromShardResolutionStrategyData(ShardResolutionStrategyData srsd) {
     IdentifierGenerator idGenerator = shardedSessionFactory.getIdentifierGenerator(srsd.getEntityName());
     if ((idGenerator instanceof ShardEncodingIdentifierGenerator) &&

Modified: shards/trunk/src/test/org/hibernate/shards/integration/model/ModelQueryPermutedIntegrationTest.java
===================================================================
--- shards/trunk/src/test/org/hibernate/shards/integration/model/ModelQueryPermutedIntegrationTest.java	2007-07-12 04:26:31 UTC (rev 12757)
+++ shards/trunk/src/test/org/hibernate/shards/integration/model/ModelQueryPermutedIntegrationTest.java	2007-07-12 05:37:04 UTC (rev 12758)
@@ -19,11 +19,14 @@
 package org.hibernate.shards.integration.model;
 
 import org.hibernate.Query;
+import org.hibernate.QueryException;
 import org.hibernate.shards.integration.BaseShardingIntegrationTestCase;
 import org.hibernate.shards.model.Building;
 import org.hibernate.shards.model.Floor;
 import org.hibernate.shards.model.Office;
+import org.hibernate.shards.util.Lists;
 
+import java.util.Collection;
 import java.util.List;
 
 /**
@@ -85,48 +88,53 @@
     super.tearDown();
   }
 
-  public void testLoadAllBuildings() throws Exception {
+  public void testLoadAllBuildings()  {
     String queryString = "from Building";
     Query query = session.createQuery(queryString);
+    @SuppressWarnings("unchecked")
     List<Building> buildings = query.list();
     assertEquals(2, buildings.size());
     assertTrue(buildings.contains(b1));
     assertTrue(buildings.contains(b2));
   }
 
-  public void testLoadBuildingByName() throws Exception {
+  public void testLoadBuildingByName()  {
     String queryString = "from Building as b where b.name=:name";
     Query query = session.createQuery(queryString).setString("name", "b2");
     Building b2Reloaded = (Building) query.uniqueResult();
     assertEquals(b2.getBuildingId(), b2Reloaded.getBuildingId());
   }
 
-  public void testLoadBuildingsByLikeName() throws Exception {
+  public void testLoadBuildingsByLikeName()  {
     String queryString = "from Building as b where b.name like :name";
     Query query = session.createQuery(queryString).setString("name", "b%");
+    @SuppressWarnings("unchecked")
     List<Building> buildings = query.list();
     assertEquals(2, buildings.size());
     assertTrue(buildings.contains(b1));
     assertTrue(buildings.contains(b2));
   }
 
-  public void testLoadHighFloors() throws Exception {
+  public void testLoadHighFloors()  {
     String queryString = "from Floor as f where f.number >= 3";
     Query query = session.createQuery(queryString);
+    @SuppressWarnings("unchecked")
     List<Floor> floors = query.list();
     assertEquals(1, floors.size());
   }
 
-  public void testLoadBuildingsWithHighFloors() throws Exception {
+  public void testLoadBuildingsWithHighFloors() {
     String queryString = "from Floor as f where f.number >= 3";
     Query query = session.createQuery(queryString);
+    @SuppressWarnings("unchecked")
     List<Floor> floors = query.list();
     assertEquals(1, floors.size());
   }
 
-  public void testLoadBuildingsWithHighFloorsAndLargeOffices() throws Exception {
+  public void testLoadBuildingsWithHighFloorsAndLargeOffices() {
     String queryString = "from Building b join b.floors floor join floor.offices office where office.label = 'LAHGE'";
     Query query = session.createQuery(queryString);
+    @SuppressWarnings("unchecked")
     List<Building> buildings = query.list();
     assertEquals(2, buildings.size());
   }
@@ -134,6 +142,7 @@
   public void testNamedQuery() {
     Query query = session.getNamedQuery("SelectFloorsHigherThan");
     query.setInteger("lowestFloor", 3);
+    @SuppressWarnings("unchecked")
     List<Floor> floors = query.list();
     assertEquals(1, floors.size());
   }
@@ -141,6 +150,7 @@
   public void testSetMaxResults() {
     String queryString = "from Floor";
     Query query = session.createQuery(queryString).setMaxResults(1);
+    @SuppressWarnings("unchecked")
     List<Floor> floors = query.list();
     assertEquals(1, floors.size());
   }
@@ -148,16 +158,47 @@
   public void testSetFirstResultQuery() {
     String queryString = "from Floor";
     Query query = session.createQuery(queryString).setFirstResult(2);
+    @SuppressWarnings("unchecked")
     List<Floor> floors = query.list();
     assertEquals(2, floors.size());
   }
 
-  public void xtestAggregating() throws Exception {
+  public void xtestAggregating()  {
     //TODO(maulik) make this test work
     String queryString = "min(floor.number) from Floor floor";
     Query query = session.createQuery(queryString);
+    @SuppressWarnings("unchecked")
     List<Building> buildings = query.list();
     assertEquals(2, buildings.size());
   }
 
+  public void testCreateSimpleFilter() {
+    Building b = (Building) session.get(Building.class, b1.getBuildingId());
+    assertNotNull(b);
+    Collection<Floor> coll = b.getFloors();
+    Query query = session.createFilter(coll, "");
+    @SuppressWarnings("unchecked")
+    List<Floor> filteredFloors = query.list();
+    assertEquals(3, filteredFloors.size());
+  }
+
+  public void testCreateFancyFilter() {
+    Building b = (Building) session.get(Building.class, b1.getBuildingId());
+    assertNotNull(b);
+    Collection<Floor> coll = b.getFloors();
+    Query query = session.createFilter(coll, "where number > 2");
+    @SuppressWarnings("unchecked")
+    List<Floor> filteredFloors = query.list();
+    assertEquals(1, filteredFloors.size());
+  }
+
+  public void testCreateFilterForUnattachedCollection() {
+    List<Floor> floors = Lists.newArrayList();
+    try {
+      session.createFilter(floors, "");
+      fail("expected query exception");
+    } catch (QueryException qe) {
+      // good
+    }
+  }
 }




More information about the hibernate-commits mailing list