Entity primary keys are now forcibly sequence generated

This commit is contained in:
neviyn 2021-05-09 16:00:34 +01:00
parent 87303d1ad7
commit ca567b2e60
3 changed files with 42 additions and 24 deletions

View File

@ -11,6 +11,7 @@ import javax.persistence.JoinColumn
import javax.persistence.JoinTable
import javax.persistence.ManyToMany
import javax.persistence.ManyToOne
import javax.persistence.SequenceGenerator
import javax.validation.constraints.Pattern
@Entity
@ -30,14 +31,20 @@ open class Member(
inverseJoinColumns = [JoinColumn(name = "role_id", referencedColumnName = "id")]
)
open var roles: MutableSet<Role> = mutableSetOf(),
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) open var id: Long = -1
@Id
@SequenceGenerator(name="member_id_seq", sequenceName = "member_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "member_id_seq")
open var id: Long = -1
)
@Entity
open class Role(
@Column(unique = true)
open var name: String = "",
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) open var id: Long = -1
@Id
@SequenceGenerator(name="role_id_seq", sequenceName = "role_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "role_id_seq")
open var id: Long = -1
)
@Entity
@ -54,7 +61,10 @@ open class Image(
inverseJoinColumns = [JoinColumn(name = "tag_id", referencedColumnName = "id")]
)
open var tags: MutableSet<Tag> = mutableSetOf(),
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) open var id: Long = -1
@Id
@SequenceGenerator(name="image_id_seq", sequenceName = "image_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "image_id_seq")
open var id: Long = -1
)
@Entity
@ -62,5 +72,8 @@ open class Tag(
@Column(unique = true)
@Pattern(regexp = "[a-zA-Z0-9_]*") // Only allow alphanumeric and underscores
open var tag: String = "",
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) open var id: Long = -1
@Id
@SequenceGenerator(name="tag_id_seq", sequenceName = "tag_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "tag_id_seq")
open var id: Long = -1
)

View File

@ -1,34 +1,34 @@
DROP SCHEMA IF EXISTS booru CASCADE ;
DROP SCHEMA IF EXISTS booru CASCADE;
CREATE SCHEMA booru;
CREATE TABLE booru.member
(
id bigint NOT NULL PRIMARY KEY,
name varchar NOT NULL UNIQUE,
email varchar NOT NULL,
password varchar NOT NULL,
enabled boolean
id bigserial NOT NULL PRIMARY KEY,
name varchar NOT NULL UNIQUE,
email varchar NOT NULL,
password varchar NOT NULL,
enabled boolean NOT NULL
);
CREATE TABLE booru.image
(
id bigint NOT NULL PRIMARY KEY,
filename varchar NOT NULL UNIQUE,
uploader bigint NOT NULL,
id bigserial NOT NULL PRIMARY KEY,
filename varchar NOT NULL UNIQUE,
uploader bigint NOT NULL,
CONSTRAINT uploader FOREIGN KEY (uploader) REFERENCES booru.member (id)
);
CREATE TABLE booru.role
(
id bigint NOT NULL PRIMARY KEY,
name varchar NOT NULL
id bigserial NOT NULL PRIMARY KEY,
name varchar NOT NULL
);
CREATE TABLE booru.tag
(
id bigint NOT NULL PRIMARY KEY,
tag varchar NOT NULL UNIQUE
id bigserial NOT NULL PRIMARY KEY,
tag varchar NOT NULL UNIQUE
);
CREATE TABLE booru.tag_image
@ -43,17 +43,22 @@ CREATE TABLE booru.tag_image
CREATE TABLE booru.member_roles
(
member_id bigint NOT NULL,
role_id bigint NOT NULL,
role_id bigint NOT NULL,
CONSTRAINT role FOREIGN KEY (role_id) REFERENCES booru.role (id),
CONSTRAINT member FOREIGN KEY (member_id) REFERENCES booru.member (id)
);
CREATE SEQUENCE IF NOT EXISTS booru.member_id_seq START WITH 1 INCREMENT BY 1;
CREATE SEQUENCE IF NOT EXISTS booru.image_id_seq START WITH 1 INCREMENT BY 1;
CREATE SEQUENCE IF NOT EXISTS booru.role_id_seq START WITH 1 INCREMENT BY 1;
CREATE SEQUENCE IF NOT EXISTS booru.tag_id_seq START WITH 1 INCREMENT BY 1;
-- Populate default values
INSERT INTO booru.role (id, name)
VALUES (1, 'ADMIN'),
(2, 'USER');
INSERT INTO booru.member (id, name, email, password, enabled)
VALUES (1, 'root', 'root@example.org', '$2a$10$XiMxQ1Qe.AprOdJKUPum6O2URy2b4ogffujVZVpUT8CJPksfSyNZK', TRUE);
INSERT INTO booru.role (name)
VALUES ('ADMIN'),
('USER');
INSERT INTO booru.member (name, email, password, enabled)
VALUES ('root', 'root@example.org', '$2a$10$XiMxQ1Qe.AprOdJKUPum6O2URy2b4ogffujVZVpUT8CJPksfSyNZK', TRUE);
INSERT INTO booru.member_roles (member_id, role_id)
VALUES (1, 1),
(1, 2);

View File

@ -1,3 +1,3 @@
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.url=jdbc:h2:mem:testdb;MODE=PostgreSQL
spring.datasource.driverClassName=org.h2.Driver
images.directory=./images