forked from sermakov/JavaPatternMirea
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtsk16.java
More file actions
232 lines (198 loc) · 7.29 KB
/
tsk16.java
File metadata and controls
232 lines (198 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package a;
//import com.fasterxml.jackson.annotation.JsonBackReference;
//import com.fasterxml.jackson.annotation.JsonIgnore;
//import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
//import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.persistence.*;
import javax.sql.DataSource;
import javax.transaction.Synchronization;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;
/*
sudo mkdir /run/postgresql
sudo chown postgres /run/postgresql
sudo chgrp postgres /run/postgresql
sudo su - postgres
pg_ctl -D /var/lib/postgres/data start
createdb db
psql -d db
psql -h localhost -p 5432 -U postgres -d db
pg_ctl -D /var/lib/postgres/data stop
# Hibernate: create sequence hibernate_sequence start 1 increment 1
# Hibernate: create table dgs (id int4 not null, bd varchar(255), nm varchar(255), rs_id int4, primary key (id))
# Hibernate: create table urs (id int4 not null, fn varchar(255), ln varchar(255), primary key (id))
# Hibernate: alter table if exists dgs add constraint FK1n8n2m20onaxgxi49a460eb6e foreign key (rs_id) references urs
# Hibernate: select tsk15_usr0_.id as id1_2_0_, tsk15_usr0_.fn as fn2_2_0_, tsk15_usr0_.ln as ln3_2_0_ from usrs tsk15_usr0_ where tsk15_usr0_.id=?
*/
@SpringBootApplication
public class tsk16 {
private static final String db = "db";
private static final String tb1 = "urs";
private static final String tb2 = "dgs";
public static void main(String[] args)
{ SpringApplication.run(tsk16.class, args); }
@SuppressWarnings("DuplicatedCode") @Configuration
public static class A {
@Bean public HikariDataSource ds() {
val a = new HikariConfig();
a.setJdbcUrl("jdbc:postgresql://localhost:5432/" + db);
a.setDriverClassName("org.postgresql.Driver");
a.setUsername("postgres");
a.setPassword("postgres");
return new HikariDataSource(a);
}
@Bean public LocalSessionFactoryBean fc(DataSource b) {
val a = new LocalSessionFactoryBean();
a.setDataSource(b);
a.setPackagesToScan(this.getClass().getPackageName());
val c = new Properties();
c.setProperty("hibernate.dialect",
"org.hibernate.dialect.PostgreSQL92Dialect");
c.setProperty("hibernate.show_sql", "true");
c.setProperty("hibernate.hbm2ddl.auto",
DEBUG ? "create" : "update");
a.setHibernateProperties(c);
return a;
}
@Bean public PlatformTransactionManager
pt(LocalSessionFactoryBean a) {
return new HibernateTransactionManager(
Objects.requireNonNull(a.getObject())); }
}
private static final boolean DEBUG = false; // #define
@Service @RequiredArgsConstructor
public static class sr {
private final SessionFactory sf;
private Session s;
@PostConstruct public void a()
{ s = sf.openSession(); $(); }
@PreDestroy public void b() { s.close(); }
@TestOnly public void $() {
if (!DEBUG) { // #if
System.out.println(gt(1, usr.class));
System.out.println(gt(1, dg.class));
System.out.println(gt(2, dg.class));
} else { // #else
nw(
usr.class,
new usr(
"name",
"surname"),
null,
null);
nw(
dg.class,
new dg(
"dog",
"breed",
gt(1, usr.class)),
null,
null);
nw(
dg.class,
new dg(
"dog",
"breed",
gt(1, usr.class)),
null,
null);
} } // #endif
@FunctionalInterface
public interface clb { void a(); }
@SuppressWarnings("UnusedReturnValue")
public <T> T nw(
Class<?> b,
T a,
@Nullable clb c,
@Nullable AtomicReference<Transaction> e
) {
val d = s.beginTransaction();
s.persist(b.getSimpleName(), a);
d.registerSynchronization(new Synchronization()
{ @Override public void beforeCompletion() {}
@Override public void afterCompletion(int i)
{ if (c != null) c.a(); } });
if (e != null) e.set(d);
d.commit();
return a;
}
public <T> T gt(int a, Class<T> b)
{ return s.get(b, a); }
@Deprecated(forRemoval = true)
public usr gu(int a) { return ((dg) s
.createSQLQuery("select * from %s where id = :a"
.formatted(tb2)
).setParameter("a", a).uniqueResult()).rs; }
@Deprecated(forRemoval = true)
@SuppressWarnings("unchecked")
public List<dg> gds(int a) { return (List<dg>) s
.createSQLQuery("select dgs from %s where id = :a"
.formatted(tb1)
).setParameter("a", a).getResultList(); }
}
@Table(name = tb1) @Entity
@RequiredArgsConstructor
public static class usr {
@GeneratedValue(strategy =
GenerationType.IDENTITY)
@Nullable @Id Integer id;
@NonNull String fn;
@NonNull String ln;
@OneToMany(
mappedBy = "rs",
fetch = FetchType.EAGER)
// @JsonIgnoreProperties("rs")
// @JsonManagedReference
List<dg> dgs = new ArrayList<>();
public usr() {}
@Override public String toString()
{ return "(%d %s %s %s)"
.formatted(
id,
fn,
ln,
dgs
.stream()
.map(a -> "(%d %s %s)"
.formatted(a.id, a.nm, a.bd))
.toList()); }
}
@Table(name = tb2) @Entity @ToString
@RequiredArgsConstructor
public static class dg {
@GeneratedValue(strategy =
GenerationType.IDENTITY)
@Id @Nullable Integer id;
@NonNull String nm;
@NonNull String bd;
@ManyToOne
// @JoinColumn(
// name = "rs_id",
// nullable = false,
// updatable = false)
// @JsonBackReference
@NonNull usr rs;
public dg() {}
}
}