# Runtime polymophism with TypeBinder bridge

**URL:** <https://discourse.hibernate.org/t/runtime-polymophism-with-typebinder-bridge/6057>\
**Category:** Hibernate Search\
**Created:** [February 18, 2022, 9:25am UTC](https://discourse.hibernate.org/t/runtime-polymophism-with-typebinder-bridge/6057 "2022-02-18T09:25:58Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [February 21, 2022, 12:54pm UTC](https://discourse.hibernate.org/t/runtime-polymophism-with-typebinder-bridge/6057/2 "2022-02-21T12:54:23Z")

</div>

Hello,

This is basically the same problem as the one exposed in [this post](https://discourse.hibernate.org/t/propertybinder-for-base-type-reports-no-children-property-found-in-parent-class/5493).

In short, the API is not designed to allow what you want to do, but there are workarounds.

I opened [[HSEARCH-4488] - Hibernate JIRA](https://hibernate.atlassian.net/browse/HSEARCH-4488) to provide ways to address this use case without hacks.  
Also, related: [[HSEARCH-438] - Hibernate JIRA](https://hibernate.atlassian.net/browse/HSEARCH-438)

This answer might work for you:

> [@PropertyBinder for base type reports no children property found in parent class](https://discourse.hibernate.org/t/propertybinder-for-base-type-reports-no-children-property-found-in-parent-class/5493/2):
>
> Otherwise, yet another hack that could work, but that I’ve never tried: you could leverage [`fromOtherEntity`](https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#mapper-orm-bridge-bridgedelement-dependencies-fromOtherEntity) in a way that it was never intended to be used. You could declare a `getSelf()` getter in `PublicationEntity` , and override it in each subtype to return that subtype (e.g. `PublicationEntity getSelf() {...}` in `PublicationEntity` , `JournalPublicationEntity getSelf() {...}` in `JournalPublicationEntity` , etc.). Don’t forget to annotate the getter with `@javax.persistence.Transient` to avoid problems with Hibernate ORM. Then you should be able to do this:
> 
> ```auto
> public class PublicationBinder implements PropertyBinder {
> @Override
> public void bind(PropertyBindingContext context) {
> context.dependencies()
> .use("issn").use("publisher").use("year"); // publication basic
> context.dependencies().fromOtherEntity( JournalPublicationEntity.class, "self" )
> .use("title") // fields inside both in journal and conference
> .use("number").use("volumn"); // journal specific
> context.dependencies().fromOtherEntity( ConferencePublicationEntity.class, "self" )
> .use("title") // fields inside both in journal and conference
> .use("abstractNumber"); // conference specific
> 
> ```
> 
> I’d recommend testing that changing a `title` actually triggers reindexing, because this is really a hack and I’ve never tried it. But in theory, it could work…

Adapting to your code:

```auto

// No field to search here
@Inheritance(strategy = InheritanceType.JOINED)
@TypeBinding(binder = @TypeBinderRef(type = FullNameBinder.class))
public abstract class AbstractEntity {

  @OneToOne(mappedBy = "parentEntity")
   private EntityToSearch entityToSearch ;
  //other fields

  // Hack, see https://discourse.hibernate.org/t/runtime-polymophism-with-typebinder-bridge/6057
  @Transient
  public abstract AbstractEntity getSelf();
}

@PrimaryKeyJoinColumn(name = "concreteClassA _id")
ConcreteClassA extends AbstractEntity {
  
  //Field to search 
  // @KeywordField // NOTE: Commented out because you don't need this if you already index fullName in your bridge.
  private String fullName;

  // Hack, see https://discourse.hibernate.org/t/runtime-polymophism-with-typebinder-bridge/6057
  @Override
  public ConcreteClassA getSelf() {
    return this;
  }
}

public class FullNameBinder implements TypeBinder {
    @Override
    public void bind(final TypeBindingContext context) {
       IndexFieldReference<String> fullName= context.indexSchemaElement().field("fullName", IndexFieldTypeFactory::asString).toReference();
        // Hack, see https://discourse.hibernate.org/t/runtime-polymophism-with-typebinder-bridge/6057
        context.dependencies().fromOtherEntity( ConcreteClassA.class, "self" )
            .use("fullName");
       context.bridge(AbstractEntity.class, new MyBridge(fullName));
    }

private static class MyBridge implements TypeBridge<AbstractEntity> {

		private final IndexFieldReference<String> fullName;

		private MyBridge(IndexFieldReference<String> fullNameField) {
			this.fullName= fullNameField;
		}

		@Override
		public void write(DocumentElement target, AbstractEntity abstractEntity , TypeBridgeWriteContext context) {
			if (abstractEntity instanceof ConcreteClassA ) {
				target.addValue(this.fullName, ((ConcreteClassA ) abstractEntity ).getFullName());
			}
		}

	}
}

```

---

_[View the full topic](https://discourse.hibernate.org/t/runtime-polymophism-with-typebinder-bridge/6057)._
