RHSRV-3 - Implement Hibernate PSQL with basic /register API
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
f85a3982a4
commit
88a9b2b706
12
pom.xml
12
pom.xml
@ -24,6 +24,18 @@
|
|||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
<version>3.3.2</version>
|
<version>3.3.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hibernate</groupId>
|
||||||
|
<artifactId>hibernate-core</artifactId>
|
||||||
|
<version>5.6.15.Final</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.postgresql</groupId>
|
||||||
|
<artifactId>postgresql</artifactId>
|
||||||
|
<version>42.7.3</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
package wtf.beatrice.releasehive;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class AccountController {
|
||||||
|
|
||||||
|
private final AccountService accountService = new AccountService();
|
||||||
|
|
||||||
|
|
||||||
|
@PostMapping("/register")
|
||||||
|
public String register(
|
||||||
|
@RequestParam(name = "username") String username,
|
||||||
|
@RequestParam(name="password") String password)
|
||||||
|
{
|
||||||
|
|
||||||
|
UUID id = UUID.randomUUID();
|
||||||
|
User user = new User();
|
||||||
|
user.setUsername(username);
|
||||||
|
user.setPassword(password);
|
||||||
|
user.setUuid(id);
|
||||||
|
accountService.registerUser(user);
|
||||||
|
return "user registered, " + username + ", " + password + ", " + id.toString();
|
||||||
|
}
|
||||||
|
}
|
15
src/main/java/wtf/beatrice/releasehive/AccountService.java
Normal file
15
src/main/java/wtf/beatrice/releasehive/AccountService.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package wtf.beatrice.releasehive;
|
||||||
|
|
||||||
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.Transaction;
|
||||||
|
|
||||||
|
public class AccountService
|
||||||
|
{
|
||||||
|
|
||||||
|
public void registerUser(User user) {
|
||||||
|
Session session = HibernateManager.getSession();
|
||||||
|
Transaction tx = session.beginTransaction();
|
||||||
|
session.save(user);
|
||||||
|
tx.commit();
|
||||||
|
}
|
||||||
|
}
|
44
src/main/java/wtf/beatrice/releasehive/HibernateManager.java
Normal file
44
src/main/java/wtf/beatrice/releasehive/HibernateManager.java
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package wtf.beatrice.releasehive;
|
||||||
|
|
||||||
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
|
import org.hibernate.Transaction;
|
||||||
|
import org.hibernate.cfg.Configuration;
|
||||||
|
|
||||||
|
public class HibernateManager
|
||||||
|
{
|
||||||
|
private static HibernateManager instance;
|
||||||
|
private static Session session;
|
||||||
|
private static SessionFactory sessionFactory;
|
||||||
|
|
||||||
|
|
||||||
|
private HibernateManager() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void initialize() {
|
||||||
|
sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void shutdown() {
|
||||||
|
if(session != null && !session.isOpen()) {
|
||||||
|
session.close();
|
||||||
|
}
|
||||||
|
if(sessionFactory != null && sessionFactory.isOpen()) {
|
||||||
|
session.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static Session getSession() {
|
||||||
|
if(session != null && (!session.isOpen() || !session.isConnected())) {
|
||||||
|
session.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
session = sessionFactory.openSession();
|
||||||
|
return session;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Transaction beginTransaction() {
|
||||||
|
return getSession().beginTransaction();
|
||||||
|
}
|
||||||
|
}
|
@ -2,9 +2,14 @@ package wtf.beatrice.releasehive;
|
|||||||
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.Transaction;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
@ -12,9 +17,26 @@ public class Main {
|
|||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
LOGGER.info("Hello world!");
|
LOGGER.info("Hello world!");
|
||||||
|
Runtime.getRuntime().addShutdownHook(shutdownHook);
|
||||||
|
|
||||||
LOGGER.info("Initializing Spring Boot");
|
LOGGER.info("Initializing Spring Boot");
|
||||||
SpringApplication.run(Main.class, args);
|
SpringApplication.run(Main.class, args);
|
||||||
LOGGER.info("Spring Boot initialized!");
|
LOGGER.info("Spring Boot initialized!");
|
||||||
|
|
||||||
|
HibernateManager.initialize();
|
||||||
|
|
||||||
|
Session session = HibernateManager.getSession();
|
||||||
|
Transaction transaction = session.beginTransaction();
|
||||||
|
List<User> users = session.createQuery("FROM User", User.class).getResultList();
|
||||||
|
for (User user : users) {
|
||||||
|
LOGGER.info("ID: {}, Name: {}", user.getUuid(), user.getUsername());
|
||||||
}
|
}
|
||||||
|
transaction.commit();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Thread shutdownHook = new Thread(() -> {
|
||||||
|
HibernateManager.shutdown();
|
||||||
|
LOGGER.info("Shutting down!");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
@ -1,13 +0,0 @@
|
|||||||
package wtf.beatrice.releasehive;
|
|
||||||
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
public class TryController {
|
|
||||||
|
|
||||||
@GetMapping("/try")
|
|
||||||
public String tryMethod() {
|
|
||||||
return "Hello World!";
|
|
||||||
}
|
|
||||||
}
|
|
48
src/main/java/wtf/beatrice/releasehive/User.java
Normal file
48
src/main/java/wtf/beatrice/releasehive/User.java
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
package wtf.beatrice.releasehive;
|
||||||
|
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name="users")
|
||||||
|
public class User
|
||||||
|
{
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@Column
|
||||||
|
private UUID uuid;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
|
||||||
|
@Column
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
public UUID getUuid() {
|
||||||
|
return uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUuid(UUID uuid) {
|
||||||
|
this.uuid = uuid;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
28
src/main/resources/hibernate.cfg.xml
Normal file
28
src/main/resources/hibernate.cfg.xml
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE hibernate-configuration PUBLIC
|
||||||
|
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
|
||||||
|
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||||
|
|
||||||
|
<hibernate-configuration>
|
||||||
|
<session-factory>
|
||||||
|
|
||||||
|
<!-- Connection settings -->
|
||||||
|
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
|
||||||
|
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/releasehive</property>
|
||||||
|
<property name="hibernate.connection.username">relhive</property>
|
||||||
|
<property name="hibernate.connection.password">development</property>
|
||||||
|
|
||||||
|
<!-- SQL dialect -->
|
||||||
|
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
|
||||||
|
|
||||||
|
<!-- Print executed SQL to stdout -->
|
||||||
|
<property name="show_sql">true</property>
|
||||||
|
|
||||||
|
<!-- Update database on startup -->
|
||||||
|
<property name="hibernate.hbm2ddl.auto">update</property>
|
||||||
|
|
||||||
|
<!-- Annotated entity classes -->
|
||||||
|
<mapping class="wtf.beatrice.releasehive.User"/>
|
||||||
|
|
||||||
|
</session-factory>
|
||||||
|
</hibernate-configuration>
|
Loading…
Reference in New Issue
Block a user