Dear future reader: After working through Sannes answer and the link he provides, I decided to go with a Sequence instead. Newer Hibernates take care that it works on MySQL as well.
I have an abstract
class that contains some basic values that I want to have in all my entities (such as id, creation date, creation user, etc.). Currently I struggle with the combination of having a table per concrete class and having its id work.
It seems I can either have a table per class OR a single place where I configure the ID, but not both. That seems very odd and unlikely to me, so I want to make sure that this is the case.
Versions that might be relevant: hibernate 5, mysql 5.5, java 1.8, spring 4.3.
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@MappedSuperclass
public abstract class GeneralData implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false)
protected int id;
protected String name;
// getters, setters, constructors, etc.
}
Example subclass:
@Entity
public class ExampleClass extends GeneralData {
// own values, constructors, getters, setters, etc.
}
Error I get:
Cannot use identity column key generation with <union-subclass> mapping for: org.example.ExampleClass
Things I've tried:
- I have tried to make
GeneralData
an@Entity
, but that didn't help. - I tried to use a
GenerationType.TABLE
but that failed because the key is too big. (Also, I'd rather not have this, IDENTITY is what I want) Error:Specified key was too long; max key length is 1000 bytes
I'd rather not move the id column to each and every concrete class. I'm totally fine if the ID is unique only for a single table and not via the whole dependency tree.
If I should upgrade my libs, database, etc. please keep in mind that such can only be done with a lot of extra work (for internal reasons). This would be a last resort and I'd like to see some evidence that it helps first.