-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
44 lines (36 loc) · 1.49 KB
/
Main.java
File metadata and controls
44 lines (36 loc) · 1.49 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
package me.gregorsomething.example;
import me.gregorsomething.database.*;
import java.sql.SQLException;
public class Main {
public static void main(String[] args) {
// Make database
DatabaseDetails details = DatabaseDetails.builder()
.dbURL("127.0.0.1")
.user("postgres")
.password("postgres")
.dbName("test")
.build();
Database database = PostgresDatabaseProvider.of(details);
// Init repository
UserRepository userRepository = RepositoryProvider.create(UserRepository.class, database);
// Read
User user = userRepository.byId(1);
// Use in transaction
userRepository.inTransaction(repo -> {
repo.addUser("Test1", new Email("test", "example.com"));
repo.addUser("Test2", new Email("test2", "example.com"));
});
// Other type of transactions
Transaction transaction = userRepository.getNewTransaction();
try (transaction) {
UserRepository repoInTransaction = userRepository.asTransactional(transaction);
repoInTransaction.addUser("Test1", new Email("test", "example.com"));
repoInTransaction.addUser("Test2", new Email("test2", "example.com"));
transaction.commit();
} catch (SQLException e) {
// No rollback needed, close handels that
throw new RuntimeException(e);
}
userRepository.updateUser(user);
}
}