@MappedSuperclass
public abstract class PostgresDomainObject {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(insertable=false, updatable=false,
columnDefinition="BigSerial not null")
public long id;
...
}
The BigSerial data type in Postgres is shorthand for the following:
CREATE SEQUENCE tablename_colname_seq;
CREATE TABLE tablename (
colname integer NOT NULL DEFAULT nextval('tablename_colname_seq')
);
ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname
The key to making this work is
GenerationType.IDENTITY
. The insertable
and updatable
attributes don't seem to actually work - I've left them in as a indication of how the column behaves, but they don't seem to have any effect (at least not without IDENTITY
). But using IDENTITY
as the generation type causes Hibernate to not include the id column in insert statements, meaning that the default will get used.The really beauty of this is that classes that extend PostgresDomainObject and declare
@Entity
will get their own dedicated sequence. If I had declared the column as a bigint and used GenerationType.AUTO
, it would have created a single sequence, called "hibernate_sequence", and that sequence would have been used by every table. If I had declared the base class as @Entity
and used the Hibernate/JPA table inheritance model (an atrocity I may rant about in some future post), I would again have been limited to using a single shared sequence.There are two good reasons for wanting to have a dedicated sequence for each table instead of a single shared one:
1) Performance - having sequence generators dedicated to each to table helps to prevents contention when there are multiple concurrent requests for new sequence numbers
2) Maintainability - sequences are often allocated in batches (say, 50 at a time) and some errors can cause the entire batch to be skipped. If there is a recurring error on a busy system, it's even possible to exhaust the sequence. It's also possible to exhaust the sequence on a very large table that has a lot of turnover. In those cases, unlikely as they may be, it's a lot easier to track down the problem and fix it if it's a dedicated sequence.
If the database in question is a simple, low-volume data store, none of this matters much. But the systems I care about are at the other end of the spectrum - either high-volume OLTP systems or data warehouses that have tables with millions of rows and high levels of concurrent access.
Awesome! Should be posted to SO.
ReplyDeleteThanks! Now I can run my application on MySQL and Postgres without changing the GenerationType.
ReplyDeleteIt is not working for primary and foreign key.
ReplyDeleteDo you have any idea that how to generate sequence id for primary and foreign key table ?
I checked my code and found some mistake but It is working......
ReplyDeleteGood article !!!!!!!!!!
Hi, I totally agree with you, the single sequence makes really no sense.
ReplyDeletePerfect explanation!
Thanks
Great content thanks for sharing this informative blog which provided me technical information keep posting.
ReplyDeleteSelenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training
Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts, have a nice weekend!keep it up!!!
ReplyDeleteandroid training in chennai
android online training in chennai
android training in bangalore
android training in hyderabad
android Training in coimbatore
android training
android online training
cami avizesi - no deposit bonus forex 2021 - takipçi satın al - takipçi satın al - takipçi satın al - takipcialdim.com/tiktok-takipci-satin-al/ - instagram beğeni satın al - instagram beğeni satın al - google haritalara yer ekleme - btcturk - tiktok izlenme satın al - sms onay - youtube izlenme satın al - google haritalara yer ekleme - no deposit bonus forex 2021 - tiktok jeton hilesi - tiktok beğeni satın al - binance - takipçi satın al - uc satın al - finanspedia.com - sms onay - sms onay - tiktok takipçi satın al - tiktok beğeni satın al - twitter takipçi satın al - trend topic satın al - youtube abone satın al - instagram beğeni satın al - tiktok beğeni satın al - twitter takipçi satın al - trend topic satın al - youtube abone satın al - instagram beğeni satın al - tiktok takipçi satın al - tiktok beğeni satın al - twitter takipçi satın al - trend topic satın al - youtube abone satın al - instagram beğeni satın al - perde modelleri - instagram takipçi satın al - takipçi satın al - instagram takipçi satın al - betboo
ReplyDeleteThis post is so interactive and informative.keep update more information...
Salesforce Training in Chennai
Salesforce Training in Anna Nagar
en son çıkan perde modelleri
ReplyDeleteen son çıkan perde modelleri
uc satın al
özel ambulans
yurtdışı kargo
minecraft premium
lisans satın al
nft nasıl alınır
Good content. You write beautiful things.
ReplyDeletesportsbet
vbet
hacklink
sportsbet
taksi
mrbahis
hacklink
mrbahis
vbet
Success Write content success. Thanks.
ReplyDeletebetmatik
betturkey
deneme bonusu
canlı poker siteleri
kralbet
betpark
canlı slot siteleri
amasya
ReplyDeleteantakya
edirne
elazığ
kayseri
TSİO2R
kars
ReplyDeletekütahya
aydın
balıkesir
bitlis
WPKZB
دهانات خارجية
ReplyDeleteدهانات
شركة مكافحة الحشرات بالاحساء DI1UNYqQHq
ReplyDelete