Dynamic Addition of Fields in Entity class Based on db columns

Hi @yrodiere

I have a scenario where i dont want to map the field( existing in database column) in VO , I want my query to run smoothly at runtime without complaining the mapping of that no existing column in vo
Example:
@Entity
class User{
@Id
@Column(name = ID_COLUMN, nullable = false, length = 63)
@FullTextField(name = ID_COLUMN)
@DocumentId
private String id;

@Column(name = DOMAIN_COLUMN, length = 15)
@KeywordField(name = DOMAIN_COLUMN,normalizer = "lowercase")
@Enumerated(EnumType.STRING)
private DomainEnum dataDomain;

@Column(name = SOURCE_TYPE_COLUMN, length = 15)
@KeywordField(name =SOURCE_TYPE_COLUMN,normalizer = "lowercase")
@Enumerated(EnumType.STRING)
private SourceTypeEnum sourceType;


@Column(name = STATUS_COLUMN, length = 15)
@Enumerated(EnumType.STRING)
@KeywordField(name = STATUS_COLUMN,normalizer = "lowercase")
private EntityStatus status;

}
in the above vo i configured the fields i want , and these fields are also present in table , but lets assume if i add one more column named sourceData of type varchar in db table , but i will not add mapping of that field in vo , i want that to happen dynamically at runtime , automatic addition of field in vo , and any query to run smoothly on this entity without error ,
is there any way to handle this , please help
Thanks

Hello,

I’m not sure I understand what you want. In particular I don’t know what you mean by “VO”. So I’ll just state some facts, maybe that will help you:

  1. Hibernate Search was definitely not designed to “automatically”, “dynamically” add fields to your schema, at least not by itself. A core assumption is that you manually annotate each property that you need indexed. If you don’t want to, you will have to implement the “mapping automation” yourself.
  2. You cannot change the mapping at runtime. Once Hibernate ORM and Hibernate Search started, the mapping are final.
  3. You can change the mapping between two startups, but (with Elasticsearch) you will still need to update the Elasticsearch schema after a mapping update; see this section of the documentation.
  4. You can define a mapping without annotations, using the programmatic mapping API. Using this API, on application startup, you could manually go through the Hibernate ORM metamodel, or your own metamodel, and define Hibernate Search fields as you see fit, potentially on every single property. That way the mapping may change “automatically” as you add properties to your entities.

Hi @yrodiere ,
Thanks for the reply ,by vo i meant Entity class , I explored the programmatic mapping api , but that is for index and searchable fields , but i have to implement that only for hibernate entity mapping with @column in that , so do we have any mechanism to detect at runtime , that the db query is having fields that are not mapped in entity class, so thats get added automatically in entity class .

In short why we need this feature is like we have to provide release on prod , everytime even if the field is not searchable(not in hibernate search/elastic search,only in hibernate) , so to avoid release on prod when the field is not indexed in elastic but only added in db column , we need this dynamic mechanism to handle dynamic addition of that type of fields everytime in entity mapping automatically.

Thanks,

There is no support in Hibernate Search to ignore fields that are not indexed when creating predicates. Hibernate Search uses metadata about the fields in order to build the predicates, so if the metadata is missing, it just cannot build the predicates.

That being said, you have complete control over which fields you target in a query. So just be careful about which fields you include in a query; if a field is not indexed, just don’t query that field.

You have access to a metamodel to check whether a field exists or not: see this section of the documentation.

1 Like