Creating custom annotation as a shorcut for @FullTextField with many params

The idea is to have simple custom annotaion, let’s call it LocalizedFieldIndexed and it should be a shortcut for

@FullTextField(
    projectable = Projectable.NO,
    searchable = Searchable.YES,
    analyzer = "customAnalyzer",
    valueBridge = @ValueBridgeRef( type = LocalizedFieldBridge.class )
)
private LocalizedField name;

But when I try to create custom annotation there is an error reported on @FullTextField. What am I doing wrong?

The annotation @FullTextField is disallowed for this location

I mean I know what’s wrong here - @FullTextField is meant to be used only by class properties, but is there any other way to create my custom annotation?

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@FullTextField(
    projectable = Projectable.NO,
    searchable = Searchable.YES,
    analyzer = "customAnalyzer",
    valueBridge = @ValueBridgeRef( type = LocalizedFieldBridge.class )
)
public @interface LocalizedFieldIndexed {

	//

}

@FullTextField is not a meta-annotation, it cannot be used on annotations.

What you can do, however, is declare your own annotation and the corresponding annotation processor, which will do exactly what you want.

See https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#mapper-orm-custom-annotations

1 Like

My knowledge is far below annotations. Of course I tried, but no success. Tried to learn too, but can’t find any decent documentation that would explain in detail.

When I try to boot up my app, Hibernate is not happy at all. Annotation is added inside my Article class on name property but exception is at class PriceInfo which is also a property of Article. That tells me my annotation made a mess.

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD })
@PropertyMapping(
	processor = @PropertyMappingAnnotationProcessorRef(
		type = LocalizedFieldIndexed.CustomPropertyMappingAnnotationProcessor.class
	)
)
@Documented
public @interface LocalizedFieldIndexed {

	FullTextField fullTextField() default @FullTextField(
		projectable = Projectable.NO,
		searchable = Searchable.YES,
		analyzer = "customAnalyzer",
		valueBridge = @ValueBridgeRef(type = LocalizedFieldBridge.class)
	);

	public class CustomPropertyMappingAnnotationProcessor
		implements PropertyMappingAnnotationProcessor<LocalizedFieldIndexed> {

		@Override
		public void process(
			final PropertyMappingStep mapping,
			final LocalizedFieldIndexed annotation,
			final PropertyMappingAnnotationProcessorContext context
		) {
			//
		}

	}

}

You didn’t provide the full code of your annotation processor (there’s an empty comment right in the middle?) nor the stack trace. I can’t help without information…

Hi!
Thanks for your answer. There is no code because I have no idea what to do inside procesor LOL
But nevermind, I’ll be happy with original Hibernate Search annotations, that’s not a bad thing.

Thanks!
BR,
Hrvoje

I don’t know what to say, I already pointed you to the docs here and that doc points you to examples here. You can copy-paste that and adapt it to your needs.

If you think that’s not “decent” documentation then I’m afraid that’ll have to do regardless, because that’s the best I can do.

No, no, no! When I was writing “decent documentation”, I had general annotations in mind, not annotations of Hibermate or Hibernate Search.
I am aware we’re not all of the same skills, and I’ll have to invest more time to analyze and learn about it. I’m sure I’ll sort it out at some point, but right now, I’ll put it on TODO list.
Please accept my apologies! It was not my intention to be rude, especially to you because you helped me with a million of good advices and hints and they all wre of highest quality

No problem, sorry for the misunderstanding. FWIW, this concept of annotation processor is completely specific to Hibernate Search. (there’s also something called “annotation processors” in the Java compiler but… let’s just say I don’t want to be anywhere near that API ;D)

Anyway, let me know if you have other issues when you give it another try :slight_smile:

1 Like