From 97fc4a047182d658befae37889d5477c02419639 Mon Sep 17 00:00:00 2001 From: ctayeb Date: Fri, 22 Feb 2019 13:00:41 +0100 Subject: [PATCH] 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. --- .../couchbase/repository/join/Address.java | 49 +++++++++++++++++++ .../repository/join/AddressRepository.java | 26 ++++++++++ .../couchbase/repository/join/Author.java | 13 +++++ .../join/AuthorAndBookPopulatorListener.java | 3 ++ .../data/couchbase/repository/join/Book.java | 36 +++++++------- .../repository/join/N1qlJoinTests.java | 13 +++-- .../couchbase/core/CouchbaseTemplate.java | 3 +- 7 files changed, 121 insertions(+), 22 deletions(-) create mode 100644 src/integration/java/org/springframework/data/couchbase/repository/join/Address.java create mode 100644 src/integration/java/org/springframework/data/couchbase/repository/join/AddressRepository.java diff --git a/src/integration/java/org/springframework/data/couchbase/repository/join/Address.java b/src/integration/java/org/springframework/data/couchbase/repository/join/Address.java new file mode 100644 index 00000000..bff978bd --- /dev/null +++ b/src/integration/java/org/springframework/data/couchbase/repository/join/Address.java @@ -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; + } +} diff --git a/src/integration/java/org/springframework/data/couchbase/repository/join/AddressRepository.java b/src/integration/java/org/springframework/data/couchbase/repository/join/AddressRepository.java new file mode 100644 index 00000000..b1c1e83d --- /dev/null +++ b/src/integration/java/org/springframework/data/couchbase/repository/join/AddressRepository.java @@ -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 { +} \ No newline at end of file diff --git a/src/integration/java/org/springframework/data/couchbase/repository/join/Author.java b/src/integration/java/org/springframework/data/couchbase/repository/join/Author.java index f79592ed..a2b2c9db 100644 --- a/src/integration/java/org/springframework/data/couchbase/repository/join/Author.java +++ b/src/integration/java/org/springframework/data/couchbase/repository/join/Author.java @@ -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 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 getBooks() { return books; } + + public Address getAddress() { + return address; + } + + public void setAddress(Address address) { + this.address = address; + } } \ No newline at end of file diff --git a/src/integration/java/org/springframework/data/couchbase/repository/join/AuthorAndBookPopulatorListener.java b/src/integration/java/org/springframework/data/couchbase/repository/join/AuthorAndBookPopulatorListener.java index 20f1f363..99223b44 100644 --- a/src/integration/java/org/springframework/data/couchbase/repository/join/AuthorAndBookPopulatorListener.java +++ b/src/integration/java/org/springframework/data/couchbase/repository/join/AuthorAndBookPopulatorListener.java @@ -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); } } diff --git a/src/integration/java/org/springframework/data/couchbase/repository/join/Book.java b/src/integration/java/org/springframework/data/couchbase/repository/join/Book.java index c607c1ce..f080b9c6 100644 --- a/src/integration/java/org/springframework/data/couchbase/repository/join/Book.java +++ b/src/integration/java/org/springframework/data/couchbase/repository/join/Book.java @@ -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; + } } \ No newline at end of file diff --git a/src/integration/java/org/springframework/data/couchbase/repository/join/N1qlJoinTests.java b/src/integration/java/org/springframework/data/couchbase/repository/join/N1qlJoinTests.java index 5d3516d9..6dd9d406 100644 --- a/src/integration/java/org/springframework/data/couchbase/repository/join/N1qlJoinTests.java +++ b/src/integration/java/org/springframework/data/couchbase/repository/join/N1qlJoinTests.java @@ -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); } } \ No newline at end of file diff --git a/src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplate.java b/src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplate.java index b9a291d5..dac1ed7d 100644 --- a/src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplate.java +++ b/src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplate.java @@ -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) 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)) {