DATACOUCH-156 - Make generated N1QL queries filter on the type field.
The generated N1QL queries currently don't filter on the type at all. Add a criteria to the WHERE clause that checks the field holding type information is matching the entity fully qualified class name. Add a placeholder for inline N1QL queries that can be replaced by the same type information criteria.
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package org.springframework.data.couchbase.repository;
|
||||
|
||||
import com.couchbase.client.java.repository.annotation.Field;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
public class Item {
|
||||
|
||||
@Id
|
||||
public String id;
|
||||
|
||||
@Field("desc")
|
||||
public String description;
|
||||
|
||||
public Item(String id, String description) {
|
||||
this.id = id;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Item item = (Item) o;
|
||||
|
||||
if (!id.equals(item.id)) return false;
|
||||
return !(description != null ? !description.equals(item.description) : item.description != null);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = id.hashCode();
|
||||
result = 31 * result + (description != null ? description.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package org.springframework.data.couchbase.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface ItemRepository extends CrudRepository<Item, String> {
|
||||
|
||||
List<Object> findAllByDescriptionNotNull();
|
||||
}
|
||||
@@ -16,12 +16,15 @@
|
||||
|
||||
package org.springframework.data.couchbase.repository;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import com.couchbase.client.java.Bucket;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -47,19 +50,50 @@ public class N1qlCrudRepositoryTests {
|
||||
private CouchbaseTemplate template;
|
||||
|
||||
private PartyRepository partyRepository;
|
||||
private ItemRepository itemRepository;
|
||||
|
||||
private static final Item item = new Item("itemNotParty", "short description");
|
||||
private static final Party party = new Party("partyNotItem", "partyName", "short description", new Date(), 120);
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
partyRepository = new CouchbaseRepositoryFactory(template).getRepository(PartyRepository.class);
|
||||
itemRepository = new CouchbaseRepositoryFactory(template).getRepository(ItemRepository.class);
|
||||
|
||||
itemRepository.save(item);
|
||||
partyRepository.save(party);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUp() {
|
||||
itemRepository.delete("itemNotParty");
|
||||
partyRepository.delete("partyNotItem");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDistinguishBetweenItemsAndParties() {
|
||||
List<Object> items = itemRepository.findAllByDescriptionNotNull();
|
||||
List<Object> parties = partyRepository.findAllByDescriptionNotNull();
|
||||
|
||||
assertTrue(items.contains(item));
|
||||
assertTrue(parties.contains(party));
|
||||
|
||||
assertFalse(items.contains(party));
|
||||
assertFalse(parties.contains(item));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSaveObjectWithN1qlKeywordField() {
|
||||
Party party = new Party("partyHasKeyword", "party", "desc is a N1QL keyword", new Date(), 40);
|
||||
partyRepository.save(party);
|
||||
List<Party> parties = partyRepository.findAllByDescriptionNotNull();
|
||||
List<Object> parties = partyRepository.findAllByDescriptionNotNull();
|
||||
|
||||
assertTrue(client.exists("partyHasKeyword"));
|
||||
assertTrue(parties.contains(party));
|
||||
for (Object o : parties) {
|
||||
if (!(o instanceof Party)) {
|
||||
fail("expected only Party objects");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,6 @@ public interface PartyRepository extends CouchbaseRepository<Party, String> {
|
||||
@View(designDocument = "party", viewName = "byDate")
|
||||
List<Party> findFirst3ByEventDateGreaterThanEqual(Date targetDate);
|
||||
|
||||
List<Party> findAllByDescriptionNotNull();
|
||||
List<Object> findAllByDescriptionNotNull();
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user