If you like to generate IDs that are shared (and unique) between multiple persisted objects use the @TableGenerator. H2 and many other databases don't have internal support for sequences so @SequenceGenerator doesn't work.
Here's a quick example to have a unique/shared @Id across two objects:
@Entity
public class Class1 {
// setting pkColumnValue of TableGenerator is very important ;-)
@Id
@TableGenerator(
name = "guid",
initialValue = 0,
allocationSize = 10,
table = "GUID_SEQ",
pkColumnName = "GEN_KEY",
valueColumnName = "GEN_VALUE",
pkColumnValue = "GUID")
@GeneratedValue(strategy = GenerationType.TABLE, generator = "guid")
private long id;
}
@Entity
public class Class2 {
// use the same pkColumnValue
@Id
@TableGenerator(
name = "guid",
initialValue = 0,
allocationSize = 10,
table = "GUID_SEQ",
pkColumnName = "GEN_KEY",
valueColumnName = "GEN_VALUE",
pkColumnValue = "GUID")
@GeneratedValue(strategy = GenerationType.TABLE, generator = "guid")
private long id;
}