Skip to content

Commit

Permalink
Completes example
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperancinha committed Jan 30, 2024
1 parent f436342 commit 9abb7a6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
Expand Up @@ -2,24 +2,31 @@ package org.jesperancinha.repeateddislikes.fix.domain


import jakarta.persistence.*
import java.util.UUID
import java.util.*

const val SCHEMA_BAD = "fix"
const val SCHEMA = "fix"

@Table(name = "users", schema = SCHEMA_BAD)
@Table(name = "users", schema = SCHEMA)
@Entity
data class User(
@Id
@GeneratedValue(strategy = GenerationType.UUID)
val id: UUID,
val name: String,
@OneToMany(cascade = [CascadeType.DETACH], fetch = FetchType.LAZY)
@OneToMany(cascade = [CascadeType.DETACH], fetch = FetchType.EAGER)
@JoinTable(
schema = SCHEMA,
name = "USERS_RECEIPTS"
)
val receipts: List<Receipt>,
@OneToMany(cascade = [CascadeType.DETACH], fetch = FetchType.LAZY)
val shops: List<Shop>
@OneToMany(cascade = [CascadeType.DETACH], fetch = FetchType.EAGER)
@JoinTable(
schema = SCHEMA,
name = "USERS_SHOPS"
)val shops: List<Shop>
)

@Table(name = "receipts", schema = SCHEMA_BAD)
@Table(name = "receipts", schema = SCHEMA)
@Entity
data class Receipt(
@Id
Expand All @@ -33,7 +40,7 @@ data class Receipt(



@Table(name = "shops", schema = SCHEMA_BAD)
@Table(name = "shops", schema = SCHEMA)
@Entity
data class Shop(
@Id
Expand Down
14 changes: 12 additions & 2 deletions repeated-dislikes/src/main/resources/data.sql
Expand Up @@ -7,7 +7,7 @@ insert into BAD.SHOPS (NAME)
values ('Cats Para Dice');
SET @receipt1=random_uuid();
SET @receipt2=random_uuid();
SET @userid=select ID from BAD.USERS WHERE NAME = 'CatOne';
SET @userid=(select ID from BAD.USERS WHERE NAME = 'CatOne');
insert into BAD.RECEIPTS (ID, USER_ID, SHOP_ID)
values (@receipt1, @userid,
(select ID from BAD.SHOPS WHERE NAME = 'Cats Para Dice')
Expand Down Expand Up @@ -61,7 +61,7 @@ insert into BADFIX.SHOPS (NAME)
values ('Cats Para Dice BadFix');
SET @receipt1=random_uuid();
SET @receipt2=random_uuid();
SET @userid=select ID from BADFIX.USERS WHERE NAME = 'CatOne BadFix';
SET @userid=(select ID from BADFIX.USERS WHERE NAME = 'CatOne BadFix');
insert into BADFIX.RECEIPTS (ID, USER_ID, SHOP_ID)
values (@receipt1, @userid,
(select ID from BADFIX.SHOPS WHERE NAME = 'Cats Para Dice BadFix')
Expand Down Expand Up @@ -89,12 +89,15 @@ values (random_uuid(), @userid,
truncate table FIX.USERS;
truncate table FIX.SHOPS;
truncate table FIX.RECEIPTS;
truncate table FIX.USERS_RECEIPTS;
truncate table FIX.USERS_SHOPS;
insert into FIX.USERS (ID, NAME)
values (random_uuid(), 'CatOne');
insert into FIX.SHOPS (ID, NAME)
values (random_uuid(), 'Cats Para Dice');
SET @receipt3=random_uuid();
SET @receipt4=random_uuid();
SET @userid=(select ID from FIX.USERS WHERE NAME = 'CatOne');
insert into FIX.RECEIPTS (ID, USER_ID, SHOP_ID)
values (@receipt3,
(select ID from FIX.USERS WHERE NAME = 'CatOne'),
Expand All @@ -105,3 +108,10 @@ values (@receipt4,
(select ID from FIX.USERS WHERE NAME = 'CatOne'),
(select ID from FIX.SHOPS WHERE NAME = 'Cats Para Dice')
);
SET @shopId=(select ID from FIX.SHOPS WHERE NAME = 'Cats Para Dice');
insert into FIX.USERS_RECEIPTS(USER_ID, RECEIPTS_ID)
values (@userid,@receipt3);
insert into FIX.USERS_RECEIPTS(USER_ID, RECEIPTS_ID)
values (@userid,@receipt4);
insert into FIX.USERS_SHOPS(USER_ID, SHOPS_ID)
values (@userid,@shopId);
17 changes: 17 additions & 0 deletions repeated-dislikes/src/main/resources/schema.sql
Expand Up @@ -18,6 +18,8 @@ DROP TABLE IF EXISTS FIX.RECEIPTS;

DROP TABLE IF EXISTS FIX.SHOPS;

DROP TABLE IF EXISTS FIX.USERS_RECEIPTS;

CREATE TABLE IF NOT EXISTS BAD.BAD_DISLIKES_RELATIONS
(
id UUID NOT NULL DEFAULT random_uuid() PRIMARY KEY,
Expand Down Expand Up @@ -95,3 +97,18 @@ CREATE TABLE IF NOT EXISTS FIX.SHOPS
id UUID NOT NULL DEFAULT random_uuid() PRIMARY KEY,
name VARCHAR(255) NOT NULL
);


CREATE TABLE IF NOT EXISTS FIX.USERS_RECEIPTS
(
id UUID NOT NULL DEFAULT random_uuid() PRIMARY KEY,
user_id UUID,
receipts_id UUID
);

CREATE TABLE IF NOT EXISTS FIX.USERS_SHOPS
(
id UUID NOT NULL DEFAULT random_uuid() PRIMARY KEY,
user_id UUID,
shops_id UUID
);

0 comments on commit 9abb7a6

Please sign in to comment.