Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HSEARCH-3692 Document property bridges
- Loading branch information
Showing
18 changed files
with
1,034 additions
and
11 deletions.
There are no files selected for viewing
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
14 changes: 14 additions & 0 deletions
14
...rg/hibernate/search/documentation/mapper/orm/bridge/propertybridge/ormcontext/MyData.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,14 @@ | ||
/* | ||
* Hibernate Search, full-text search for your domain model | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.search.documentation.mapper.orm.bridge.propertybridge.ormcontext; | ||
|
||
public enum MyData { | ||
|
||
INDEXED, | ||
PROJECTED; | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
...earch/documentation/mapper/orm/bridge/propertybridge/ormcontext/MyDataPropertyBinder.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,56 @@ | ||
/* | ||
* Hibernate Search, full-text search for your domain model | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.search.documentation.mapper.orm.bridge.propertybridge.ormcontext; | ||
|
||
import org.hibernate.Session; | ||
import org.hibernate.search.engine.backend.document.DocumentElement; | ||
import org.hibernate.search.engine.backend.document.IndexFieldReference; | ||
import org.hibernate.search.mapper.orm.HibernateOrmExtension; | ||
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; | ||
|
||
public class MyDataPropertyBinder implements PropertyBinder<MyDataPropertyBinding> { | ||
|
||
@Override | ||
public void bind(PropertyBindingContext context) { | ||
context.getDependencies() | ||
.useRootOnly(); | ||
|
||
context.setBridge( new Bridge( | ||
context.getIndexSchemaElement().field( "myData", f -> f.asString() ).toReference() | ||
) ); | ||
} | ||
|
||
//tag::include[] | ||
private static class Bridge implements PropertyBridge { | ||
|
||
private final IndexFieldReference<String> field; | ||
|
||
private Bridge(IndexFieldReference<String> field) { | ||
this.field = field; | ||
} | ||
|
||
@Override | ||
public void write(DocumentElement target, Object bridgedElement, PropertyBridgeWriteContext context) { | ||
Session session = context.extension( HibernateOrmExtension.get() ) // <1> | ||
.getSession(); // <2> | ||
// ... do something with the session ... | ||
//end::include[] | ||
/* | ||
* I don't know what to do with the session here, | ||
* so I'm just going to extract data from it. | ||
* This is silly, but at least it allows us to check the session was successfully retrieved. | ||
*/ | ||
MyData dataFromSession = (MyData) session.getProperties().get( "test.data.indexed" ); | ||
target.addValue( field, dataFromSession.name() ); | ||
//tag::include[] | ||
} | ||
} | ||
//end::include[] | ||
} |
24 changes: 24 additions & 0 deletions
24
...arch/documentation/mapper/orm/bridge/propertybridge/ormcontext/MyDataPropertyBinding.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,24 @@ | ||
/* | ||
* Hibernate Search, full-text search for your domain model | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.search.documentation.mapper.orm.bridge.propertybridge.ormcontext; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import org.hibernate.search.mapper.pojo.bridge.mapping.annotation.PropertyBinderRef; | ||
import org.hibernate.search.mapper.pojo.bridge.mapping.annotation.declaration.PropertyBinding; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ ElementType.METHOD, ElementType.FIELD }) | ||
@PropertyBinding(binder = @PropertyBinderRef(type = MyDataPropertyBinder.class)) | ||
@Documented | ||
public @interface MyDataPropertyBinding { | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
.../hibernate/search/documentation/mapper/orm/bridge/propertybridge/ormcontext/MyEntity.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,37 @@ | ||
/* | ||
* Hibernate Search, full-text search for your domain model | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.search.documentation.mapper.orm.bridge.propertybridge.ormcontext; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
|
||
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed; | ||
|
||
@Entity | ||
@Indexed | ||
public class MyEntity { | ||
|
||
@Id | ||
@GeneratedValue | ||
private Integer id; | ||
|
||
@MyDataPropertyBinding | ||
private MyData myData; | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public MyData getMyData() { | ||
return myData; | ||
} | ||
|
||
public void setMyData(MyData myData) { | ||
this.myData = myData; | ||
} | ||
} |
Oops, something went wrong.