From: <gca...@us...> - 2013-03-15 13:55:32
|
Revision: 4204 http://openutils.svn.sourceforge.net/openutils/?rev=4204&view=rev Author: gcatania Date: 2013-03-15 13:55:23 +0000 (Fri, 15 Mar 2013) Log Message: ----------- Merged revisions 4197,4203 via svnmerge from https://openutils.svn.sourceforge.net/svnroot/openutils/trunk/openutils-bshd5 ........ r4197 | gcatania | 2013-03-11 16:29:17 +0100 (lun, 11 mar 2013) | 1 line BSHD-19 re-enable tests that no longer fail after version bump ........ r4203 | gcatania | 2013-03-14 15:25:36 +0100 (gio, 14 mar 2013) | 1 line BSHD-20 add flag to change identifier restriction behavior, tests ........ Revision Links: -------------- http://openutils.svn.sourceforge.net/openutils/?rev=4197&view=rev http://openutils.svn.sourceforge.net/openutils/?rev=4203&view=rev Modified Paths: -------------- branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/example/ExampleTree.java branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java branches/openutils-bshd5-backport/src/test/java/it/openutils/hibernate/test/HibernateDAOLazyLoadTest.java Added Paths: ----------- branches/openutils-bshd5-backport/src/test/java/it/openutils/hibernate/test/HibernateDAOFindIdentifierTest.java Property Changed: ---------------- branches/openutils-bshd5-backport/ branches/openutils-bshd5-backport/src/ Property changes on: branches/openutils-bshd5-backport ___________________________________________________________________ Modified: svnmerge-integrated - /trunk/openutils-bshd5:1-4040,4042-4056,4058-4092,4192,4195,4201 + /trunk/openutils-bshd5:1-4040,4042-4056,4058-4092,4095-4096,4098-4195,4197-4203 Modified: svn:mergeinfo - /trunk/openutils-bshd5:4045-4047*,4057,4059,4069-4070*,4073*,4075-4078*,4080*,4082*,4084-4085*,4087*,4089*,4091-4092*,4192*,4195*,4201* + /trunk/openutils-bshd5:4045-4047*,4057,4059,4069-4070*,4073*,4075-4078*,4080*,4082*,4084-4085*,4087*,4089*,4091-4092*,4192*,4195*,4197*,4201*,4203* Property changes on: branches/openutils-bshd5-backport/src ___________________________________________________________________ Modified: svn:mergeinfo - /trunk/openutils-bshd5/src:4045-4047,4057,4069-4070,4073,4075-4078,4080,4082,4084-4085,4087,4089,4091-4092,4192,4195,4201 + /trunk/openutils-bshd5/src:4045-4047,4057,4069-4070,4073,4075-4078,4080,4082,4084-4085,4087,4089,4091-4092,4192,4195,4201,4203 Modified: branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/example/ExampleTree.java =================================================================== --- branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/example/ExampleTree.java 2013-03-14 14:25:36 UTC (rev 4203) +++ branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/example/ExampleTree.java 2013-03-15 13:55:23 UTC (rev 4204) @@ -46,6 +46,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; @@ -68,6 +69,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>>(); @@ -183,6 +186,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 @@ -261,9 +276,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: branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java =================================================================== --- branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java 2013-03-14 14:25:36 UTC (rev 4203) +++ branches/openutils-bshd5-backport/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java 2013-03-15 13:55:23 UTC (rev 4204) @@ -39,6 +39,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; @@ -113,16 +114,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); @@ -196,6 +188,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) Copied: branches/openutils-bshd5-backport/src/test/java/it/openutils/hibernate/test/HibernateDAOFindIdentifierTest.java (from rev 4203, trunk/openutils-bshd5/src/test/java/it/openutils/hibernate/test/HibernateDAOFindIdentifierTest.java) =================================================================== --- branches/openutils-bshd5-backport/src/test/java/it/openutils/hibernate/test/HibernateDAOFindIdentifierTest.java (rev 0) +++ branches/openutils-bshd5-backport/src/test/java/it/openutils/hibernate/test/HibernateDAOFindIdentifierTest.java 2013-03-15 13:55:23 UTC (rev 4204) @@ -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); + } + +} Modified: branches/openutils-bshd5-backport/src/test/java/it/openutils/hibernate/test/HibernateDAOLazyLoadTest.java =================================================================== --- branches/openutils-bshd5-backport/src/test/java/it/openutils/hibernate/test/HibernateDAOLazyLoadTest.java 2013-03-14 14:25:36 UTC (rev 4203) +++ branches/openutils-bshd5-backport/src/test/java/it/openutils/hibernate/test/HibernateDAOLazyLoadTest.java 2013-03-15 13:55:23 UTC (rev 4204) @@ -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. |