We have a @MappedSuperclass class Entity
which defines the default id and the generator:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Override
public K getId() {
return id;
}
I want to not use any generator in subclass Foo
at all, because I will provide the primary key myself.
I must extend Foo
from the base class Entity
, because a lot and a lot functions depend on this base class. The annotation @AttributeOverrides
seem开发者_C百科s not support to override @GeneratedValue
.
Sounds to me like Foo
is not actually a subtype of Entity
and should never have really extended it. Inheritance is a strong contract. You would not extend a something just to get access to some methods.
In hibernate when you get a Foo object from the database you are actually also selecting from the Entity table. Both tables are joined via their @Id
field, you will see the the Id
in the Foo
will be the same as the Id
in Entity
.
This enables us to do the following.
Entity entity = new Foo();
if (entity instanceof Foo) {
//we have a Foo object
} else if( entity instanceof ...) {
//we have a ...
}
If you want to supply your own Id you should no longer extend the super class. If you have a whole lot of logic you want to include into a number of hibernate classes but they are not really subtypes of the class, consider using @Embedded
instead.
精彩评论