I'm trying to set up a Hibernate filter with annotations. I would like to specify it in a base class and make all sub classes use it but whenever I try to enable it, Hibernate fails to find the filter. Is it possible at all to inherit filter annotations?
我正在尝试使用注释设置Hibernate过滤器。我想在基类中指定它并使所有子类使用它但是每当我尝试启用它时,Hibernate都无法找到过滤器。是否有可能继承过滤器注释?
3 个解决方案
#1
Are you using the hibernate filter directly, or are you extending the hibernate filter for your own purposes? Annotations aren't inherited by default in Java, although if you were writing the annotation yourself, and I'm assuming hibernate didn't do this on their annotations, you can specify the @Inherited meta-annotation on your annotation to make it inherit. This only works for Type level annotations, though. Also, some people write an annotation manager to simulate full inheritance. You could do something like that, either extend hibernates mechanism or write a preprocessor that adds the annotations where they should have been inherited.
你是直接使用hibernate过滤器,还是为了自己的目的扩展hibernate过滤器?默认情况下,Java中没有继承注释,但如果您自己编写注释,并且我假设hibernate没有在注释上执行此操作,则可以在注释上指定@Inherited元注释以使其继承。但这仅适用于类型级别注释。此外,有些人编写注释管理器来模拟完全继承。您可以执行类似的操作,或者扩展hibernates机制,或者编写一个预处理器来添加应该继承的注释。
#2
Since 3.5.0 it's at least possible for @MappedSuperclass. Not sure if that helps you... see: HHH-4332
从3.5.0开始,@MappedSuperclass至少可以实现。不确定这有助于你......见:HHH-4332
#3
What John Ellinwood wrote is precisely the case. That is why @Filter
needs to be specified on all subclasses, at least in Hibernate Annotations 3.4.0-GA.
John Ellinwood写的正是如此。这就是为什么需要在所有子类上指定@Filter的原因,至少在Hibernate Annotations 3.4.0-GA中是这样。
Furthermore, although that might not be apparent, you will need the same annotation on all mapped collections of that class, if you expect those collections to be susceptible to filtering as well.
此外,尽管这可能并不明显,但如果您希望这些集合也易于过滤,则需要在该类的所有映射集合上使用相同的注释。
#1
Are you using the hibernate filter directly, or are you extending the hibernate filter for your own purposes? Annotations aren't inherited by default in Java, although if you were writing the annotation yourself, and I'm assuming hibernate didn't do this on their annotations, you can specify the @Inherited meta-annotation on your annotation to make it inherit. This only works for Type level annotations, though. Also, some people write an annotation manager to simulate full inheritance. You could do something like that, either extend hibernates mechanism or write a preprocessor that adds the annotations where they should have been inherited.
你是直接使用hibernate过滤器,还是为了自己的目的扩展hibernate过滤器?默认情况下,Java中没有继承注释,但如果您自己编写注释,并且我假设hibernate没有在注释上执行此操作,则可以在注释上指定@Inherited元注释以使其继承。但这仅适用于类型级别注释。此外,有些人编写注释管理器来模拟完全继承。您可以执行类似的操作,或者扩展hibernates机制,或者编写一个预处理器来添加应该继承的注释。
#2
Since 3.5.0 it's at least possible for @MappedSuperclass. Not sure if that helps you... see: HHH-4332
从3.5.0开始,@MappedSuperclass至少可以实现。不确定这有助于你......见:HHH-4332
#3
What John Ellinwood wrote is precisely the case. That is why @Filter
needs to be specified on all subclasses, at least in Hibernate Annotations 3.4.0-GA.
John Ellinwood写的正是如此。这就是为什么需要在所有子类上指定@Filter的原因,至少在Hibernate Annotations 3.4.0-GA中是这样。
Furthermore, although that might not be apparent, you will need the same annotation on all mapped collections of that class, if you expect those collections to be susceptible to filtering as well.
此外,尽管这可能并不明显,但如果您希望这些集合也易于过滤,则需要在该类的所有映射集合上使用相同的注释。