|
From: <gca...@us...> - 2013-03-14 14:25:47
|
Revision: 4203
http://openutils.svn.sourceforge.net/openutils/?rev=4203&view=rev
Author: gcatania
Date: 2013-03-14 14:25:36 +0000 (Thu, 14 Mar 2013)
Log Message:
-----------
BSHD-20 add flag to change identifier restriction behavior, tests
Modified Paths:
--------------
trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java
trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java
trunk/openutils-bshd5/src/test/java/it/openutils/hibernate/test/HibernateDAOLazyLoadTest.java
Added Paths:
-----------
trunk/openutils-bshd5/src/test/java/it/openutils/hibernate/test/HibernateDAOFindIdentifierTest.java
Modified: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java
===================================================================
--- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java 2013-03-12 09:13:58 UTC (rev 4202)
+++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java 2013-03-14 14:25:36 UTC (rev 4203)
@@ -45,6 +45,7 @@
import org.hibernate.criterion.Example.PropertySelector;
import org.hibernate.criterion.MatchMode;
import org.hibernate.metadata.ClassMetadata;
+import org.hibernate.proxy.HibernateProxy;
import org.hibernate.type.Type;
@@ -67,6 +68,8 @@
private boolean isIgnoreCaseEnabled;
+ private boolean jointPropertyAndIdentifierFiltering;
+
private final Map<String, Set<String>> excludedProperties = new HashMap<String, Set<String>>();
private final Map<String, List<Criterion>> additionalConditions = new HashMap<String, List<Criterion>>();
@@ -182,6 +185,18 @@
}
/**
+ * The default behaviour of this class ignores properties set on a filter entity with a non-null identifier.
+ * Invoking this method reverses the default behaviour by enabling property filtering regardless of identifier
+ * presence.
+ * @return this, for method concatenation
+ */
+ public ExampleTree enableJointPropertyAndIdentifierFiltering()
+ {
+ jointPropertyAndIdentifierFiltering = true;
+ return this;
+ }
+
+ /**
* add an additional criterion for properties of the subentity at the given path
* @param associationPath the association path with respect to the filter entity
* @param criterion the criterion to add
@@ -257,9 +272,12 @@
entity,
classMetadata,
sessionFactory.getCurrentSession()); // BSHD-11
- if (isIdSet)
+ if (isIdSet && (HibernateProxy.class.isInstance(entity) || !jointPropertyAndIdentifierFiltering))
{
- // BSHD-20 if the identifier is set on a property, do not impose further conditions
+ // BSHD-20 only impose the identifier conditions in the following cases:
+ // 1) if the current entity is an hibernate proxy (because we assume the identifier restriction is
+ // enough and the entity is already aligned
+ // 2) if the corresponding flag has not been explicitly activated
return;
}
Modified: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java
===================================================================
--- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java 2013-03-12 09:13:58 UTC (rev 4202)
+++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java 2013-03-14 14:25:36 UTC (rev 4203)
@@ -38,6 +38,7 @@
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.hibernate.metadata.ClassMetadata;
+import org.hibernate.proxy.HibernateProxy;
import org.hibernate.type.Type;
@@ -109,16 +110,7 @@
private void createSubExamples(Criteria crit, Object entity, String[] walkedProperties)
{
ClassMetadata classMetadata = ExampleTreeUtils.getClassMetadata(entity, sessionFactory);
- boolean isIdSet = ExampleTreeUtils.addIdentifierRestriction(
- crit,
- entity,
- classMetadata,
- sessionFactory.getCurrentSession()); // BSHD-11
- if (isIdSet)
- {
- // BSHD-20 if the identifier is set on a property, do not impose further conditions
- return;
- }
+ ExampleTreeUtils.addIdentifierRestriction(crit, entity, classMetadata, sessionFactory.getCurrentSession()); // BSHD-11
String path = ExampleTreeUtils.getPath(walkedProperties);
Map<String, FilterMetadata> currFilterMetadata = getFilterMetadata(path);
@@ -192,6 +184,14 @@
private Example example(Object entity, Set<String> propertiesToExclude)
{
+ if (HibernateProxy.class.isInstance(entity))
+ {
+ // BSHD-19 javassist hibernate proxies do not seem to work with examples (property values are not
+ // returned)
+ HibernateProxy proxy = (HibernateProxy) entity;
+ entity = proxy.getHibernateLazyInitializer().getImplementation();
+ }
+
Example ex = Example.create(entity);
ex.setPropertySelector(new ExcludeBackrefPropertySelector(ExampleTreePropertySelectorSupport.NOT_NULL)); // BSHD-15
for (String propertyName : propertiesToExclude)
Added: trunk/openutils-bshd5/src/test/java/it/openutils/hibernate/test/HibernateDAOFindIdentifierTest.java
===================================================================
--- trunk/openutils-bshd5/src/test/java/it/openutils/hibernate/test/HibernateDAOFindIdentifierTest.java (rev 0)
+++ trunk/openutils-bshd5/src/test/java/it/openutils/hibernate/test/HibernateDAOFindIdentifierTest.java 2013-03-14 14:25:36 UTC (rev 4203)
@@ -0,0 +1,142 @@
+/**
+ *
+ * openutils base Spring-Hibernate DAO (http://www.openmindlab.com/lab/products/bshd5.html)
+ *
+ * Copyright(C) 2005-2012, Openmind S.r.l. http://www.openmindonline.it
+ *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl-2.1.html
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package it.openutils.hibernate.test;
+
+import it.openutils.hibernate.example.ExampleTree;
+import it.openutils.hibernate.test.dao.FooDAO;
+import it.openutils.hibernate.test.model.Bar;
+import it.openutils.hibernate.test.model.Foo;
+
+import java.util.List;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
+import org.testng.Assert;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+
+/**
+ * @author gcatania
+ */
+@ContextConfiguration(locations = "/spring-tests.xml")
+public class HibernateDAOFindIdentifierTest extends AbstractTransactionalTestNGSpringContextTests
+{
+
+ @Autowired
+ private FooDAO fooDAO;
+
+ @BeforeClass
+ protected final void preloadData()
+ {
+ executeSqlScript("/preload-data.sql", false);
+ }
+
+ @AfterClass
+ protected final void cleanupData()
+ {
+ super.deleteFromTables("foo", "bar");
+ }
+
+ private List<Foo> searchFoos(Long barId, String barStr, Long fooId, String fooStr)
+ {
+ Bar barFilter = new Bar();
+ barFilter.setId(barId);
+ barFilter.setS(barStr);
+ Foo fooFilter = new Foo();
+ fooFilter.setId(fooId);
+ fooFilter.setBar(barFilter);
+ fooFilter.setS(fooStr);
+ return fooDAO.findFiltered(fooFilter);
+ }
+
+ private Foo findFoo(Long barId, String barStr, Long fooId, String fooStr)
+ {
+ List<Foo> found = searchFoos(barId, barStr, fooId, fooStr);
+ Assert.assertEquals(found.size(), 1);
+ return found.get(0);
+ }
+
+ private void dontFindFoo(Long barId, String barStr, Long fooId, String fooStr)
+ {
+ List<Foo> found = searchFoos(barId, barStr, fooId, fooStr);
+ Assert.assertEquals(found.size(), 0);
+ }
+
+ @Test
+ public void testFindWithParentId()
+ {
+ Foo foundFoo = findFoo(1L, null, null, "fooX_X");
+ Assert.assertEquals(foundFoo.getId().longValue(), 3L);
+ }
+
+ @Test
+ public void testFindWithParentProperty()
+ {
+ Foo foundFoo = findFoo(null, "bar1", null, "fooX_X");
+ Assert.assertEquals(foundFoo.getId().longValue(), 3L);
+ }
+
+ @Test
+ public void testDontFindWithParentId()
+ {
+ dontFindFoo(1L, null, null, "foo2_1");
+ }
+
+ @Test
+ public void testDontFindWithParentProperty()
+ {
+ dontFindFoo(null, "bar1", null, "foo2_1");
+ }
+
+ @Test
+ public void testFindWithBothParentIdAndPropertyKeepingInMindTheMagicFlagIsDisabled()
+ {
+ List<Foo> foundFoos = searchFoos(2L, "bar1", null, null);
+ Assert.assertEquals(foundFoos.size(), 3);
+ for (Foo foo : foundFoos)
+ {
+ Assert.assertEquals(foo.getBar().getId().longValue(), 2L);
+ }
+ }
+
+ @Test
+ public void testDontFindWithBothParentIdAndPropertyKeepingInMindTheMagicFlagIsEnabled()
+ {
+ Bar barFilter = new Bar();
+ barFilter.setId(2L);
+ barFilter.setS("bar1");
+ Foo fooFilter = new Foo();
+ fooFilter.setBar(barFilter);
+ ExampleTree et = new ExampleTree(fooFilter);
+ et.enableJointPropertyAndIdentifierFiltering();
+ List<Foo> foundFoos = fooDAO.findFiltered(et);
+ Assert.assertEquals(foundFoos.size(), 0);
+ }
+
+}
Property changes on: trunk/openutils-bshd5/src/test/java/it/openutils/hibernate/test/HibernateDAOFindIdentifierTest.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:keywords
+ Author Date Id Revision
Added: svn:eol-style
+ native
Modified: trunk/openutils-bshd5/src/test/java/it/openutils/hibernate/test/HibernateDAOLazyLoadTest.java
===================================================================
--- trunk/openutils-bshd5/src/test/java/it/openutils/hibernate/test/HibernateDAOLazyLoadTest.java 2013-03-12 09:13:58 UTC (rev 4202)
+++ trunk/openutils-bshd5/src/test/java/it/openutils/hibernate/test/HibernateDAOLazyLoadTest.java 2013-03-14 14:25:36 UTC (rev 4203)
@@ -36,6 +36,7 @@
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.testng.Assert;
+import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@@ -59,6 +60,12 @@
executeSqlScript("/preload-data.sql", false);
}
+ @AfterClass
+ protected final void cleanupData()
+ {
+ super.deleteFromTables("foo", "bar");
+ }
+
private List<Foo> findFoo(String s, Bar bar)
{
Foo filter = new Foo();
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|