Indeed, this is not what I expected after reading javadoc @TableGenerator :
This annotation defines a primary key generator that can be referenced by name when a generator element is specified for the GeneratedValue annotate. A table generator can be specified in an entity class or in a field or property of a primary key. The scope of the generator name is global for the storage unit (for all types of generators).
Maybe I'm not interpreting correctly (section 9.1.38 of the JPA specification does not tell us anymore), but I expected that I could reference the generator name from another Entity in @GeneratedValue .
In any case, it seems to work. In the Dog object:
@TableGenerator(table = "myseq", name = "dog_gen", pkColumnName = "seq_name", valueColumnName = "seq_val", pkColumnValue = "Dog", allocationSize = 1) @Id @GeneratedValue(strategy = GenerationType.TABLE, generator = "dog_gen") private Long id;
And in Cat , well, the same thing:
@TableGenerator(table = "myseq", name = "dog_gen", pkColumnName = "seq_name", valueColumnName = "seq_val", pkColumnValue = "Dog", allocationSize = 1) @Id @GeneratedValue(strategy = GenerationType.TABLE, generator = "dog_gen") private Long id;
Pascal thivent
source share