DATACOUCH-432 - Fix ANSI Joins OneToOne relationships

Couchbase template could get only the component type information which
works for collection properties but when the join is one-to-one for a
single generic type it would fail. The fix is use to the actual type if
the property is not a collection.

Original PR: #177.
This commit is contained in:
ctayeb
2019-02-22 13:00:41 +01:00
committed by Subhashni Balakrishnan
parent 5af2262029
commit 97fc4a0471
7 changed files with 121 additions and 22 deletions

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2012-2019 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.couchbase.repository.join;
import org.springframework.data.annotation.Id;
/**
* @author Tayeb Chlyah
*/
public class Address {
@Id
String id;
String name;
String country;
public Address(String id, String name, String country) {
this.id = id;
this.name = name;
this.country = country;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getCountry() {
return country;
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2012-2019 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.couchbase.repository.join;
import org.springframework.data.couchbase.core.query.N1qlSecondaryIndexed;
import org.springframework.data.couchbase.repository.CouchbaseRepository;
/**
* @author Tayeb Chlyah
*/
@N1qlSecondaryIndexed(indexName = "addressIndex")
interface AddressRepository extends CouchbaseRepository<Address, String> {
}

View File

@@ -24,6 +24,8 @@ import org.springframework.data.couchbase.core.query.N1qlPrimaryIndexed;
/**
* Author test class for N1QL Join tests
*
* @author Tayeb Chlyah
*/
@N1qlPrimaryIndexed
public class Author {
@@ -36,6 +38,9 @@ public class Author {
@N1qlJoin(on = "lks.name=rks.authorName", fetchType = FetchType.IMMEDIATE)
List<Book> books;
@N1qlJoin(on = "lks.name=rks.name", fetchType = FetchType.IMMEDIATE)
Address address;
public Author(String id, String name) {
this.id = id;
this.name = name;
@@ -56,4 +61,12 @@ public class Author {
public List<Book> getBooks() {
return books;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}

View File

@@ -25,6 +25,7 @@ import org.springframework.test.context.support.DependencyInjectionTestExecution
/**
* Populates author and book documents for N1ql Join tests
*
* @author Tayeb Chlyah
*/
public class AuthorAndBookPopulatorListener extends DependencyInjectionTestExecutionListener {
@@ -44,6 +45,8 @@ public class AuthorAndBookPopulatorListener extends DependencyInjectionTestExecu
Book book = new Book("Book" + i+j, "foo"+i, "");
template.save(book);
}
Address address = new Address("Address" + i, "foo" + i, "bar");
template.save(address);
}
}

View File

@@ -21,28 +21,28 @@ import org.springframework.data.annotation.Id;
* Book test class for N1QL Join tests
*/
public class Book {
@Id
String name;
@Id
String name;
String authorName;
String authorName;
String description;
String description;
public Book(String name, String authorName, String description) {
this.name = name;
this.authorName = authorName;
this.description = description;
}
public Book(String name, String authorName, String description) {
this.name = name;
this.authorName = authorName;
this.description = description;
}
public String getName() {
return this.name;
}
public String getName() {
return this.name;
}
public String getAuthorName() {
return this.authorName;
}
public String getAuthorName() {
return this.authorName;
}
public String getDescription() {
return this.description;
}
public String getDescription() {
return this.description;
}
}

View File

@@ -15,8 +15,6 @@
*/
package org.springframework.data.couchbase.repository.join;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -30,10 +28,13 @@ import org.springframework.data.repository.core.support.RepositoryFactorySupport
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import static org.junit.Assert.*;
/**
* N1ql Join tests
*
* @author Subhashni Balakrishnan
* @author Tayeb Chlyah
*/
@RunWith(ContainerResourceRunner.class)
@ContextConfiguration(classes = IntegrationTestApplicationConfig.class)
@@ -50,11 +51,14 @@ public class N1qlJoinTests {
private AuthorRepository authorRepository;
private AddressRepository addressRepository;
@Before
public void setup() throws Exception {
RepositoryFactorySupport factory = new CouchbaseRepositoryFactory(operationsMapping, indexManager);
bookRepository = factory.getRepository(BookRepository.class);
authorRepository = factory.getRepository(AuthorRepository.class);
addressRepository = factory.getRepository(AddressRepository.class);
}
@Test
@@ -62,8 +66,10 @@ public class N1qlJoinTests {
Author a = authorRepository.findById("Author" + 1).get();
assertTrue(a.books.size() == 5);
for(Book b:a.books) {
assertEquals("Join on author name mismatch", a.name, b.authorName);
assertEquals("Book Join on author name mismatch", a.name, b.authorName);
}
assertNotNull(a.address);
assertEquals("Address Join on author name mismatch", a.name, a.address.name);
}
@Test
@@ -74,5 +80,6 @@ public class N1qlJoinTests {
Author saveda = authorRepository.findById(name).get();
assertTrue(saveda.books.isEmpty());
assertNull(saveda.address);
}
}

View File

@@ -92,6 +92,7 @@ import static org.springframework.data.couchbase.core.support.TemplateUtils.SELE
* @author Simon Baslé
* @author Young-Gu Chae
* @author Mark Paluch
* @author Tayeb Chlyah
*/
public class CouchbaseTemplate implements CouchbaseOperations, ApplicationEventPublisherAware {
@@ -721,7 +722,7 @@ public class CouchbaseTemplate implements CouchbaseOperations, ApplicationEventP
persistentEntity.doWithProperties((PropertyHandler<CouchbasePersistentProperty>) prop -> {
if (prop.isAnnotationPresent(N1qlJoin.class)) {
N1qlJoin definition = prop.findAnnotation(N1qlJoin.class);
TypeInformation type = prop.getTypeInformation().getComponentType();
TypeInformation type = prop.getTypeInformation().getActualType();
Class clazz = type.getType();
N1qlJoinResolver.N1qlJoinResolverParameters parameters = new N1qlJoinResolver.N1qlJoinResolverParameters(definition, id, persistentEntity.getTypeInformation(), type);
if (N1qlJoinResolver.isLazyJoin(definition)) {