JPA, HIBERNATE. How to get the following value from @TableGenerator

I have an entity class with the following primary key generation strategy

@Id
@Column(name = "id", nullable = false)
@TableGenerator(name = "USERGENERATOR", table = "my_sequences", pkColumnName = "sequence_name", pkColumnValue = "user_id", valueColumnName = "next_value")
@GeneratedValue(strategy = GenerationType.TABLE, generator = "USERGENERATOR")
protected Integer id;

This worked fine until a new requirement came up, when I needed to insert a new row using my own query. Auto_increment is not used in the primary key column because of the Entity Inheritance strategy (@Inheritance (strategy = InheritanceType.TABLE_PER_CLASS)).

I was wondering if there is a way to ask the table generator for the next value using EntityManager.

+4
source share
2 answers

, @TableGenerator API, SQL . :

-- get the current value
select next_value from my_sequences where sequence_name = 'user_id';
-- update value
update my_sequences set next_value = next_value + 1;

, @TableGenerator .

0

@GeneratedValue(strategy = GenerationType.TABLE, generator = "USERGENERATOR") @GeneratedValue(strategy = GenerationType.IDENTITY) , , 1

0

Source: https://habr.com/ru/post/1653154/


All Articles