Skip to content

Instantly share code, notes, and snippets.

@Sam-Kruglov
Created January 9, 2018 11:31
Show Gist options
  • Save Sam-Kruglov/8a2b859ad6e7747f6e4669cd7b07d948 to your computer and use it in GitHub Desktop.
Save Sam-Kruglov/8a2b859ad6e7747f6e4669cd7b07d948 to your computer and use it in GitHub Desktop.
Create PostgreSQL sequence generator
/**
* Defines common Id Generator that use PostgreSQL sequences properly.
* It uses a separate sequence for the chosen ID, as opposed to using a single sequence for all IDs;
* the same result can be achieved by simply using {@code @GeneratedValue(strategy = GenerationType.IDENTITY)}, but
* that way hibernate would use a sequence as an identity, which would decrease performance.
* <p>
* Generates a sequence for each entity "entityName_seq"
*/
@GenericGenerator(name = "SequencePerEntityGenerator", strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
parameters = @Parameter(name = "prefer_sequence_per_entity", value = "true"))
package com.samkruglov.entities;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment