From d3592e0f8c65962a8d00dafd61eae5805bf73171 Mon Sep 17 00:00:00 2001 From: Kartik Gupta Date: Sat, 16 Jan 2016 17:41:50 +0530 Subject: [PATCH] Unqiue Constraint Added --- .../java/org/activejpa/entity/BaseObject.java | 13 ++++--- .../java/org/activejpa/entity/Filter.java | 26 ++++++++------ .../entity/EntityCollectionTest.java | 35 +++++++++++++------ .../examples/petclinic/model/BaseEntity.java | 4 +-- .../examples/petclinic/model/OwnerTests.java | 4 +-- pom.xml | 10 +++++- 6 files changed, 59 insertions(+), 33 deletions(-) diff --git a/activejpa-core/src/main/java/org/activejpa/entity/BaseObject.java b/activejpa-core/src/main/java/org/activejpa/entity/BaseObject.java index e96d183..bb3de77 100644 --- a/activejpa-core/src/main/java/org/activejpa/entity/BaseObject.java +++ b/activejpa-core/src/main/java/org/activejpa/entity/BaseObject.java @@ -3,16 +3,12 @@ */ package org.activejpa.entity; +import org.activejpa.jpa.JPA; + import javax.persistence.EntityManager; import javax.persistence.Query; import javax.persistence.TypedQuery; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaDelete; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Join; -import javax.persistence.criteria.Root; - -import org.activejpa.jpa.JPA; +import javax.persistence.criteria.*; /** * @author ganeshs @@ -42,6 +38,9 @@ private static void updateQueryParams(Query query, Filter filter) { protected static TypedQuery createQuery(Class entityType, String attribute, Class attributeType, Filter filter) { CriteriaBuilder builder = getEntityManager().getCriteriaBuilder(); CriteriaQuery cQuery = builder.createQuery(attributeType); + // + cQuery.distinct(filter.isDistinct()); + // Root root = cQuery.from(entityType); if (attribute != null) { Join join = root.join(attribute); diff --git a/activejpa-core/src/main/java/org/activejpa/entity/Filter.java b/activejpa-core/src/main/java/org/activejpa/entity/Filter.java index ea44525..70d0e7e 100644 --- a/activejpa-core/src/main/java/org/activejpa/entity/Filter.java +++ b/activejpa-core/src/main/java/org/activejpa/entity/Filter.java @@ -3,21 +3,15 @@ */ package org.activejpa.entity; +import org.activejpa.entity.Condition.Operator; + +import javax.persistence.Query; +import javax.persistence.criteria.*; import java.io.StringWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import javax.persistence.Query; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaDelete; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Order; -import javax.persistence.criteria.Predicate; -import javax.persistence.criteria.Root; - -import org.activejpa.entity.Condition.Operator; - /** * @author ganeshs * @@ -35,7 +29,17 @@ public class Filter { private boolean cacheable; private boolean shouldPage; - + + private boolean distinct = false; + + public boolean isDistinct() { + return distinct; + } + + public void setDistinct(boolean distinct) { + this.distinct = distinct; + } + public Filter(int perPage, int pageNo, Condition... conditions) { this.pageNo = pageNo > 0 ? pageNo : 1; this.shouldPage = perPage > 0; diff --git a/activejpa-core/src/test/java/org/activejpa/entity/EntityCollectionTest.java b/activejpa-core/src/test/java/org/activejpa/entity/EntityCollectionTest.java index b8daafb..4fae1f1 100644 --- a/activejpa-core/src/test/java/org/activejpa/entity/EntityCollectionTest.java +++ b/activejpa-core/src/test/java/org/activejpa/entity/EntityCollectionTest.java @@ -3,13 +3,6 @@ */ package org.activejpa.entity; -import static org.testng.Assert.assertEquals; - -import java.io.Serializable; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; - import org.activejpa.entity.testng.BaseModelTest; import org.activejpa.jpa.JPA; import org.testng.IObjectFactory; @@ -18,6 +11,11 @@ import org.testng.annotations.Test; import org.testng.internal.ObjectFactoryImpl; +import java.io.Serializable; +import java.util.*; + +import static org.testng.Assert.assertEquals; + /** * @author ganeshs * @@ -31,7 +29,8 @@ public class EntityCollectionTest extends BaseModelTest { private DummyModel child2; private DummyModel child3; - + + private DummyModel child4; /** * HACK. `mvn test` will be run before the package is created. javaagent can be loaded only from a jar. Since the * jar is not yet created, it will throw agent not found exception. This is a hack to get rid of that exception @@ -90,7 +89,9 @@ public void shouldSearchByMultipleKeyValues() { @Test public void shouldSearchUsingFilter() { - assertEquals(model.collection("children").where(new Filter(new Condition("children.column1", "testChildColumn0"), new Condition("children.column2", "testChildColumn2"))), Arrays.asList(child1)); + Filter filter = new Filter(new Condition("children.column1", "testChildColumn0")); + List values = model.collection("children").where(filter); + assertEquals(values, Arrays.asList(child1)); } @Test @@ -108,7 +109,21 @@ public void shouldAddItemToCollectionUsingGetter() { collection.add(model); assertEquals(parent.models.size(), 1); } - + + @Test + public void shouldReturnDistinctRows(){ + Filter filter = new Filter(new Condition("children.column1", "testChildColumn1")); + filter.setDistinct(true); + List values = model.collection("children").where(filter); + for (int i=0;i excepted = new ArrayList(); + excepted.add(child2); + excepted.add(child3); + //System.out.println("Values size = "+values.size()); + assertEquals(values, excepted); + } @Test public void shouldAddItemToCollectionUsingField() { ParentWithField parent = new ParentWithField(); diff --git a/activejpa-examples/activejpa-examples-spring/src/main/java/org/activejpa/examples/petclinic/model/BaseEntity.java b/activejpa-examples/activejpa-examples-spring/src/main/java/org/activejpa/examples/petclinic/model/BaseEntity.java index 9bb30e9..0005809 100644 --- a/activejpa-examples/activejpa-examples-spring/src/main/java/org/activejpa/examples/petclinic/model/BaseEntity.java +++ b/activejpa-examples/activejpa-examples-spring/src/main/java/org/activejpa/examples/petclinic/model/BaseEntity.java @@ -15,13 +15,13 @@ */ package org.activejpa.examples.petclinic.model; +import org.activejpa.entity.Model; + import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.MappedSuperclass; -import org.activejpa.entity.Model; - /** * Simple JavaBean domain object with an id property. Used as a base class for objects needing this property. * diff --git a/activejpa-examples/activejpa-examples-spring/src/test/java/org/activejpa/examples/petclinic/model/OwnerTests.java b/activejpa-examples/activejpa-examples-spring/src/test/java/org/activejpa/examples/petclinic/model/OwnerTests.java index 9f6b4be..87e9729 100644 --- a/activejpa-examples/activejpa-examples-spring/src/test/java/org/activejpa/examples/petclinic/model/OwnerTests.java +++ b/activejpa-examples/activejpa-examples-spring/src/test/java/org/activejpa/examples/petclinic/model/OwnerTests.java @@ -15,11 +15,11 @@ */ package org.activejpa.examples.petclinic.model; +import org.testng.annotations.Test; + import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNull; -import org.testng.annotations.Test; - /** * Testng test for the {@link Owner} class. * diff --git a/pom.xml b/pom.xml index ced0cb3..cab0532 100644 --- a/pom.xml +++ b/pom.xml @@ -23,6 +23,13 @@ activejpa-0.1.2-SNAPSHOT + + + org.mockito + mockito-all + 1.9.5 + + ganeshs @@ -133,7 +140,8 @@ html true - + + org.eluder.coveralls