使用 org.hibernate.id.enhanced.TableGenerator 实现。

package org.example.demo.hibernate;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Persistence;

public class Test {
    public static void main(String[] args) {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("test");
        EntityManager em = factory.createEntityManager();

        Person p = new Person();
        Address a = new Address();

        em.getTransaction().begin();
        em.persist(p);
        em.persist(a);
        em.getTransaction().commit();

        em.close();
        factory.close();
    }

    @Entity(name = "Person")
    public static class Person {
        @Id
        @GeneratedValue(strategy = GenerationType.TABLE)
        private Long id;
    }

    @Entity(name = "Address")
    public static class Address {
        @Id
        @GeneratedValue(strategy = GenerationType.TABLE)
        private Long id;
    }
}

生成 SQL

drop table if exists Address cascade
drop table if exists hibernate_sequences cascade
drop table if exists Person cascade

create table Address (id int8 not null, primary key (id))
create table hibernate_sequences (sequence_name varchar(255) not null, next_val int8, primary key (sequence_name))
create table Person (id int8 not null, primary key (id))
select tbl.next_val from hibernate_sequences tbl where tbl.sequence_name='default' for update of tbl
insert into hibernate_sequences (sequence_name, next_val)  values ('default',1)
update hibernate_sequences set next_val=2  where next_val=1 and sequence_name='default'

select tbl.next_val from hibernate_sequences tbl where tbl.sequence_name='default' for update of tbl
update hibernate_sequences set next_val=3  where next_val=2 and sequence_name='default'

insert into Person (id) values (1)
insert into Address (id) values (2)
package org.example.demo.hibernate;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Persistence;
import javax.persistence.TableGenerator;

public class Test {
    public static void main(String[] args) {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("test");
        EntityManager em = factory.createEntityManager();

        Person p1 = new Person();
        Person p2 = new Person();
        Address a1 = new Address();
        Address a2 = new Address();

        em.getTransaction().begin();
        em.persist(p1);
        em.persist(p2);
        em.persist(a1);
        em.persist(a2);
        em.getTransaction().commit();

        em.close();
        factory.close();
    }

    @Entity(name = "Person")
    public static class Person {
        @Id
        @GeneratedValue(strategy = GenerationType.TABLE, generator = "table")
        @TableGenerator(name = "table", pkColumnValue = "person_id")
        private Long id;
    }

    @Entity(name = "Address")
    public static class Address {
        @Id
        @GeneratedValue(strategy = GenerationType.TABLE, generator = "table")
        @TableGenerator(name = "table", pkColumnValue = "address_id")
        private Long id;
    }
}

生成 SQL

drop table if exists Address cascade
drop table if exists hibernate_sequences cascade
drop table if exists Person cascade

create table Address (id int8 not null, primary key (id))
create table hibernate_sequences (sequence_name varchar(255) not null, next_val int8, primary key (sequence_name))
create table Person (id int8 not null, primary key (id))
select tbl.next_val from hibernate_sequences tbl where tbl.sequence_name='person_id' for update of tbl
insert into hibernate_sequences (sequence_name, next_val)  values ('person_id',1)
update hibernate_sequences set next_val=51  where next_val=1 and sequence_name='person_id'

select tbl.next_val from hibernate_sequences tbl where tbl.sequence_name='person_id' for update of tbl
update hibernate_sequences set next_val=101  where next_val=51 and sequence_name='person_id'

select tbl.next_val from hibernate_sequences tbl where tbl.sequence_name='address_id' for update of tbl
insert into hibernate_sequences (sequence_name, next_val)  values ('address_id',1)
update hibernate_sequences set next_val=51  where next_val=1 and sequence_name='address_id'

select tbl.next_val from hibernate_sequences tbl where tbl.sequence_name='address_id' for update of tbl
update hibernate_sequences set next_val=101  where next_val=51 and sequence_name='address_id'

insert into Person (id) values (1)
insert into Person (id) values (2)
insert into Address (id) values (1)
insert into Address (id) values (2)

results matching ""

    No results matching ""