Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unqiue Constraint Added #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -42,6 +38,9 @@ private static void updateQueryParams(Query query, Filter filter) {
protected static <T extends Model, S extends Model> TypedQuery<S> createQuery(Class<T> entityType, String attribute, Class<S> attributeType, Filter filter) {
CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<S> cQuery = builder.createQuery(attributeType);
//
cQuery.distinct(filter.isDistinct());
//
Root<T> root = cQuery.from(entityType);
if (attribute != null) {
Join join = root.join(attribute);
Expand Down
26 changes: 15 additions & 11 deletions activejpa-core/src/main/java/org/activejpa/entity/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*
Expand All @@ -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
Expand Down Expand Up @@ -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<Model> values = model.collection("children").where(filter);
assertEquals(values, Arrays.asList(child1));
}

@Test
Expand All @@ -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<Model> values = model.collection("children").where(filter);
for (int i=0;i<values.size();i++){
System.out.println(values.get(i).getId());
}
List<Model> excepted = new ArrayList<Model>();
excepted.add(child2);
excepted.add(child3);
//System.out.println("Values size = "+values.size());
assertEquals(values, excepted);
}
@Test
public void shouldAddItemToCollectionUsingField() {
ParentWithField parent = new ParentWithField();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
<tag>activejpa-0.1.2-SNAPSHOT</tag>
</scm>

<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
</dependency>
</dependencies>
<developers>
<developer>
<id>ganeshs</id>
Expand Down Expand Up @@ -133,7 +140,8 @@
<format>html</format>
</formats>
<aggregate>true</aggregate>
</configuration>
<check/>
</configuration>
</plugin>
<plugin>
<groupId>org.eluder.coveralls</groupId>
Expand Down