-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HSEARCH-5235 WIP: draft of injecting field references
- Loading branch information
1 parent
204633f
commit a6d1f74
Showing
49 changed files
with
1,004 additions
and
59 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
.../hibernate/search/documentation/mapper/orm/binding/document/model/dsl/injection/Book.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.search.documentation.mapper.orm.binding.document.model.dsl.injection; | ||
|
||
import jakarta.persistence.Convert; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
|
||
import org.hibernate.search.documentation.testsupport.data.ISBN; | ||
import org.hibernate.search.documentation.testsupport.data.ISBNAttributeConverter; | ||
import org.hibernate.search.mapper.pojo.bridge.mapping.annotation.PropertyBinderRef; | ||
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.GenericField; | ||
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed; | ||
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.PropertyBinding; | ||
|
||
@Entity | ||
@Indexed | ||
public class Book { | ||
|
||
@Id | ||
@GeneratedValue | ||
@GenericField | ||
private Integer id; | ||
|
||
@Convert(converter = ISBNAttributeConverter.class) | ||
@PropertyBinding(binder = @PropertyBinderRef(type = ISBNBinder.class)) | ||
private ISBN isbn; | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public ISBN getIsbn() { | ||
return isbn; | ||
} | ||
|
||
public void setIsbn(ISBN isbn) { | ||
this.isbn = isbn; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...cumentation/mapper/orm/binding/document/model/dsl/injection/DocumentModelDslInjectIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.search.documentation.mapper.orm.binding.document.model.dsl.injection; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmUtils.with; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import jakarta.persistence.EntityManagerFactory; | ||
|
||
import org.hibernate.search.documentation.testsupport.BackendConfigurations; | ||
import org.hibernate.search.documentation.testsupport.DocumentationSetupHelper; | ||
import org.hibernate.search.documentation.testsupport.data.ISBN; | ||
import org.hibernate.search.mapper.orm.Search; | ||
import org.hibernate.search.mapper.orm.session.SearchSession; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
class DocumentModelDslInjectIT { | ||
@RegisterExtension | ||
public DocumentationSetupHelper setupHelper = DocumentationSetupHelper.withSingleBackend( BackendConfigurations.simple() ); | ||
|
||
private EntityManagerFactory entityManagerFactory; | ||
|
||
@BeforeEach | ||
void setup() { | ||
entityManagerFactory = setupHelper.start().setup( Book.class ); | ||
} | ||
|
||
@Test | ||
void smoke() { | ||
with( entityManagerFactory ).runInTransaction( entityManager -> { | ||
Book book = new Book(); | ||
book.setIsbn( ISBN.parse( "978-0-58-600835-5" ) ); | ||
entityManager.persist( book ); | ||
} ); | ||
|
||
with( entityManagerFactory ).runInTransaction( entityManager -> { | ||
SearchSession searchSession = Search.session( entityManager ); | ||
|
||
List<Book> result = searchSession.search( Arrays.asList( Book.class ) ) | ||
.where( f -> f.match().field( "isbn" ).matching( "978-0-58-600835-5" ) ) | ||
.fetchHits( 20 ); | ||
|
||
assertThat( result ).hasSize( 1 ); | ||
} ); | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
...nate/search/documentation/mapper/orm/binding/document/model/dsl/injection/ISBNBinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.search.documentation.mapper.orm.binding.document.model.dsl.injection; | ||
|
||
import org.hibernate.search.documentation.testsupport.data.ISBN; | ||
import org.hibernate.search.engine.backend.document.DocumentElement; | ||
import org.hibernate.search.engine.backend.document.IndexFieldReference; | ||
import org.hibernate.search.mapper.pojo.bridge.PropertyBridge; | ||
import org.hibernate.search.mapper.pojo.bridge.binding.PropertyBindingContext; | ||
import org.hibernate.search.mapper.pojo.bridge.mapping.programmatic.PropertyBinder; | ||
import org.hibernate.search.mapper.pojo.bridge.runtime.PropertyBridgeWriteContext; | ||
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.KeywordField; | ||
|
||
//tag::bind[] | ||
public class ISBNBinder implements PropertyBinder { | ||
|
||
@KeywordField(normalizer = "isbn") | ||
IndexFieldReference<String> isbn; | ||
|
||
IndexFieldReference<String> isbnNoAnnotation; | ||
|
||
@Override | ||
public void bind(PropertyBindingContext context) { | ||
context.dependencies() | ||
.useRootOnly(); | ||
|
||
context.bridge( // <6> | ||
ISBN.class, // <7> | ||
new ISBNBridge( isbn ) // <8> | ||
); | ||
} | ||
//end::bind[] | ||
|
||
//tag::write[] | ||
private static class ISBNBridge implements PropertyBridge<ISBN> { | ||
|
||
private final IndexFieldReference<String> fieldReference; | ||
|
||
private ISBNBridge(IndexFieldReference<String> fieldReference) { | ||
this.fieldReference = fieldReference; | ||
} | ||
|
||
@Override | ||
public void write(DocumentElement target, ISBN bridgedElement, PropertyBridgeWriteContext context) { | ||
String indexedValue = /* ... (extraction of data, not relevant) ... */ | ||
//end::write[] | ||
bridgedElement.getStringValue(); | ||
//tag::write[] | ||
target.addValue( this.fieldReference, indexedValue ); // <1> | ||
} | ||
} | ||
//end::write[] | ||
//tag::bind[] | ||
} | ||
//end::bind[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.